EmguCV和OpenCV中HoughCircles的区别是什么?

3 投票
1 回答
7925 浏览
提问于 2025-04-18 10:16

我正在尝试使用EmguCV 2.2和C#来检测这张图片中的圆圈,但一直没有成功。

在这里输入图片描述

使用OpenCV和cv2这个Python包,下面的代码可以正确找到上面图片中的8个圆圈:

img = cv2.imread('test2.png')   
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 10, param1=15, param2=10, minRadius=5, maxRadius=5)

为了简洁起见,我就不写绘制圆圈的代码了,但如果用cv2.circle把每个找到的圆圈填充成绿色,输出的效果是这样的:

在这里输入图片描述

但是我在C#中似乎找不到那些相同的圆圈。

我尝试了很多参数,但像下面这样的代码却没有找到图片中的任何圆圈:

var gray = new Image<Gray, byte>("test2.png");
var circles = gray.HoughCircles(
                accumulatorThreshold: new Gray(16), dp: 1,
                cannyThreshold: new Gray(9),
                minDist: 10, minRadius: 4, maxRadius: 6)[0];

如果能帮我用C#找到这8个圆圈,我将非常感激!!

提前谢谢你的帮助!

1 个回答

5

我用以下代码来寻找霍夫圆。

Image<Bgr, byte> Img_Result_Bgr = new Image<Bgr, byte>(Img_Source_Gray.Width, Img_Source_Gray.Height);
CvInvoke.cvCvtColor(Img_Source_Gray.Ptr, Img_Result_Bgr.Ptr, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_GRAY2BGR);
Gray cannyThreshold = new Gray(12);
Gray circleAccumulatorThreshold = new Gray(26);
double Resolution = 1.90;
double MinDistance = 10.0;
int MinRadius = 0;
int MaxRadius = 0;

CircleF[] HoughCircles = Img_Source_Gray.Clone().HoughCircles(
                        cannyThreshold,
                        circleAccumulatorThreshold,
                        Resolution, //Resolution of the accumulator used to detect centers of the circles
                        MinDistance, //min distance 
                        MinRadius, //min radius
                        MaxRadius //max radius
                        )[0]; //Get the circles from the first channel
#region draw circles
foreach (CircleF circle in HoughCircles)
    Img_Result_Bgr.Draw(circle, new Bgr(Color.Red), 2);
#endregion

imageBox1.Image = Img_Result_Bgr;

这是程序的 输出结果

因为这些圆是分开的,所以我更喜欢使用最小外接圆的方法来找到这些圆的坐标。你可以查看这个 链接

为了更轻松地找到这些圆的坐标,可以按照以下步骤操作:

  1. 为这个二值图像找到轮廓。
  2. 逐个遍历每个轮廓。
  3. 把轮廓的点转换成一个点集合。
  4. 对这个点集合使用 MinEnclosingCircle() 方法。
  5. 准确获取每个圆的坐标。

撰写回答