原理:
直角坐标系中圆可以表示为:
对应参数方程:
对于确定的某点像素坐标(x,y),选取(a,b,r) 的不同组合,可以得到经过这一像素的(x,y)的所有圆,在a、b、r坐标系中绘制图像:
API:
public static CircleSegment[] HoughCircles(InputArray image, HoughModes method, double dp, double minDist, double param1 = 100, double param2 = 100, int minRadius = 0, int maxRadius = 0);
使用:
if (fileDialog.ShowDialog() == DialogResult.OK)
{
picFile = fileDialog.FileName;
inputMat = Cv2.ImRead(picFile, ImreadModes.Grayscale);
Mat finallyOutMate = Cv2.ImRead(picFile, ImreadModes.AnyColor);
var circles = Cv2.HoughCircles(inputMat, HoughModes.Gradient, 1, 10, 100, 100, 40, 800);
//绘制圆
Scalar scalar = new Scalar(255, 0, 0);
foreach (var m in circles)
{
Cv2.Circle(finallyOutMate, (int)m.Center.X, (int)m.Center.Y, (int)m.Radius, scalar,4);
}
picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(finallyOutMate);
}
补充说明:
本案例在.NET使用的OpenCV库为OpenCvSharp4