C#-OpenCvSharp-Hough transform detection circle (with source code)

 

前言:

Cv2.HoughCirclesIs the method used in OpenCvSharp to detect circles in images. It is based on the Hough Transform to identify circles in images.

Core functions:

Cv2.HoughCircles ()

CirclesSegment[] HoughCircles(
    InputArray image,         // 输入图像,应该是单通道灰度图像
    HoughMethods method,      // 霍夫变换的方法,通常使用 HoughMethods.Gradient
    double dp,                // 累加器分辨率与图像分辨率的倒数之比,一般设置为 1
    double minDist,           // 检测到的圆之间的最小距离
    double param1 = 100,      // Canny 边缘检测的第一个阈值
    double param2 = 100,      // 圆心检测阈值,通常设置为较小值以减少错误检测
    int minRadius = 0,        // 最小圆半径
    int maxRadius = 0         // 最大圆半径
);
  1. InputArray image: Input image to detect circles. This should usually be a single channel grayscale image.

  2. HoughMethods method: Hough transform method. Usually HoughMethods.Gradient is used, which is a gradient-based method and suitable for most cases.

  3. double dp: The ratio of the accumulator resolution to the reciprocal of the image resolution. Typically set to 1, indicating the same resolution as the input image.

  4. double minDist: Minimum distance between detected circles. This parameter can be used to control whether to merge close circles. Typically, you can adjust this value based on your application.

  5. double param1: The first threshold of Canny edge detection. Edge detection is a part of Hough transform and is used to detect edges in images. Typically set to 100.

  6. double param2: Circle center detection threshold. This threshold is used to determine whether a detected circle is a true circle. Usually set to a smaller value to reduce false detections.

  7. int minRadius and int maxRadius: Minimum and maximum radius of the circle to be detected. If you know the radius of the circle you want to detect, you can set these two values. If you're not sure, you can set them both to 0 and the method will automatically find circles of any size.

return value

Cv2.HoughCirclesThe method returns an array of CirclesSegment where each element represents a detected circle. Each CirclesSegment contains the following information:

  • Center: Circle center coordinates.
  • Radius: The radius of the circle.

code
 

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 霍夫变换检测圆
{
    class Program
    {
        static void Main(string[] args)
        {
            // 读取图像
            Mat image = Cv2.ImRead("C:/Users/CGW/Desktop/digits/气泡.jpg", ImreadModes.Color);
            Cv2.ImShow("原图", image);
            // 转换为灰度图像
            Mat gray = new Mat();
            Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);

            // 使用 HoughCircles 方法检测圆
            CircleSegment[] circles = Cv2.HoughCircles(
                gray,
                HoughModes.Gradient, // 使用梯度法进行霍夫圆变换
                dp: 1, // 分辨率因子
                minDist: 30, // 圆之间的最小距离
                param1: 100, // Canny 边缘检测的高阈值
                param2: 30, // 圆心累加器的阈值
                minRadius: 5, // 最小半径
                maxRadius: 50 // 最大半径(如果为0,则根据图像尺寸自动调整)
            );

            // 绘制检测到的圆
            // 如果找到了圆,绘制它们
            for (int i = 0; i < circles.Length; i++)
            {
                Point2f center = circles[i].Center;
                Cv2.Circle(image, (int)center.X, (int)center.Y, 1, new Scalar(0, 100, 100), 3, LineTypes.AntiAlias);
                // circle outline                    
                Cv2.Circle(image, (int)center.X, (int)center.Y, (int)circles[i].Radius, new Scalar(0, 255, 0), 3, LineTypes.AntiAlias);
            }

            // 显示结果
         
            Cv2.ImShow("Detected Circles", image);
            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();
        }
    }
}

result:

Guess you like

Origin blog.csdn.net/m0_55074196/article/details/133745562