Python Opencv转换颜色空间 RGB转为HSV
举个例子,通过摄像头把视频中的蓝色区域凸显出来,剩余的颜色均为黑色
import numpy as npcap = cv2.VideoCapture(0)while(1):# Take each frame_, frame = cap.read()# Convert BGR to HSVhsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)# define range of blue color in HSVlower_blue = np.array([110,50,50])upper_blue = np.array([130,255,255])# Threshold the HSV image to get only blue colorsmask = cv2.inRange(hsv, lower_blue, upper_blue)# Bitwise-AND mask and original imageres = cv2.bitwise_and(frame,frame, mask= mask)cv2.imshow('frame',frame)cv2.imshow('mask',mask)cv2.imshow('res',res)k = cv2.waitKey(5) & 0xFFif k == 27:breakcv2.destroyAllWindows()
效果
任意RGB颜色转为HSV例子
green = np.uint8([[[0,255,0 ]]])hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)print hsv_green[[[ 60 255 255]]]
则绿色范围为[H-10, 100,100] 到 [H+10, 255, 255]
H为60