如何在OpenCV中为InRange阈值选择最佳HSV值 [英] How to chose the optimal HSV values for InRange thresholding in OpenCV

查看:1453
本文介绍了如何在OpenCV中为InRange阈值选择最佳HSV值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相关的问题:


  1. 我正在使用 inRange 阈值功能。如果我手动选择较低和较高的HSV界限,我会得到很好的结果,但这对照明条件非常敏感。是否有算法可以确定给定目标和背景的HSV值的最佳阈值界限?

  1. I'm doing color object detection in OpenCV with the inRange threshold function. I'm getting excellent results if I manually choose the lower and upper HSV bounds, but this is very sensitive to the lighting conditions. Is there an algorithm that can determine the optimal threshold bounds given the HSV values of the target and the background?

例如,以下是我正在使用的值:

As an example, here are the values I'm working with:

目标HSV值:[15 ,37,51]

Target HSV value: [15, 37, 51]

背景HSV值:[90,21,211]

Background HSV value: [90, 21, 211]

inRange 是简单的逐像素过滤器。它不会动态更改阈值条件。是否有自适应阈值或Otsu阈值的颜色?或者可以利用目标和背景的先验HSV值的算法?

inRange is simple pixel-by-pixel filter. It doesn't change the threshold conditions dynamically. Is there a color equivalent of adaptive threshold or Otsu threshold? Or an algorithm that can make use of the a priori HSV values of the target and the background?


推荐答案

如果大部分图像大致相同(适用于示例大部分是背景),而不是你可以执行以下操作:

If most of the image is more or less the same (for example most of it is a background) than you can do the following:


  1. 计算图像中像素值的中位数(M) 。有些人使用均值来代替,但我更倾向于使用中位数。

  1. Calculate median (M) of pixel values in image. Some people use mean instead but I prefer median for its robustness.

计算图像中像素值的中位数绝对偏差(MAD)。再次,我更喜欢它超过标准偏差的稳健性。

Calculate median absolute deviation (MAD) of pixel values in image. Again I prefer it over standard deviation for its robustness.

所有范围都在(M - k *之间) MAD)和(M + k * MAD)将是背景(或对象)。根据你的应用选择常数'k'的值(我猜它会在1到5之间)。

Everything that will fall in range between (M - k * MAD) and (M + k * MAD) will be the background (or object). Choose value of constant 'k' according to your application (I guess it will be somewhere between 1 and 5).

小提示,如果这是你第一次使用MAD :正常分布情况下,1标准偏差约等于1.5 MAD。

Little tip, if this is your first time using MAD: 1 standard deviation is approximately equal to 1.5 MAD in case of normal distribution.

这篇关于如何在OpenCV中为InRange阈值选择最佳HSV值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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