霍夫圆检测原理及OpenCV 找圆API使用(C#)

原理:
直角坐标系中圆可以表示为:
在这里插入图片描述
在这里插入图片描述
对应参数方程:
在这里插入图片描述
对于确定的某点像素坐标(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

.NET 环境的OpenCv库

猜你喜欢

转载自blog.csdn.net/weixin_40671962/article/details/127311154