HSV 是用于颜色检测的良好颜色空间。
这是一个 hsv 颜色图供参考:
x轴表示[0,180]中的色调,y轴1表示[0,255]中的饱和度,y轴2表示S = 255,同时保持V = 255。
要查找颜色,通常只需查找 H 和 S 的范围,并将 v 设置为 range(20, 255)。
例如:
- 检测橙色
我的另一个答案的详细信息:Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)
为了找到橙色,我们查找地图,并找到最佳范围:H :[10, 25], S: [100, 255] 和 V: [20, 255]。所以掩码是 cv2.inRange(hsv,(10, 100, 20), (25, 255, 255) )
#!/usr/bin/python3
# 2018.01.21 20:46:41 CST
import cv2
img = cv2.imread("test.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv,(10, 100, 20), (25, 255, 255) )
cv2.imshow("orange", mask);cv2.waitKey();cv2.destroyAllWindows()
结果:
- 检测绿色/黄色/蓝色
How to define a threshold value to detect only green colour objects in an image :Opencv
- 检测两种不同的颜色
How to detect two different colors using `cv2.inRange` in Python-OpenCV?