How to set HSV color range in OpenCV?

人走茶凉 提交于 2019-12-04 05:16:08

问题


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)

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:


回答1:


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)

Also make sure that hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) as OpenCV follows the BGR convention.

EDIT:

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)


来源:https://stackoverflow.com/questions/42825020/how-to-set-hsv-color-range-in-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!