OpenCV:选择用于颜色过滤的HSV阈值 [英] OpenCV: Choosing HSV thresholds for color filtering

查看:96
本文介绍了OpenCV:选择用于颜色过滤的HSV阈值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了从图像中滤除颜色,必须设置需要检测哪种颜色的边界.我觉得这主要是一个反复试验的过程.有什么方法可以快速找到特定颜色的正确阈值?在这种特定情况下,我尝试检测下图中的图形的灰色区域.当然,这没有检测到虚线.对于此示例,我需要非常具体的界限.问题是,如何才能轻松找到它们?

In order to filter a color out of an image, it's necessary to set boundaries as to which color needs to be detected. I have a feeling this is mostly a trial-and-error process. Are there any ways to quickly find the correct thresholds for a particular color? In this specific case I'm trying to detect the gray area of the graph in the picture below. This without detecting the dotted lines of course. For this example I need very specific boundaries. The question is, how can I find them easily?

hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)

lower = np.array([0, 0, 0], np.uint8)
upper = np.array([180, 255, 200], np.uint8)

mask = cv2.inRange(hsv, lower, upper)

推荐答案

另一种选择是使用在线图像颜色选择器.您可以上传图片,并获得一些值,例如 HSV:97.5°5.1%61.57%.注意,您需要将它们转换为H,S和V的OpenCV比例尺.

Another option is to use online image color picker. You can upload your image and will get some values like HSV: 97.5° 5.1% 61.57% in your case. Note, you need to convert them to OpenCV scales of H, S and V.

H,OpenCV中的色相在0到180之间变化,但是在外部世界中,色度通常以0到360度为单位进行测量,因此要获得颜色的H值 h = 97.5°/2 = 48.7

H, hue in OpenCV varies from 0 to 180, but in the outer world it is usually measured in degrees from 0 to 360, so to get the H of your color h = 97.5° / 2 = 48.7

S和V的测量范围是 0(在外部世界= 0%) 255(在外部世界= 100%),所以

S and V are measured from 0 ( = 0% in outer world) to 255 ( = 100% in outer world), so

s = 255 * 5.1% = 13
v = 255 * 61.57% = 157

因此,目标HSV颜色为(49,13,157).我建议使用±10作为范围.或更严格些.我认为,对于您的情况,只选择中心图的像素,不带任何标签,然后根据需要应用形态学运算就可以了.

So, the target HSV color is (49, 13, 157). I suggest using ±10 for the range. Or being even more strict. I think it might be OK for your case to select only the pixels of the central graph, without any labels, and then apply morphological operation close, if needed.

这篇关于OpenCV:选择用于颜色过滤的HSV阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆