What is the best way to display each ycbcr channel of an image separately in Python with numpy, scipy, skimage etc?

891 views Asked by At

UPDATED:This function is supposed to take in an ndarray (eg, one obtained from pyplot.imread()) and display each YCbCr channel separately:

import numpy as np
import matplotlib.pyplot as plt
from skimage import color

def plot_ycbcr(a):
    b = color.rgb2ycbcr(a) # we assume the input image is in RGB--if not, this will
                            # not give the correct output
    b = b.astype(int)

    plt.figure()
    for i in range(3):
        plt.subplot(1,3,i+1)
        temp = np.zeros(b.shape,dtype=b.dtype)
        temp[:,:,i] = b[:,:,i]
        plt.imshow(color.ycbcr2rgb(temp))

This seems like it should work, but for some reason when I try to convert back to rgb I get very tiny (e-10) floating point numbers, both positive and negative. As far as I know RGB images aren't even supposed to have negative values... what's going on here and how can I fix it?

OLD POST:

def plot_ycbcr(a):
    b = color.rgb2ycbcr(a) # we assume the input image is in RGB--if not, this will
                            # not give the correct output
    plt.figure()
    for i in range(3):
        plt.subplot(1,3,i+1)
        temp = np.zeros(b.shape,dtype=b.dtype)
        temp[:,:,i] = b[:,:,i]
        plt.imshow(temp)

This is based off of a similar function that plots each RGB channel of an image. With that, it was easy, because imshow(), as far as I can tell, will display the image from any array with the shape (x,y,3) using RGB encoding by default.

I figured there would be some kwarg I could pass in to change the image encoding mode, but I was unable to find such a thing. I know I could figure out a workaround by converting channels individually or something but that sounds like a pain in the butt. So what's the easiest way to get these out in YCbCr? Can it be done using only the libraries I imported (and maybe scipy)?

0

There are 0 answers