Mike Webb
Mike Webb

Reputation: 9003

In Emgu.CV what do these threshold values mean and is there a better way to detect a circle?

Background

Here is my Emgu.CV code for getting an image and drawing the circles found in it (mostly code from the ShapeDetection project in Emgu.CV.Examples solution that came with the EmguCV download):

//Load the image from file
Image<Bgr, Byte> img = new Image<Bgr, byte>(myImageFile);

//Get and sharpen gray image (don't remember where I found this code; prob here on SO)
Image<Gray, Byte> graySoft = img.Convert<Gray, Byte>().PyrDown().PyrUp();
Image<Gray, Byte> gray = graySoft.SmoothGaussian(3);
gray = gray.AddWeighted(graySoft, 1.5, -0.5, 0);

Image<Gray, Byte> bin = gray.ThresholdBinary(new Gray(149), new Gray(255));

Gray cannyThreshold = new Gray(149);
Gray cannyThresholdLinking = new Gray(149);
Gray circleAccumulatorThreshold = new Gray(1000);

Image<Gray, Byte> cannyEdges = bin.Canny(cannyThreshold, cannyThresholdLinking);

//Circles
CircleF[] circles = cannyEdges.HoughCircles(
    cannyThreshold,
    circleAccumulatorThreshold,
    4.0, //Resolution of the accumulator used to detect centers of the circles
    15.0, //min distance 
    5, //min radius
    0 //max radius
    )[0]; //Get the circles from the first channel

//draw circles (on original image)
foreach (CircleF circle in circles)
    img.Draw(circle, new Bgr(Color.Brown), 2);

Here is the image:

Image of circles

The Questions

  1. OK, so I know what the threshold in ThresholdBinary is. Since I am getting the binary image from the gray-scale image it is the intensity of the gray in the picture. This works as the intensity of the gray-scale circle in the pic is 150 to 185. I assume this is the same for the first argument to HoughCircles.

    What I don't know is what circleAccumulatorThreshold, Resolution of the accumulator, and min distance (2nd, 3rd, and 4th args to HoughCircles) are or what values should go there. I obviously do not have the correct values because the circle in the pic is not 'houghed' correctly.

  2. My second question is, is there a better way to find the circle? I need to be able to detect this circle in many types of light (i.e. the circle color intensity may be low, like 80 or lower) and to get its dimensions in the pic. What is the best way to match a circle? Should I make the circle another color and look in the original image for that color? Any other ideas?

Thanks

Upvotes: 1

Views: 18143

Answers (2)

john k
john k

Reputation: 6615

  • accumulator is how many points must 'accumulate' to be considered a circle. Higher numbers mean less circles detected.
  • Resolution is how close the point has to be to the proposed circle. Basically the 'size' of the pixel.
  • MinDistance is how close the circles are allowed to be to each other. In your example you've got 3 circles all very close to each other. Increasing minimum distance would prevent the overlapping circles and instead just draw one.

As for your answer to number two, blur the image, convert to greyscale, then threshold to eliminate lighting differences is the usual solution

Upvotes: 3

Oliver
Oliver

Reputation: 696

While this question is a "lot" old, I'd like to propose an answer to question #2 for the benefit of those who might come across similar problem.

What you can do is:

  1. Threshold the image to remove the background,
  2. Detect objects in the image,
  3. Calculate the circularity of the circle (http://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)), it should be 1 if it is a circle. using the method FindContours (in emgucv) provides all information that you need to calculate for the area and the perimeter of a circle. You can then use these information to get the dimension of your detected circle.

Upvotes: 1

Related Questions