我要将RGB图像转换为YIQ图像,反之亦然.问题是Python给我一个奇怪的图像,而MATLAB显示正确的图像.我花了几个小时来弄清楚什么是错的,但我仍然不知道.
我使用Python 3.5.2与OpenCV 3.1.0和MATLAB R2016a.
RGB2YIQ的Python代码:
import cv2 as cv
import numpy as np
def rgb2yiq(img):
row, col, ch = img.shape
Y = np.zeros((row,col))
I = np.zeros((row,col))
Q = np.zeros((row,col))
for i in range(row):
for j in range(col):
Y[i,j] = 0.299 * img[i,j,2] + 0.587 * img[i,j,1] + 0.114 * img[i,j,0]
I[i,j] = 0.596 * img[i,j,2] - 0.274 * img[i,j,1] - 0.322 * img[i,j,0]
Q[i,j] = 0.211 * img[i,j,2] - 0.523 * img[i,j,1] + 0.312 * img[i,j,0]
yiq = cv.merge((Y,I,Q))
return yiq.astype(np.uint8)
def main():
img = cv.imread("C:/Users/Kadek/Documents/MATLAB/peppers.jpg")
img = rgb2yiq(img)
cv.imwrite("YIQ.jpg&#