将RGB转换为YIQ并返回RGB Python

时间:2019-03-10 12:10:18

标签: python opencv image-processing rgb

需要此代码的帮助,它发出了一个奇怪的图像。 使用Python和OpenCV

RGB-> YIQ

def RGBtoYIQ():

path = metodos.SelectIMG()
img = cv2.imread(path)
w, h ,c= img.shape
print(img.shape)
Y = np.zeros((w, h))
I = np.zeros((w, h))
Q = np.zeros((w, h))
for i in range(0, w):
    for j in range(0, h):
        R = img[i, j, 2]
        G = img[i, j, 1]
        B = img[i, j, 0]
        # RGB -> YIQ
        Y[i,j] = int((0.299*R) + (0.587 * G) + (0.114 * B))


        I[i,j] = int((0.596 * R) - (0.274 * G) - (0.322 * B))


        Q[i,j] = int((0.211 * R) - (0.523 * G) + (0.312 * B))



yiq = cv2.merge((Y, I, Q))

img_out = yiq.astype(np.uint8)
cv2.imwrite("YIQ.jpg", img_out)
cv2.imshow('Image YIQ', img_out)
cv2.waitKey(0)
cv2.destroyAllWindows()
return img_out

结果显示如下: This(rgB_>YIQ) 当我尝试使用:p转换回RGB时

def YIQtoRGB(img):

w, h, c = img.shape
R = np.zeros((w, h))
G = np.zeros((w, h))
B = np.zeros((w, h))
for i in range(0, w):
    for j in range(0, h):
        Y = img[i, j, 2]
        I = img[i, j, 1]
        Q = img[i, j, 0]
        R[i, j] = int((1.000 * Y) + (0.956 * I) + (0.621 * Q))
        R[i, j] = CheckLimite(R[i, j]) #check if pixel>255 or <0
        G[i, j] = int((1.000 * Y) - (0.272 * I) - (0.647 * Q))
        G[i, j] = CheckLimite(G[i, j])
        B[i, j] = int((1.000 * Y) - (1.106 * I) + (1.703 * Q))
        B[i, j] = CheckLimite(B[i, j])

RGB = cv2.merge((B, G, R))
img_out = RGB.astype(np.uint8)
cv2.imwrite("YIQtoRGB.jpg", img_out)
cv2.imshow('Image  RGB', img_out)
cv2.waitKey(0)
cv2.destroyAllWindows()

产生类似this (YIQ->RGB)

的结果

有人可以帮我解决,代码有什么问题吗?

0 个答案:

没有答案