2

I'm using OpenCV to detect areas of red color from images with different backgrounds and light conditions:

A) Ideal conditions:

enter image description here

B) Similar color background:

enter image description here

C) Low light:

enter image description here

I've mainly tried using inRange. First in the BGR color space, by extracting pixels that had a minimum red value of around 200. That worked well in A and B but not in C (low light). I tried converting the image to HSV and doing an inRange for reds (borrowing from here):

Mat imgThresholded;
Mat imageHSV;
cvtColor(src, imageHSV, CV_BGR2HSV);

int iLowH = 0;
int iHighH = 50;

int iLowS = 100;
int iHighS = 255;

int iLowV = 80;
int iHighV = 255;

inRange(imageHSV,
        Scalar(iLowH, iLowS, iLowV),
        Scalar(iHighH, iHighS, iHighV),
        imgThresholded);

return imgThresholded;

Which works well with A and C, but not with B.

Which approach would be best?

Thanks!

Community
  • 1
  • 1
Eric
  • 16,003
  • 15
  • 87
  • 139

3 Answers3

3

RGB is almost always the wrong colour space for machine vision. HSV is a good approach - you might need to adapt the range based on overall brightness because of the camera's resposne

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • Thanks @Martin. The trouble with HSV is when the background is of a very similar colour (see image **B** in the question). In that scenario, would adapting the range based on overall brightness help at all? – Eric Sep 21 '14 at 18:54
  • Yes that's the idea of hsv, if the hue is the same you can use brightness to distinguish them. In this case you can also use histogram to decide that common HSV pixels are background and rarer ones are object – Martin Beckett Sep 21 '14 at 21:43
1

I think that the best approach is to train a classifier (neural network, svm, ... ), to classify object/background using color components values as features. It should work with 3D points (R,G,B - values will be coordinates). For start point I recommend standard opencv's example points_classifier.cpp (located in examples/cpp folder).

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • 1
    Thanks @Andrey. Sure, a classifier sounds like a robust approach, but I'm concerned that the amount of time required to understand how to use it and then train it will be worth it, in relation to other solutions. Also, can you please recommend other code examples? I just spent two hours trying to get `points_classifier.cpp` to compile. Turns out it uses a version of `ml.hpp` which is not in the current OpenCV 2.4.9 framework binary. – Eric Sep 21 '14 at 19:11
1

IMHO, you should add more reasoning other than color. Color is the very basic (but it can be used as a first step) and limited type of object recognition tool, because there is no guarantee that nothing in your background would have similar color. For example, adding image segmentation link, or shape detection link

Community
  • 1
  • 1
Samer
  • 1,923
  • 3
  • 34
  • 54
  • 1
    Thanks @Samer. So which additional tool(s) would you recommend using, which would be adequate for all scenarios? I've tried using shape detection using [OpenCV's Canny algorithm](http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/canny_detector/canny_detector.html), but it doesn't work very well when light conditions are low (see image **B** in the question). – Eric Sep 21 '14 at 19:16
  • @Eric, Have you considered [Template Matching](http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html)? – Samer Sep 21 '14 at 21:37