使用EMGU识别相当简单的形状

时间:2017-01-25 18:05:40

标签: c# opencv image-processing computer-vision emgucv

我正试图找出一个" T"形状以及" L"形状可以反转,如下图所示。我在C#中使用EMGU。

形状:T

enter image description here

形状:L(常规)

enter image description here

形状:L(反转)

enter image description here

我试图不断检测这些不同的形状,我尝试使用the EMGU shape detection tutorial中所示的Contour方法。问题是它没有正确检测轮廓点,或者偶尔检测到轮廓点,它不可靠。

轮廓逻辑输出

enter image description here

这些形状大致相同,但可以旋转或略有不同的视角。什么是一种准确,有效的方法来一致地检测这些不同的形状?

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经用您给定的图像尝试了我的算法,结果和代码如下。我使用了thresholding和findcontours函数来检测你的数字。

Thresholding Function Applied

findContours Function applied

Thresholding Function Applied

findContours Function applied

以下是查找精确轮廓的代码。一旦找到轮廓,我们就可以计算出它的长度或者只是存储它的大小。无论在任何方向,相同的轮廓总是具有相同的尺寸或长度(大约)。

Mat original_image = new Mat();
Mat gray = new Mat();
Mat threshold_image = new Mat();
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat new_image = new Mat();
Mat con = new Mat();

//TrackBar Scroll
    private void trackBarLow_Scroll(object sender, EventArgs e)
    {
        try
        {
            low = trackBarLow.Value;
            threshold_value = trackBarThreshold.Value;
            CvInvoke.CvtColor(original_image, gray, ColorConversion.Bgr2Gray, 0);
            CvInvoke.Threshold(gray, threshold_image, low, threshold_value, ThresholdType.Binary);
            Image<Bgr, Byte> threshold_I = threshold_image.ToImage<Bgr, Byte>();
            pictureBox2.Image = threshold_I.ToBitmap();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
     }

     private void buttonExactContours_Click(object sender, EventArgs e)
     {
        try
        {
            new_image = new Mat(new System.Drawing.Size(640, 480), DepthType.Cv16S, 3);
            new_image.SetTo(new Emgu.CV.Structure.MCvScalar(150, 0, 150));
            con = threshold_image;

            Image<Bgr, Byte> new_image1_I = new_image.ToImage<Bgr, Byte>();
            pictureBox3.Image = new_image1_I.ToBitmap();

            CvInvoke.FindContours(con, contours, null, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple, new Point(0, 0));

            IInputArrayOfArrays arr;

            for (int j = 0; j < contours.Size; j++)
            {
                arr = contours[j];

                //textBox1.AppendText(Environment.NewLine + CvInvoke.ArcLength(arr, true).ToString() + Environment.NewLine);
                textBox1.AppendText(Environment.NewLine + contours[j].Size.ToString() + Environment.NewLine);
                if (contours[j].Size < 500) //Value "500" may vary according to the size you may want to filter
                    continue;
            }
            Image<Bgr, Byte> new_image_I = new_image.ToImage<Bgr, Byte>();
            pictureBox2.Image = new_image_I.ToBitmap();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }