Question

I have an image represented with a Numpy array, i.e. each pixel is an array [r,g,b]. Now, I want to convert it in YUV using matrix multiplication and trying not to use loops.

self.yuv=self.rgb
self.yuv=dot([[   0.299,  0.587,    0.114  ],
              [-0.14713, -0.28886,  0.436  ],
              [   0.615, -0.51499, -0.10001]], 
             self.yuv[:,:])

I get the error - objects not aligned. I guess that's because self.yuv[i,j] is not a vertical vector. transpose doesn't help.

Any ideas?

Was it helpful?

Solution

Your matrix has shape (3, 3) while your image has shape (rows, cols, 3) and np.dot does "a sum product over the last axis of a and the second-to-last of b."

The simplest solution is to reverse the order of the operands inside np.dot and transpose your conversion matrix:

rgb2yuv = np.array([[0.299, 0.587, 0.114],
                    [-0.14713, -0.28886, 0.436],
                    [0.615, -0.51499, -0.10001]])
self.yuv = np.dot(self.rgb, rgb2yuv.T)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top