图像处理
颜色转换
颜色转换一般有150多种,常用的有2种BGR ↔ Gray, BGR ↔ HSV
注意
HSV 色调范围[0,179], 饱和度范围[0,255], 值得范围[0,255]
应用场景
目标跟踪
我们可以利用颜色特征,颜色对象,利用HSV更加容易得到,步骤如下:
抽取视频得每一帧
转换BGR->HSV
后去HSV得蓝色得阈值
抽取蓝色图像对象
import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while(1):
# Take each frame
_, frame = cap.read()
# Convert BGR to HSV
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
# Threshold the HSV image to get only blue colors
mask = cv.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv.bitwise_and(frame,frame, mask= mask)
cv.imshow('frame',frame)
cv.imshow('mask',mask)
cv.imshow('res',res)
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
如何获取颜色对应的HSV值?
green = np.uint8([[[0,255,0 ]]])
hsv_green = cv.cvtColor(green,cv.COLOR_BGR2HSV)
print( hsv_green )
[[[ 60 255 255]]]