如何在OpenCV中设置HSV颜色范围? [英] How to set HSV color range in OpenCV?
问题描述
我有一个手机,它像打击一样是HSV直方图,我想跟踪它的移动.基于它的直方图,我设置如下图像范围:
I have a phone and it's HSV histogram like blow,and I want to track this phone's movement.Based on it's histogram,I set image range like this:
greenLower = (300, 0, 50)
greenUpper = (50, 128,250 )
cv2.inRange(hsv, greenLower, greenUpper)
但是挥舞电话时什么也没发现,我很确定这是因为颜色范围错误,您能告诉我如何正确设置颜色范围吗?尤其是当HUE值在[300〜50]之间时,由于HUE是线圈,我应该将其设置为(50〜300)还是(300〜50).
But nothing got detected out when waving the phone,and I am pretty sure it is because color range is wrong,would you tell me how to get color rang setting right?Especially,when HUE values are between [300~50],should I set it to (50~300) or (300~50) due to HUE is a cirle.
Phone
HSV histogram:
推荐答案
您错误地设置了上限和下限,它们必须是:
You have wrongly set the upper and lower bounds, they must be:
greenLower = (50, 0, 50) # Previously (300, 0, 50)
greenUpper = (300, 128, 250) # Previously (50, 128,250)
还要确保hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
作为OpenCV遵循BGR
约定.
Also make sure that hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
as OpenCV follows the BGR
convention.
要在多个范围0~50
和300~359
中对颜色进行分段,可以对两个范围执行两次cv2.inRange()
,如下所示:
To segment colors in multiple ranges 0~50
and 300~359
, you can perform cv2.inRange()
twice for two ranges as:
greenLower1 = (0, 0, 20)
greenUpper1 = (50, 128, 100)
greenLower2 = (300, 0, 20)
greenUpper2 = (359, 128, 100)
mask1 = cv2.inRange(img_hsv, greenLower1, greenUpper1)
mask2 = cv2.inRange(img_hsv, greenLower2, greenUpper2)
mask = cv2.max(mask1, mask2)
这篇关于如何在OpenCV中设置HSV颜色范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!