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天全站免登陆