opencv 有150种空间转换方法,我们只研究最长用的两种:
1.BGR->灰色 flag=BGR2GRAY
2.BGR->HSV flag=BGR2HSV
函数使用
cvtColor(img,flag)
import cv2 as cv
flags = [i for i in dir(cv) if i.startswith('COLOR_')]
print( flags ) # 查看所有转换模式
在HSV中表示一种颜色比在BGR中更容易
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()
参考文献:
https://docs.opencv.org/3.4.3/df/d9d/tutorial_py_colorspaces.html
https://blog.csdn.net/qq_41905045/article/details/81324671