RGB,YUV和HSV色彩空间转换

空间模型介绍和转换公式可参考 RGB、YUV和HSV颜色空间模型
RGB和YUV转换

def RGB2YUV(img):
    R,G,B = np.split(img, 3, axis=2)
    Y = 0.299*R + 0.587*G + 0.114*B
    U = 128 - 0.1687*R - 0.3313*G + 0.5* B
    V = 0.5*R - 0.4187*G - 0.0813*B + 128 
    return Y, U, V

def YUV2RGB(Y,U,V):
    R = Y + 1.402* (V-128)
    G = Y - 0.34414* (U-128) - 0.71414* (V-128)
    B = Y + 1.772* (U-128)
    img = np.concatenate((R,G,B), axis=2)
    return img

RGB和HSV转换

# 利用opencv实现
### 注意opencv转换后的HSV值域为:H: 0-180 S:  0-255 V: 0-255
img_bgr =cv2.imread(img_path)
img_hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)
planes = cv2.split(img_hsv)
### 此处可对split的通道进行处理
h = random.randint(3,10)
planes[0] += h
planes[0] = np.clip(planes[0],0,180)
s = random.randint(20,40)
planes[1] += s
planes[1] = np.clip(planes[1],0,255)
v = random.uniform(0.8, 0.9) 
planes[2] = (planes[2]*v).astype(np.uint8)
#planes[2] = (((planes[2]/255)**v)*255).astype(np.uint8)
### 
img_hsvAD = cv2.merge(planes)
img_bgrAD = cv2.cvtColor(np.clip(img_hsvAD,0,255),cv2.COLOR_HSV2RGB)

# 对pixel实现,转换后值域为 H :[0, 360]. S, V: [0, 1].
## 参考 [HSVtoRGB1,0.py ](https://github.com/RansonTham42/Python/blob/master/HSVtoRGB/HSVtoRGB1%2C0.py)
## 参考 [Python RGB 和HSV颜色相互转换](https://www.cnblogs.com/huangshiyu13/p/7521105.html)


你可能感兴趣的:(RGB,YUV和HSV色彩空间转换)