visual-studio - HoughCircles 在 OpenCV 中无法正确检测圆圈

标签 visual-studio emgucv opencv3.0

我使用的是 Visual Studio 2015、OpenCV.3 和 EmguCV.3。
我的代码如下所示,结果如图所示。我知道问题是HoughCircles函数的输入值,但不知道哪些输入适合这张图片。我很感激任何帮助。

                Image<Gray, byte> OriginalImage = new Image<Gray, byte>(Openfile.FileName);
                Image<Gray, byte> ResizedImage = OriginalImage.Resize(OriginalImage.Width / 2, OriginalImage.Height / 2, Emgu.CV.CvEnum.Inter.Cubic);

                //********** Convert Image to Binary
                Image<Gray, byte> smoothImg = 
                ResizedImage.SmoothGaussian(5);
                smoothImg._Erode(5);
                smoothImg._Dilate(5);
                Image<Gray, byte> BinaryImage = 
                smoothImg.ThresholdBinary(new Gray(20), new Gray(255));

                //********** Find Circles
                Image<Rgb, byte> ROIImgScaledCircles = ROIImgScaled.Convert<Rgb, byte>();
                CircleF[] circles = smoothImg.HoughCircles(
                    new Gray(180),//cannyThreshold
                    new Gray(60),//circleAccumulatorThreshold
                    2.0, //dp:Resolution of the accumulator used to detect centers of the circles
                    10.0, //min distance 
                    10, //min radius
                    128 //max radius
                    )[0]; //Get the circles from the first channel
                foreach (CircleF cir in circles)
                {
                    ROIImgScaledCircles.Draw(cir, new Rgb(235, 20, 30), 1);
                }                   
                pbxCircles.Image = ROIImgScaledCircles.ToBitmap();

原图:

enter image description here

成立圈子:

enter image description here

最佳答案

使用完整的形状,您可能会发现检测边缘然后找到轮廓更容易。下面是一个例子:

    Image<Bgr, byte> original = new Image<Bgr, byte>(@"E:\Downloads\original.jpg");
    UMat grayscale = new UMat();            
    UMat pyrdown = new UMat();
    UMat canny = new UMat();

    double cannyThreshold = 128;

    CvInvoke.CvtColor(original, grayscale, ColorConversion.Bgr2Gray);        
    // remove noise and run edge detection
    CvInvoke.PyrDown(grayscale, pyrdown);
    CvInvoke.PyrUp(pyrdown, grayscale);
    CvInvoke.Canny(grayscale, canny, cannyThreshold, cannyThreshold * 2);

    Image<Bgr, byte> result = original.Copy();
    // find and draw circles   
    VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
    CvInvoke.FindContours(canny, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
    //CvInvoke.DrawContours(result, contours, -1, new MCvScalar(0, 0, 255));
    for (int i = 0; i < contours.Size; i++)
    {
        Ellipse ellipse = new Ellipse(CvInvoke.FitEllipse(contours[i]));
        result.Draw(ellipse, new Bgr(Color.Red), 1);
    }

    result.Save(@"E:\Downloads\circles.jpg");

结果如下,从左到右:
  • 原图
  • 模糊的图像(使用 pyrdown/pyrup)
  • 精明边缘检测的结果
  • 从轮廓重建圆

  • process from the original image to the result

    关于visual-studio - HoughCircles 在 OpenCV 中无法正确检测圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44987875/

    相关文章:

    opencv - 如何确定对象是否停止移动或离开框架

    c# - 通用图像类型 emgu C#

    c# - EmguCV 打开相机但不查看 ImageBox 中的帧

    OpenCV cuda::meanStdDev 支持 CV_32FC1

    c# - 我如何告诉 VS 2008 停止在我的文件前面放置字节顺序标记?

    c++ - 监视表达式中的 vector 大小不正确

    c# - 如何访问同一解决方案中两个应用程序中的断点

    visual-studio-2010 - 在 Visual Studio 构建后事件中删除整个文件夹

    android - 如何理解OpenCV程序中引发的此异常

    python - opencv错误文件无法打开读取cv::face::FaceRecognizer::read windows