在HSV中:H是色调,取值为[0-180]。S是饱和度,取值为[0-255]。V是黑色度,取值为[0-255]。
获取lena原图的HSV图的代码:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow(“test”,img_hsv)
cv2.waitKey(0)
显示结果:
原图:
HSV色调图:
一,对H色调进行设置时:
实例代码:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:,:,0] = (turn_green_hsv[:,:,0]-30) % 180
cv2.imshow(“test”,turn_green_hsv)
cv2.waitKey(0)
显示结果:
再将HSV色调强制转换为BGR后可以得到HSV色调调整图:
代码如下:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:,:,0] = (turn_green_hsv[:,:,0]-30) % 180
turn_green_img = cv2.cvtColor(turn_green_hsv,cv2.COLOR_HSV2BGR)
cv2.imshow(“test”,turn_green_img)
cv2.waitKey(0)
显示结果:
二,对饱和度进行调节时,lena原图中饱和度默认为1。
实例代码:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:,:,1] = turn_green_hsv[:,:,1] * 0.7
turn_green_img = cv2.cvtColor(turn_green_hsv,cv2.COLOR_HSV2BGR)
cv2.imshow(“test”,turn_green_img)
cv2.waitKey(0)
在代码中对饱和度设置为0.7,则得到的输出结果为:
当0.7设置为1.4时:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:,:,1] = turn_green_hsv[:,:,1] * 1.4
turn_green_img = cv2.cvtColor(turn_green_hsv,cv2.COLOR_HSV2BGR)
cv2.imshow(“test”,turn_green_img)
cv2.waitKey(0)
显示结果为:
三,当对明暗度进行调节时:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:,:,2] = turn_green_hsv[:,:,2] * 0.7
turn_green_img = cv2.cvtColor(turn_green_hsv,cv2.COLOR_HSV2BGR)
cv2.imshow(“test”,turn_green_img)
cv2.waitKey(0)
当明暗度为0.7时:
当明暗度为1.4时:
import cv2
img = cv2.imread(“C:\lena.jpg”,1)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:,:,2] = turn_green_hsv[:,:,2] * 1.4
turn_green_img = cv2.cvtColor(turn_green_hsv,cv2.COLOR_HSV2BGR)
cv2.imshow(“test”,turn_green_img)
cv2.waitKey(0)
显示结果: