# How to detect weather the circle is green or black...

I have currently made an image that consists of black and green dots..... I made a print of it and then clicked it with my camera..... After that i made a program to scan this image in opencv. Here is the image:-

This is the code
        image = imread("ImageTryse.jpg", 1); // Read the image
cv::resize(image, image, Size(800, 800), 0, 0, cv::INTER_CUBIC);
Mat image_gray = IncreaseContrast(image);
cvtColor(image_gray, image_gray, CV_BGR2GRAY);
vector<vec3f> circles1;
HoughCircles(image_gray, circles1, CV_HOUGH_GRADIENT, 2, 10, 100, 22, 10, 17);


I am able to scan circles with this code and run a loop through each of them.... But now i want to know weather the circle is green or black.... I don't know how i can do that.... I tried converting it to hsv but it was showing different colors on the side having light..... Please help me to solve this problem....

edit retag close merge delete

take a look at this

( 2016-07-13 15:32:50 -0500 )edit

Sort by ยป oldest newest most voted

Threshold the image by colour, i.e. black colour is (0,0,0), or close; where as green is (0,255,0), or close.

Using a range you should be able to easily separate these two colours, or any other colours.

Kind regards.

more

You can do a threshold to first get all of the circles from the background.

Find contours and determine which are circles based on shape similarity etc.

Then use each contour to form a mask for each circle.

Then you can use basic averages for each contour to assess it's colour.

The inRange function is another option for fast segmentation by known colour.

• Threshold/InRange
• FindContours
• ForEach contour, create an image mask and draw one contour.
• Get average pixel value for image, with mask, for each contour.
• Compare returned cv::Scalar to see if it is similar to green or black.

Also, only green circles will appear in the green channel of the RGB image. Black is 0 in all channels.

more

I didn't understood this part "Then you can use basic averages for each contour to assess it's colour.". Can you please elaborate? Sorry i am a newbie in image processing

( 2016-07-13 07:55:05 -0500 )edit

Certainly. So long as you understand the mask part, you're all good! So the mask selects part of the image to be analyzed. So, if you use (http://docs.opencv.org/2.4/modules/co...) cv::mean you pass the image and the mask to this function. The function will only operate on the pixels that are included in the mask, which match the contour being analyzed. The function returns a cv::Scalar which is the average colour of the pixels that are in the image, and included in the mask.

( 2016-07-13 07:59:53 -0500 )edit

oh..... SO I HAVE TO JUST COMPARE IT WITH MY SCALAR THEN.... thEN I AM ALL SET

( 2016-07-13 08:39:31 -0500 )edit

Yup. Black should be (0,0,0) and Green should be ~(0,255,0). There are other tricks but that's the essential formula. To get good contours, you should convert to grayscale, and equalizeHist before you run FindContours.

( 2016-07-13 08:42:07 -0500 )edit

FindContour can't detect the shapes properly i am using equalizeHist too. What can i do, please help?

( 2016-07-13 11:02:01 -0500 )edit

Official site

GitHub

Wiki

Documentation