YUV色彩空間是把亮度(Luma)與色度(Chroma)分離。
“Y”表示亮度,也就是灰度值。
“U”表示藍色通道與亮度的差值。
“V”表示紅色通道與亮度的差值。
對彩色圖像進行直方圖均衡化時,先將圖像從RGB空間轉到YUV空間,然後對亮度Y通道進行直方圖均衡化得到通道Y",然後將Y"UV通道進行合併。代碼如下:
import cv2
import numpy as np
img = cv2.imread('input.jpg')
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
# equalize the histogram of the Y channel
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
# convert the YUV image back to RGB format
img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imshow('Color input image', img)
cv2.imshow('Histogram equalized', img_output)
cv2.waitKey(0)