2

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?

petrichor
  • 6,459
  • 4
  • 36
  • 48
Andrei Ivanov
  • 651
  • 1
  • 9
  • 23
  • Isn't rgb a 3 dimensional array. Can you tell us what gives `self.rgb.ndim`? – petrichor May 28 '13 at 18:22
  • So, you shouldn't use `self.yuv[:,:]`. Here's a good answer for HSV conversion. You may like to check it: http://stackoverflow.com/questions/4890373/detecting-thresholds-in-hsv-color-space-from-rgb-using-python-pil/4890878#4890878 – petrichor May 28 '13 at 18:28
  • What is `self.rgb.shape`? `(X,Y,3)`? – jorgeca May 28 '13 at 19:47

1 Answers1

5

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)
Jaime
  • 65,696
  • 17
  • 124
  • 159