Reputation: 83
I'm trying to detect the circles in this image using EmguCV 2.2 with C#, but not having any luck.
Using OpenCV with the cv2 python package the following code correctly finds the 8 circles in the above image:
img = cv2.imread('test2.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 10, param1=15, param2=10, minRadius=5, maxRadius=5)
For brevity I'll omit the code to draw the circles onto img, but for reference the output - assuming I use cv2.circle to fill each found circle with green, looks like:
However I can't seem to find those same circles using C#.
I've played around with the parameters quite, but trying code such as the following doesn't find any circles in the image:
var gray = new Image<Gray, byte>("test2.png");
var circles = gray.HoughCircles(
accumulatorThreshold: new Gray(16), dp: 1,
cannyThreshold: new Gray(9),
minDist: 10, minRadius: 4, maxRadius: 6)[0];
Any help finding those 8 circles with C# will be greatly appreciated!!
Thanks in advance for your help!
Upvotes: 3
Views: 8107
Reputation: 1845
I used the following code for finding Hough Circles
Image<Bgr, byte> Img_Result_Bgr = new Image<Bgr, byte>(Img_Source_Gray.Width, Img_Source_Gray.Height);
CvInvoke.cvCvtColor(Img_Source_Gray.Ptr, Img_Result_Bgr.Ptr, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_GRAY2BGR);
Gray cannyThreshold = new Gray(12);
Gray circleAccumulatorThreshold = new Gray(26);
double Resolution = 1.90;
double MinDistance = 10.0;
int MinRadius = 0;
int MaxRadius = 0;
CircleF[] HoughCircles = Img_Source_Gray.Clone().HoughCircles(
cannyThreshold,
circleAccumulatorThreshold,
Resolution, //Resolution of the accumulator used to detect centers of the circles
MinDistance, //min distance
MinRadius, //min radius
MaxRadius //max radius
)[0]; //Get the circles from the first channel
#region draw circles
foreach (CircleF circle in HoughCircles)
Img_Result_Bgr.Draw(circle, new Bgr(Color.Red), 2);
#endregion
imageBox1.Image = Img_Result_Bgr;
Here is the Program Output.
Also Since these Circles are separate i would prefer to use Minimum Enclosing Circle method to find these Circle Co-Ordinates. Refer this link.
To Easily Find the Co-Ordinates of these Circles:
Upvotes: 5