Ask Your Question

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

asked 2016-07-13 05:10:18 -0500

hudixt gravatar image

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:- image description

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 flag offensive close merge delete


take a look at this

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

2 answers

Sort by ยป oldest newest most voted

answered 2016-07-13 07:40:50 -0500

MRDaniel gravatar image

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.

edit flag offensive delete link 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

hudixt gravatar imagehudixt ( 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 ( 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.

MRDaniel gravatar imageMRDaniel ( 2016-07-13 07:59:53 -0500 )edit


hudixt gravatar imagehudixt ( 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.

MRDaniel gravatar imageMRDaniel ( 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?

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

answered 2016-07-14 09:53:56 -0500

elias gravatar image

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-07-13 05:10:18 -0500

Seen: 394 times

Last updated: Jul 14 '16