霍夫圆检测
函数原型
public static CircleSegment[] HoughCircles(InputArray image, HoughMethods method, double dp, double minDist, double param1 = 100, double param2 = 100, int minRadius = 0, int maxRadius = 0);
返回值:CircleSegment[],包含圆心,半径
image:输入图像,单通道,灰度图像
method:霍夫变换方法,HoughCirclesMethod.Gradient
dp:用来检测圆心的累加器图像的分辨率与图像之比的倒数,且此参数允许创建一个比输入图像分辨率较低的累加器,如,dp=1累加器和输入图像具有相同的分辨率,dp=2累加器只有输入图像一半大的宽度和高度
minDist:霍夫变换检测到圆的圆心之间的最小距离,用来分辨不同的圆
param1:第一个方法特定的参数。[默认值是100],边缘检测的低阈值
param2 :第二个方法特定于参数。 [默认值是100],中心点累加器阈值,候选圆心
minRadius :最小半径
maxRadius :最大半径
以下代码,有的是无用的
private static void HoughCircular()
{
Mat src = new Mat(@"I:\OpenCvSharp学习\气泡.jpg", ImreadModes.AnyColor);
Mat Gray = new Mat();
Mat Canny = new Mat();
Mat Threthold = new Mat(src.Size(), MatType.CV_8UC3, Scalar.White);
Mat Result = new Mat(src.Size(), MatType.CV_8UC3, Scalar.White);
//灰度化
Cv2.CvtColor(src, Gray, ColorConversionCodes.RGB2GRAY);
//二值化
Cv2.Threshold(Gray, Threthold, 100, 255, ThresholdTypes.Binary);
Cv2.Canny(Threthold, Canny, 60, 200, 3, false);
CircleSegment[] circle;
circle = Cv2.HoughCircles(Gray, HoughMethods.Gradient, 1, 30, 100, 30, 1, 100);
Scalar Colar = new Scalar(0, 255, 0);
Scalar Colar1 = new Scalar(0,0,255);
for (int i = 0; i < circle.Length; i++)
{
int X = (int)circle[i].Center.X;
int Y = (int)circle[i].Center.Y;
int a = (int) circle[i].Radius;
Cv2.Circle(src,X,Y,a,Colar,2,LineTypes.Link8);//圆
Cv2.Circle(src, X, Y, 2, Colar1, 2, LineTypes.Link8);//圆心
}
Window Win1 = new Window("src", WindowMode.AutoSize, src);
Window Win2 = new Window("Gray", WindowMode.AutoSize, Gray);
Window Win4 = new Window("Canny", WindowMode.AutoSize, Canny);
Window Win5 = new Window("Result", WindowMode.AutoSize, src);
Cv2.WaitKey(0);
}