2

I'm using OpenCV on Android to find circles of specific colour's in real time. My first step is to keep only pixels which corresponds to my defined color i'm looking for (red or green in this example). Example Image.

For this purpose i'm using the method inRange().

Here is my Question: What kind of color model (RGB, BGR, HSV, ..) is required as lower-/upper-bound color parameter's? And: what is a good practice to define these color bounds in respect to natural brightness changes?

matRgba = inputFrame.rgba();

Scalar lowerColorBound = Scalar(0.0, 0.0, 0.0); // Blue, Green, Red?
Scalar upperColorBound = Scalar(0.0, 0.0, 0.0);

// convert to HSV, necessary to use inRange()
Imgproc.cvtColor(matRgba, matRgba, Imgproc.COLOR_RGB2HSV);

// keep only the pixels defined by lower and upper bound range
Core.inRange(matRgba, lowerColorBound, upperColorBound, matRgba);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mocle
  • 33
  • 5
  • The example https://docs.opencv.org/trunk/d2/dc1/camshiftdemo_8cpp-example.html#a32 suggests it should be HSV, which seems logical as the image is HSV. – Nick Feb 18 '18 at 19:07
  • I should have added that I am not having any more success assuming it is HSV than I was when I was assuming it is RGB! – Nick Feb 18 '18 at 19:23

1 Answers1

3

The required color model for the inRange(src, lowerb, upperb, dst) function in OpenCV is HSV.

The lowerb and upperb parameters specify the required lower and upper color bounds in the HSV format. In OpenCV, for HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].

For object tracking applications a possible practice (as suggested in the official documentation) to define these two color bounds can be:

  1. Start from a color to track in RGB format.
  2. Convert the color to the HSV format. Let (H, S, V) be its value.
  3. Assign the value (H - deltaH, minS, minV) to lowerb and the value (H - deltaH, maxS, maxV) to upperb.

Possible starting values for the parameters defined in step 3 can be:

  • deltaH = 10
  • minS = 100, minV = 100
  • maxS = 255, maxV = 255

Then you can adjust them to narrow down or enlarge the H, S, V intervals as needed.

tony
  • 431
  • 4
  • 12