python - RGB 到 YIQ 并返回 python
问题描述
我有一项任务需要仅使用简单的函数将 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
但这在我的课堂上不是一个有效的答案,关于如何一步完成的任何想法?
解决方案
经过多次反复试验,我找到了解决方案。
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
推荐阅读
- javascript - 如何捕获来自多个功能的拒绝?
- java - 如何在java4中解析json文件作为输入
- angular - Angular Google Maps 航路点不与动态航路点数组一起显示
- postgresql - postgresql 无法创建数据库
- java - 从 AsyncTask 回调接口访问 Activity 方法
- sas - is there a way to keep and update a "list" in a %let variable?
- python - Pandas - 检查列表列中的所有元素是否在另一个列表中
- swift - 为什么使用字符串插值比在表达式中使用 nil-coalescing 编译速度更快?
- reactjs - 每个 React Native 的 API Fetch - 与浏览器相比不同的终端日志
- security - 能否在 Outlook 365 中禁用 MAPI?