Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy transforming RGB image to YIQ color space

For a class, I need to transform RGB image into YIQ. We have been told that the conversion can be made by:

transforming rgb to yiq

I started to write a messy code with loops to have the matrix multiplication and then I found out a function

 skimage.color.yiq2rgb(imYIQ)

and when I looked inside to see what they were doing I saw the following (I'm copying stuff so it will be more clear):

yiq_from_rgb = yiq_from_rgb = np.array([[0.299,      0.587,        0.114],
                                 [0.59590059, -0.27455667, -0.32134392],
                                 [0.21153661, -0.52273617, 0.31119955]])
return np.dot(arr, yiq_from_rgb.T.copy())

when arr is just the RGB pic as a matrix

I'm trying to understand why this works? why do they take the Transpose matrix? (.T) And how exactly does the dot product work when the arr shape is different than the yiq_from_rgb?

like image 576
Dvir Itzko Avatar asked Oct 28 '17 14:10

Dvir Itzko


People also ask

How do you convert RGB to YIQ?

The RGB to YIQ conversion is defined as: [Y] [0.299 0.587 0.114] [R] [I] = [0.596 -0.275 -0.321] [G]

How to convert RGB to lab color space in Python?

The rgb2lab() function from the scikit-image color module was used to convert an image from RGB into the Lab color space.

How do I convert an image from RGB to grayscale?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.


1 Answers

In your reference figure containing the matrix for the conversion, the transformation matrix is on the left of the RGB channels. So, for the first pixel in your RGB image, let's call it (p1r, p1g, p1b) corresponding to R, G, B channels respectively, we need to multiply with the transformation matrix and sum the results like:

y1y = (0.299*p1r + 0.587*p1g + 0.114*p1b)
y1i = (0.596*p1r - 0.275*p1g - 0.321*p1b)
y1q = (0.212*p1r - 0.523*p1g + 0.311*p1b)

where (y1y,y1i,y1q) is the value for the first pixel in the resulting YIQ image, after rounding/taking int. We do the same kind of multiplication for all the pixels in the whole RGB image and obtain the desired YIQ image.

Now, since they do this whole implementation using np.dot(arr, yiq_from_rgb.T), to have the weighting work out correctly the transformation matrix needs to be transposed. And copy is just to have a dedicated of the transposed transformation matrix for the purpose of this conversion.

Also, notice that contrary to your figure, in np.dot() the RGB array is on the left of transformation matrix.

like image 108
kmario23 Avatar answered Oct 05 '22 03:10

kmario23