3

I have two related questions:

  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:

    Target HSV value: [15, 37, 51]

    Background HSV value: [90, 21, 211]

  2. 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?

My other car is a cadr
  • 1,429
  • 1
  • 13
  • 21
  • 2
    you could try to run otsu thresholding on the V channel, then use the return value to get the bounds for V with inRange – berak Oct 31 '14 at 09:19

1 Answers1

2

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. Calculate median (M) of pixel values in image. Some people use mean instead but I prefer median for its robustness.

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

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).

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.

Michael Burdinov
  • 4,348
  • 1
  • 17
  • 28
  • `(M - k * MAD)` did you mean to separate `k * MAD` to `(m - k) * MAD`? This easily produces negative numbers more often than positive ones form my experiance (so the lower bound almost always hits 0). – Douglas Gaskell Jun 21 '17 at 00:28
  • @DouglasGaskell, no it is not k*MAD. There nothing bad if lower bound is 0, as long as it makes sense for current distribution of gray levels (and very often it is). – Michael Burdinov Jun 21 '17 at 05:57