RGB в YIQ и обратно python - PullRequest
       3

RGB в YIQ и обратно python

0 голосов
/ 21 апреля 2020

У меня была задача, необходимая для преобразования 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, чтобы я мог умножить его на матрицу, например:

enter image description here

но не повезло ... Я получил неправильный ответ.

Также попробовал:

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

НО это не правильный ответ в моем классе, любая идея включена как сделать это за один шаг?

1 Ответ

0 голосов
/ 21 апреля 2020

Через много проб и ошибок я нашел решение.

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
...