参考:https://baike.baidu.com/item/HSV/547122?fr=aladdin 百度百科
参考:https://www.cnblogs.com/helenlee01/p/12707290.html
HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一种颜色空间, 也称六角锥体模型(Hexcone Model)。
这个模型中颜色的参数分别是:色调(H),饱和度(S),明度(V)。
import cv2
img=cv2.imread('test3.png', cv2.IMREAD_COLOR) # 打开文件
# 通过cv2.cvtColor把图像从BGR转换到HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# H空间中,绿色比黄色的值高一点,所以给每个像素+15,黄色的树叶就会变绿
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:, :, 0] = (turn_green_hsv[:, :, 0]+15) % 180
turn_green_img = cv2.cvtColor(turn_green_hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('image',turn_green_img)
#cv2.imwrite('./out/turn_green.jpg', turn_green_img)
cv2.waitKey(0)
# 减小饱和度会让图像损失鲜艳,变得更灰
colorless_hsv = img_hsv.copy()
colorless_hsv[:, :, 1] = 0.5 * colorless_hsv[:, :, 1]
colorless_img = cv2.cvtColor(colorless_hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('image',colorless_img)
cv2.waitKey(0)
#cv2.imwrite('./out/colorless.jpg', colorless_img)
# 调整明度全为最大,相当于只在意颜色种类,与光线无关了
darker_hsv = img_hsv.copy()
darker_hsv[:, :, 2] =255 #0.5 * darker_hsv[:, :, 2]
darker_img = cv2.cvtColor(darker_hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('image',darker_img)
cv2.waitKey(0)
#cv2.imwrite('./out/darker.jpg', darker_img)