Ejemplo n.º 1
0
        private void ApplyShapeMatching(Image <Bgr, byte> imgTemplate, double threshold = 0.00001,
                                        double area = 1000, ContoursMatchType matchType = ContoursMatchType.I2)
        {
            try
            {
                var img       = IMGDict["input"].Clone();
                var imgSource = img.Convert <Gray, byte>()
                                .SmoothGaussian(3)
                                .ThresholdBinaryInv(new Gray(240), new Gray(255));

                var imgTarget = imgTemplate.Convert <Gray, byte>()
                                .SmoothGaussian(3)
                                .ThresholdBinaryInv(new Gray(240), new Gray(255));

                var imgSourceContours = CalculateContours(imgSource, area);
                var imgTargetContours = CalculateContours(imgTarget, area);

                if (imgSourceContours.Size == 0 || imgTargetContours.Size == 0)
                {
                    throw new Exception("Not engough contours.");
                }

                for (int i = 0; i < imgSourceContours.Size; i++)
                {
                    var distance = CvInvoke.MatchShapes(imgSourceContours[i], imgTargetContours[0]
                                                        , matchType);

                    if (distance <= threshold)
                    {
                        var rect = CvInvoke.BoundingRectangle(imgSourceContours[i]);
                        img.Draw(rect, new Bgr(0, 255, 0), 4);
                        CvInvoke.PutText(img, distance.ToString("F6"), new Point(rect.X, rect.Y + 20),
                                         Emgu.CV.CvEnum.FontFace.HersheyPlain, 3, new MCvScalar(255, 0, 0));
                    }
                }

                pictureBox1.Image = img.ToBitmap();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (pictureBox1.Image == null)
                {
                    throw new Exception("Select a template image.");
                }
                var img = new Bitmap(pictureBox1.Image).ToImage <Bgr, byte>();
                ContoursMatchType matchType = ContoursMatchType.I2;

                switch (comboBox1.SelectedIndex)
                {
                case 0:
                    matchType = ContoursMatchType.I1;
                    break;

                case 1:
                    matchType = ContoursMatchType.I2;
                    break;

                case 2:
                    matchType = ContoursMatchType.I3;
                    break;
                }

                double areaThreshold = 1000;
                double.TryParse(tbMinArea.Text, out areaThreshold);
                double threshold = 0.001;
                double.TryParse(tbDistanceThreshold.Text, out threshold);

                if (OnShapeMatching != null)
                {
                    OnShapeMatching(img, threshold, areaThreshold, matchType);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }