numpy RGB到YIQ并返回Python

uqjltbpv  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(128)

我有一个任务需要转换RGB到YIQ和回来只使用简单的函数(在lib:plt cv2 np)
我得到的是“天真”的代码解决方案:

def transformRGB2YIQ(imgRGB: np.ndarray) -> np.ndarray:
    """
    Converts an RGB image to YIQ color space
    :param imgRGB: An Image in RGB
    :return: A YIQ in image color space
    """

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

    YIQ = np.dot(imgRGB.reshape(-1, 3), yiq_from_rgb).reshape(imgRGB.shape)

    return YIQ

    pass

def transformYIQ2RGB(imgYIQ: np.ndarray) -> np.ndarray:
    """
    Converts an YIQ image to RGB color space
    :param imgYIQ: An Image in YIQ
    :return: A RGB in image color space
    """
    yiq_from_rgb = np.array([[0.299, 0.587, 0.114],
                             [0.59590059, -0.27455667, -0.32134392],
                             [0.21153661, -0.52273617, 0.31119955]])
    rgb_from_yiq = np.linalg.inv(yiq_from_rgb)
    RGB = np.dot(imgYIQ.reshape(-1, 3), rgb_from_yiq).reshape(imgYIQ.shape)

    return RGB
    pass

字符串
我尝试使用np.dot并重新塑造img,以便我可以将其乘以矩阵,就像这样:


的数据
但运气不好,我答错了
还尝试:

def transformRGB2YIQ(imgRGB: np.ndarray) -> np.ndarray:
    """
    Converts an RGB image to YIQ color space
    :param imgRGB: An Image in RGB
    :return: A YIQ in image color space
    """

    YIQ = np.ndarray(imgRGB.shape)

    YIQ[:, :, 0] = 0.299 * imgRGB[:, :, 0] + 0.587 * imgRGB[:, :, 1] + 0.114 * imgRGB[:, :, 2]
    YIQ[:, :, 1] = 0.59590059 * imgRGB[:, :, 0] + (-0.27455667) * imgRGB[:, :, 1] + (-0.32134392) * imgRGB[:, :, 2]
    YIQ[:, :, 2] = 0.21153661 * imgRGB[:, :, 0] + (-0.52273617) * imgRGB[:, :, 1] + 0.31119955 * imgRGB[:, :, 2]

    return YIQ

    pass

def transformYIQ2RGB(imgYIQ: np.ndarray) -> np.ndarray:
    """
    Converts an YIQ image to RGB color space
    :param imgYIQ: An Image in YIQ
    :return: A RGB in image color space
    """
    yiq_from_rgb = np.array([[0.299, 0.587, 0.114],
                             [0.59590059, -0.27455667, -0.32134392],
                             [0.21153661, -0.52273617, 0.31119955]])
    rgb_from_yiq = np.linalg.inv(yiq_from_rgb)

    RGB = np.ndarray(imgYIQ.shape)
    RGB[:, :, 0] = 1.00000001 * imgYIQ[:, :, 0] + 0.95598634 * imgYIQ[:, :, 1] + 0.6208248 * imgYIQ[:, :, 2]
    RGB[:, :, 1] = 0.99999999 * imgYIQ[:, :, 0] + (-0.27201283) * imgYIQ[:, :, 1] + (-0.64720424) * imgYIQ[:, :, 2]
    RGB[:, :, 2] = 1.00000002 * imgYIQ[:, :, 0] + (-1.10674021) * imgYIQ[:, :, 1] + 1.70423049 * imgYIQ[:, :, 2]

    return RGB
    pass

这不是一个有效的答案在我的课上,任何想法的如何使它在一个单一的步骤?

yhqotfr8

yhqotfr81#

经过多次尝试和错误,我找到了一个解决方案。

def transformRGB2YIQ(imgRGB: np.ndarray) -> np.ndarray:
    """
    Converts an RGB image to YIQ color space
    :param imgRGB: An Image in RGB
    :return: A YIQ in image color space
    """
    yiq_from_rgb = np.array([[0.299, 0.587, 0.114],
                             [0.59590059, -0.27455667, -0.32134392],
                             [0.21153661, -0.52273617, 0.31119955]])
    OrigShape=imgRGB.shape
    return np.dot(imgRGB.reshape(-1,3), yiq_from_rgb.transpose()).reshape(OrigShape)

    pass

def transformYIQ2RGB(imgYIQ: np.ndarray) -> np.ndarray:
    """
    Converts an YIQ image to RGB color space
    :param imgYIQ: An Image in YIQ
    :return: A RGB in image color space
    """
    yiq_from_rgb = np.array([[0.299, 0.587, 0.114],
                             [0.59590059, -0.27455667, -0.32134392],
                             [0.21153661, -0.52273617, 0.31119955]])
    OrigShape=imgYIQ.shape
    return np.dot(imgYIQ.reshape(-1,3), np.linalg.inv(yiq_from_rgb).transpose()).reshape(OrigShape)

    pass

字符串

5w9g7ksd

5w9g7ksd2#

我能够使用numpy的matmul从RGB转换为YIQ,它能够对图像的每个像素执行矩阵乘法。注意,我使用的是numpy 1.24.3。dot函数也可以工作,在这种情况下,它计算每个像素的和积。更多细节请参见文档。
第一个月
下面的代码有两个实现。

def rgb2yiq(image_rgb):
    """ Converts an RGB image to YIQ colorspace
        https://en.wikipedia.org/wiki/YIQ
        http://www.progmat.uaem.mx:8080/artVol2Num2/Articulo3Vol2Num2.pdf
        """
    
    rgb2yiq_arr = np.array([[0.29889531, 0.5866247, 0.11448223],
                            [0.59597799, -0.27417610, -0.32180189],
                            [0.21147017, -0.52261711, 0.31114694]])
    
    return image_rgb @ rgb2yiq_arr # same as matmul
    return np.matmul(image_rgb, rgb2yiq_arr)
    # return np.dot(image_rgb, rgb2yiq_arr)


def yiq2rgb(image_yiq):
    """ Converts an RGB image to YIQ colorspace
        https://en.wikipedia.org/wiki/YIQ
        http://www.progmat.uaem.mx:8080/artVol2Num2/Articulo3Vol2Num2.pdf
        """
    
    yiq2rgb_arr = np.array([[1.0, 0.95608445, 0.62088850],
                            [1.0, -0.27137664, -0.64860590],
                            [1.0, -1.10561724, 1.70250126]])
    
    return image_yiq @ yiq2rgb_arr # same as matmul
    # return np.matmul(image_yiq, yiq2rgb_arr)
    # return np.dot(image_yiq, yiq2rgb_arr)

字符串

相关问题