I need help to detect circles in my images , When I run my code the circle is not searched instead the returned shape is an ellipse , sorry bad English
Input image:
Result image:
My code :
public Mat Run(string imgPath , int thresholdValue = 150)
{
var img=Cv2.ImRead(imgPath);
Bitmap bitmap = MatToBitmap(img);
pictureBox1.Image = bitmap;
Mat gray = img.CvtColor(ColorConversionCodes.BGR2GRAY);
ThresholdImg = gray.Threshold(thresholdValue, 255, ThresholdTypes.Binary);
Bitmap threshold = MatToBitmap(ThresholdImg);
pictureBox2.Image = threshold;
//Cv2.ImShow("Threshold", ThresholdImg);
Mat gaussImg = ThresholdImg.GaussianBlur(new OpenCvSharp.Size(5, 5), 0.8);
//Cv2.ImShow("GaussianBlur", gaussImg);
Mat medianImg = ThresholdImg.MedianBlur(5);
//Cv2.ImShow("MedianBlur", medianImg);
Mat kernel = new Mat(15, 15, MatType.CV_8UC1);
Mat DilateImg = ThresholdImg.Dilate(kernel);
Mat binary = DilateImg.Erode(kernel);
//Cv2.ImShow("Dilate & Erode", binary);
Mat element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new OpenCvSharp.Size(5, 5));
Mat openImg = ThresholdImg.MorphologyEx(MorphTypes.Open, element);
//Cv2.ImShow("Dilate & Erode", openImg);
Rect roi = new Rect(0, 0, bitmap.Width, bitmap.Height);
Mat ROIimg = new Mat(openImg, roi);
Cv2.ImShow("ROI Image", ROIimg);
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierachy;
Cv2.FindContours(ROIimg, out contours, out hierachy, RetrievalModes.List, ContourApproximationModes.ApproxTC89KCOS);
//According to the found contour points, fit the ellipse
for (int i = 0; i < contours.Length; i++)
{
//The fitting function must have at least 5 points, if less than it is not fitting
if (contours[i].Length < 5) continue;
//Ellipse fitting
var rrt = Cv2.FitEllipse(contours[i]);
}
}
How to accurately detect a circle? , sorry i am new , i cant post my image
I found the solution to the problem , i used Erode and Dilate to separate the circle from the shape
Mat binary = DilateImg.Dilate(kernel, null, 2);
Mat DilateImg = ThresholdImg.Erode(kernel, null, 2);
And My result image