Emgu CV用于检测烤盘上的圆形饼干

时间:2014-09-04 05:28:37

标签: c# opencv image-processing emgucv hough-transform

我对图像处理相当陌生,据说,我希望有人可以告诉我,如果我在正确的轨道上,如果没有,请指出正确的方向和/或提供一些代码示例。

我正在处理的要求:

  • 检测烤盘上的饼干数量。
  • 饼干可以是任何颜色。
  • 饼干可能被巧克力(白色或黑色)覆盖,在这种情况下,每个饼干周围会有一堆巧克力,这意味着做一个简单的对比检查可能不会起作用。
  • 饼干不会重叠,但它们可能会相互接触。

我正在尝试使用HoughCirlces的Emgu CV库,但我的结果好坏参半。这是我使用winforms和C#的代码,其中我在烤盘上加载了一个cookie图像并运行它(我对我的值没有信心)。

我是否在正确的轨道上?有任何想法吗?代码示例?

以下是一些测试图片: http://imgur.com/a/dJmU6,然后是我的代码

enter image description here

enter image description here

private int GetHoughCircles(Image image)
    {
        Bitmap bitmap = new Bitmap(image);
        Image<Bgr, Byte> img = new Image<Bgr, byte>(bitmap).Resize(466, 345, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

        //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(150);
        Gray cannyThresholdLinking = new Gray(120);
        Gray circleAccumulatorThreshold = new Gray(50);

        Image<Gray, Byte> cannyEdges = bin.Canny(cannyThreshold.Intensity, cannyThresholdLinking.Intensity);
        //Image<Gray, Byte> cannyEdges = bin.Canny(cannyThreshold, cannyThresholdLinking);

        //Circles
        CircleF[] circles = cannyEdges.HoughCircles(
            cannyThreshold,
            circleAccumulatorThreshold,
            3.0, //Resolution of the accumulator used to detect centers of the circles
            50.0, //min distance 
            20, //min radius
            30 //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);
        }

        pictureBox1.Image = new Bitmap(img.ToBitmap());
        return circles.Count();
    }

0 个答案:

没有答案