0

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)?

  • What do you want the result to look like? The described way of plotting the single RGB channels is somehow misleading, since you simply neglect the two other channels. Basically, these are pseudo color images, which quite nicely correspond due to using the RGB color space here. So, you could smply set up any other pseudo color map for the Y, Cb, Cr channels, too. But, there's no simple correspondence to any "color" like for the RGB color space. That's why: Please provide an image of your desired output. – HansHirse May 05 '21 at 10:57
  • This would be an example https://en.wikipedia.org/wiki/YCbCr#/media/File:Barns_grand_tetons_YCbCr_separation.jpg. I want to encode the image into YCbCr and then display it in YCbCr. Something like `imshow(colorspace='YCbCr')` would work if it existed. – aimlesslegs May 07 '21 at 15:09

0 Answers0