Reputation: 11
I want to detect circle center & radius. ( C#, OpenCV2 )
I tried to find it using Canny Edge, Laplace Edge, BinarizerMethod.
I found wrong position and radius.
How can I improve it?
a_circle = new IplImage(src.Size, BitDepth.U8, 3);
Cv.Copy(src, a_circle);
a_gray = new IplImage(src.Size, BitDepth.U8, 1);
Cv.CvtColor(src, a_gray, ColorConversion.BgrToGray);
Cv.Smooth(a_gray, a_gray, SmoothType.Gaussian, 9);
CvMemStorage a_Storage = new CvMemStorage();
CvSeq<CvCircleSegment> circles = Cv.HoughCircles(a_gray, a_Storage, HoughCirclesMethod.Gradient, 1, 100, a_p_edge, a_p_center, 0, 0);
foreach (CvCircleSegment item in circles)
{
Cv.DrawCircle(a_gray, item.Center, (int)item.Radius, CvColor.White, 3);
point_radius = (int)item.Radius;
point_centerX = (int)item.Center.X;
point_centerY = (int)item.Center.Y;
}
I tried to find it using Canny Edge, Laplace Edge, BinarizerMethod.
How can I find the circle center & radius?
Upvotes: 0
Views: 87
Reputation: 51
The circle can be detected with Hough Circles, but you probably still need to tweak the parameters (dp, param1, param2 etc.) for your image.
See documentation: https://docs.opencv.org/3.4/d4/d70/tutorial_hough_circle.html
using OpenCvSharp;
using Mat originalImageColor = Cv2.ImRead("C:\\circle.png", ImreadModes.Color);
using Mat workerImage = Cv2.ImRead("C:\\circle.png", ImreadModes.Grayscale);
float dp = 0.1f;
int minDistance = 200;
int param1 = 60;
int param2 = 40;
int minRadius = 350;
int maxRadius = 500;
CircleSegment[] results = Cv2.HoughCircles(workerImage, HoughModes.Gradient, dp, minDistance, param1, param2, minRadius, maxRadius);
foreach(var result in results)
{
Cv2.Circle(originalImageColor, (int)result.Center.X, (int)result.Center.Y, (int)result.Radius, Scalar.Magenta, thickness: 3);
}
Cv2.ImShow("output", originalImageColor);
Cv2.WaitKey();
Upvotes: 1