本文介绍了numpy将RGB图像转换为YIQ色彩空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

对于一个类,我需要将RGB图像转换为YIQ.有人告诉我们可以通过以下方式进行转换:

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

我开始用循环编写一个凌乱的代码以进行矩阵乘法,然后找到一个函数

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

arr只是RGB图片作为矩阵时

when arr is just the RGB pic as a matrix

我想了解为什么这可行?他们为什么要使用转置矩阵? (.T)当arr形状不同于yiq_from_rgb时,点积如何工作?

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?

推荐答案

在包含转换矩阵的参考图中,转换矩阵位于RGB通道的左侧.因此,对于RGB图像中的第一个像素,我们分别将其称为(p1r, p1g, p1b),分别与R,G,B通道相对应,我们需要与转换矩阵相乘,然后将结果求和,如下所示:

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)

其中,(y1y,y1i,y1q)是在四舍五入/取int后得到的YIQ图像中第一个像素的值.我们对整个RGB图像中的所有像素执行相同的乘法运算,以获得所需的YIQ图像.

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.

现在,由于他们使用np.dot(arr, yiq_from_rgb.T)完成了整个实现,因此要正确计算权重,需要对转换矩阵进行转置.而copy只是具有专用的转置转换矩阵以进行此转换.

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.

此外,请注意,与您的图相反,在np.dot()中,RGB阵列位于变换矩阵的左侧.

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

这篇关于numpy将RGB图像转换为YIQ色彩空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:08