麻点产品 [英] Numpy dot product
问题描述
我有一个用Numpy数组表示的图像,即每个像素都是一个数组[r,g,b].现在,我想使用矩阵乘法并尝试不使用循环将其转换为YUV.
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[:,:])
我得到了错误-对象未对齐.我猜这是因为self.yuv [i,j]不是垂直向量.转置无济于事.
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.
有什么想法吗?
推荐答案
您的矩阵的形状为(3,3)
,而您的图像的形状为(rows,cols,3)
和 np.dot
进行在a的最后一个轴和b的倒数第二个轴上的和积".
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."
最简单的解决方案是反转 np.dot
中操作数的顺序并转置转换矩阵:
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)
这篇关于麻点产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!