4

I would like to detect a red colored object in a video or image, with OpenCV and C++. What algorithms are available to do this?

I would like to do a comparison of the relationship between levels of color. Indeed, when the brightness varies, the ratio remains constant. So I want to determine the interval of acceptable values ​​for the colors of zone of interest.

For cases I look at the red R (x, y) and G (x, y) / R (x, y) and B (x, y) / R (x, y).

I will then find the ranges of acceptable values​​: to get a first idea, it releases the maximum and minimum for each report from a palette image red

I would like to find something like this :

if minR<=R(x,y)<=maxR and minG<=G(x,y)<=maxG minB<=B(x,y)<=maxB so couleur(x,y)=blanc else couleur(x,y)=NOIR

mevatron
  • 13,911
  • 4
  • 55
  • 72
SOS
  • 79
  • 1
  • 1
  • 5

3 Answers3

27

Preprocess the image using cv::inRange() with the necessary color bounds to isolate red. You may want to transform to a color-space like HSV or YCbCr for more stable color bounds because chrominance and luminance are better separated. You can use cvtColor() for this. Check out my answer here for a good example of using inRange() with createTrackbar().

So, the basic template would be:

Mat redColorOnly;
inRange(src, Scalar(lowBlue, lowGreen, lowRed), Scalar(highBlue, highGreen, highRed), redColorOnly);
detectSquares(redColorOnly);

EDIT : Just use the trackbars to determine the color range you want to isolate, and then use the color intervals you find that work. You don't have to constantly use the trackbars.

EXAMPLE :
So, for a complete example of the template here you go,

I created a simple (and ideal) image in GIMP, shown below: enter image description here

Then I created this program to filter all but the red squares:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat redFilter(const Mat& src)
{
    assert(src.type() == CV_8UC3);

    Mat redOnly;
    inRange(src, Scalar(0, 0, 0), Scalar(0, 0, 255), redOnly);

    return redOnly;
}

int main(int argc, char** argv)
{
    Mat input = imread("colored_squares.png");

    imshow("input", input);
    waitKey();

    Mat redOnly = redFilter(input);

    imshow("redOnly", redOnly);
    waitKey();

    // detect squares after filtering...

    return 0;
}

NOTE : You will not be able to use these exact same filter intervals for your real imagery; I just suggest you tune the intervals with trackbars to see what is acceptable.

The output looks like this:

enter image description here

Voila! Only the red square remains :)

Enjoy :)

Cœur
  • 37,241
  • 25
  • 195
  • 267
mevatron
  • 13,911
  • 4
  • 55
  • 72
  • thank you but i will use this programme in real_time processing so i can't use a trackbar because the idea is to detecte a number in red color in polygone have you any idea who i can do !! – SOS Jan 26 '12 at 17:13
  • 2
    Why not upgrade to OpenCV 2.3.1? What about this example doesn't work for you? `inRange` is available in OpenCV 2.1, so are `Mat` objects... – mevatron Jan 27 '12 at 14:59
  • 2
    Just switch the includes to `#include `, and remove the `` lines. – mevatron Jan 27 '12 at 15:04
  • @mevatron Add this to [OpenCV FAQ](http://stackoverflow.com/tags/opencv/info). ;) – karlphillip Apr 09 '12 at 17:41
  • @karlphillip Done :) Getting a few related questions, eh? :) – mevatron Apr 09 '12 at 18:12
  • @mevatron I think we should add all our interesting answers to the FAQ. Specially those that share source code. I voted for your edit on the tag, and hopefully someone else will agree with me. 2 votes are required to update the tag info. – karlphillip Apr 09 '12 at 18:29
  • @karlphillip Good idea! I'll add them this evening when I get home from work. – mevatron Apr 10 '12 at 15:33
  • can you add some code here how you substract these two images that only the red square is visble at the end, everything else transparent? – lukas Apr 11 '14 at 09:48
0

In that case, try to find out any unique feature for your required square which distinguish it from other squares.

For eg,

1) Color of square:- If color is different from all other squares, you can check inside each square, and select square with required color, as explained by mevatron.

2) Size of square :- If you know size of square, then compare size of each square and select best.

Community
  • 1
  • 1
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • 1
    thanks a lot but i need to detect a color after detecting a square so i use the samples opencv square.c to detect all the square but now i need to detect a red color in one square beetwen some others who can i do ?? – SOS Jan 26 '12 at 16:02
  • I am not sure i understood your question. Better add your image in your question for question to be clear. Anyway, once you know location of square, take each pixel and check if it falls in red region. – Abid Rahman K Jan 26 '12 at 17:59
  • please the idea is how i can located the square between all the square in the image because after this i will do the recognition that's why i need to extract only the square that i need – SOS Jan 27 '12 at 12:54
  • arkiaz i'm a new user for site so i can't add a picture can i give you some dtails with skyp "sossos1926" thank you – SOS Jan 27 '12 at 13:05
-1

You can convert your image from RGB value to HSV type using inbuilt function. After you can find every color has some HSV value range. So you can find that and give that as threshold and differentiate those points from others.

Darshan
  • 151
  • 5
  • 13
  • 2
    Can you provide an example relating to something the OP wants..? It'll really help... – NREZ Sep 05 '13 at 13:16