C++ 在opencv中检测黄色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9179189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detect yellow color in opencv
提问by Mohamed Kamal
i convert image to HSV after it i make the threshold the yellow color so the code is cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThreshed); but it doesn't work always give me black image .
在我将阈值设为黄色后,我将图像转换为 HSV,因此代码为 cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThresed); 但它并不总是给我黑色图像。
回答by Abid Rahman K
You should try this tutorial for "tracking yellow objects".
您应该尝试本教程“跟踪黄色对象”。
It gives a HSV range of cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed)
for yellow object.
它给出了cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed)
黄色物体的 HSV 范围。
If you have any doubt on selecting color, try this : http://www.yafla.com/yaflaColor/ColorRGBHSL.aspx
如果您对选择颜色有任何疑问,请试试这个:http: //www.yafla.com/yaflaColor/ColorRGBHSL.aspx
回答by andrea
you can also convert RGB into HUE
您还可以将 RGB 转换为 HUE
http://en.wikipedia.org/wiki/Hue
http://en.wikipedia.org/wiki/Hue
in the link you have the formula, then you know that yellow has a HUE value around 60.
在您有公式的链接中,您知道黄色的色调值约为 60。
回答by Ayush joshi
for yellow color the range as should be from 23 to 40 for example as per i am using in my yellow object tracking program
对于黄色,范围应该从 23 到 40,例如按照我在我的黄色对象跟踪程序中使用的
//Thresholding the frame for yellow
//阈值黄色的帧
cvInRangeS(hsvframe,cvScalar(23,41,133),cvScalar(40,150,255),threshy);
回答by Lucario
I know question you ask is for c++, I have python script to detect yellow color which may help you or somebody else..
我知道你问的问题是针对 C++ 的,我有 python 脚本来检测黄色,这可能对你或其他人有帮助。
def colorDetection(image):
hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
'''Red'''
# Range for lower red
red_lower = np.array([0,120,70])
red_upper = np.array([10,255,255])
mask_red1 = cv2.inRange(hsv, red_lower, red_upper)
# Range for upper range
red_lower = np.array([170,120,70])
red_upper = np.array([180,255,255])
mask_red2 = cv2.inRange(hsv, red_lower, red_upper)
mask_red = mask_red1 + mask_red2
red_output = cv2.bitwise_and(image, image, mask=mask_red)
red_ratio=(cv2.countNonZero(mask_red))/(image.size/3)
print("Red in image", np.round(red_ratio*100, 2))
'''yellow'''
# Range for upper range
yellow_lower = np.array([20, 100, 100])
yellow_upper = np.array([30, 255, 255])
mask_yellow = cv2.inRange(hsv, yellow_lower, yellow_upper)
yellow_output = cv2.bitwise_and(image, image, mask=mask_yellow)
yellow_ratio =(cv2.countNonZero(mask_yellow))/(image.size/3)
print("Yellow in image", np.round(yellow_ratio*100, 2))
回答by aardvarkk
I think your hue values may be incorrect. I'm not sure where you're getting the 112-124 hue range from if you're trying to detect yellow. I would expect the values to be closer to 40, so maybe try a range like 34-46.
我认为您的色调值可能不正确。如果您尝试检测黄色,我不确定您从哪里获得 112-124 色调范围。我希望这些值更接近 40,因此可以尝试使用 34-46 之类的范围。