Skip to main content
 首页 » 编程设计

c#之在 Emgu.CV 中,这些阈值是什么意思,是否有更好的方法来检测圆

2024年02月23日20thcjp

背景

这是我的 Emgu.CV 代码,用于获取图像并绘制其中找到的圆圈(大部分代码来自 Emgu.CV.Examples 解决方案中的 ShapeDetection 项目,EmguCV 下载随附):

//Load the image from file 
Image<Bgr, Byte> img = new Image<Bgr, byte>(myImageFile); 
 
//Get and sharpen gray image (don't remember where I found this code; prob here on SO) 
Image<Gray, Byte> graySoft = img.Convert<Gray, Byte>().PyrDown().PyrUp(); 
Image<Gray, Byte> gray = graySoft.SmoothGaussian(3); 
gray = gray.AddWeighted(graySoft, 1.5, -0.5, 0); 
 
Image<Gray, Byte> bin = gray.ThresholdBinary(new Gray(149), new Gray(255)); 
 
Gray cannyThreshold = new Gray(149); 
Gray cannyThresholdLinking = new Gray(149); 
Gray circleAccumulatorThreshold = new Gray(1000); 
 
Image<Gray, Byte> cannyEdges = bin.Canny(cannyThreshold, cannyThresholdLinking); 
 
//Circles 
CircleF[] circles = cannyEdges.HoughCircles( 
    cannyThreshold, 
    circleAccumulatorThreshold, 
    4.0, //Resolution of the accumulator used to detect centers of the circles 
    15.0, //min distance  
    5, //min radius 
    0 //max radius 
    )[0]; //Get the circles from the first channel 
 
//draw circles (on original image) 
foreach (CircleF circle in circles) 
    img.Draw(circle, new Bgr(Color.Brown), 2); 

这是图片:

问题

  1. 好的,所以我知道 ThresholdBinary 中的阈值是多少。由于我是从灰度图像中获取二值图像,因此它是图片中灰色的强度。这是因为图片中灰度圆的强度是 150 到 185。我假设这与 HoughCircles 的第一个参数相同。

    我不知道 circleAccumulatorThreshold、累加器的分辨率和最小距离(HoughCircles 的第 2、第 3 和第 4 个参数)是什么,或者那里应该有什么值。我显然没有正确的值,因为图片中的圆圈没有正确“houghed”。

  2. 我的第二个问题是,有没有更好的方法来找到圆?我需要能够在多种类型的光线下检测到这个圆圈(即圆圈颜色强度可能很低,例如 80 或更低)并在图片中获得它的尺寸。匹配圆圈的最佳方法是什么?我是否应该将圆圈换成另一种颜色并在原始图像中查找该颜色?还有其他想法吗?

谢谢

请您参考如下方法:

  • accumulator 是必须“累积”多少点才能被视为 圆圈。数字越大,检测到的圆圈越少。
  • 分辨率如何 关闭点必须是建议的圆圈。基本上是 像素的“大小”。
  • MinDistance 是圆允许的距离 成为彼此。在您的示例中,您有 3 个圆圈 彼此靠近。增加最小距离会阻止 重叠的圆圈,而不是只画一个。

至于你对第二个的答案,模糊图像,转换为灰度,然后阈值来消除光照差异是通常的解决方案