How to match a set of contours with another set of contours

Viewed 81

Im trying to create a text matching application where I match the contours of 1 image with another one. I dont want to do OCR as I dont want to read the character, I just want to check if both contours are same like in the image below :

enter image description here I tried to do it with template matching, but template matching doesnt work on an array of contours. I saved the contours points to an list and tried to use the list against a set of another list in template match.

How can I match the contours of these 2 images and get a percentage of match?

EDIT:

As suggested by Jeru Luke, I tried the shape matching with all 3 matching types, but not getting proper result.

Here is the code I used :

      private void ApplyShapeMatching()
    {
        try
        {
            listBox1.Items.Clear();
            var img = IMGDict["input"].Clone();
            var imgSource = img.Convert<Gray, byte>()
                .ThresholdBinaryInv(new Gray(50), new Gray(255));
            pictureBox3.Image = imgSource.ToBitmap();

            var imgTarget = imgtemplate.Convert<Gray, byte>()
                .ThresholdBinaryInv(new Gray(50), new Gray(255));
            pictureBox4.Image = imgTarget.ToBitmap();

            VectorOfVectorOfPoint SourceContour = new VectorOfVectorOfPoint();
            Mat hier = new Mat();
            CvInvoke.FindContours(imgSource, SourceContour, hier, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

            VectorOfVectorOfPoint TargetContours = new VectorOfVectorOfPoint();
            Mat hier1 = new Mat();
            CvInvoke.FindContours(imgTarget, TargetContours, hier1, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
            ContoursMatchType type = new ContoursMatchType() ;

            if (comboBox1.SelectedIndex == 0)
            {
              type   = ContoursMatchType.I1;
            }

            if (comboBox1.SelectedIndex == 1)
            {
                type = ContoursMatchType.I2;
            }

            if (comboBox1.SelectedIndex == 2)
            {
                type = ContoursMatchType.I3;
            }
        

            for (int i = 0; i < SourceContour.Size; i++)
            {
                var distance = CvInvoke.MatchShapes( SourceContour[i] , TargetContours[i] , type);

            
                    var rect = CvInvoke.BoundingRectangle(SourceContour[i]);

                if(distance > 5.0)
                {
                    img.Draw(rect, new Bgr(0, 0, 255), 2);
                }

                else
                {
                    img.Draw(rect, new Bgr(0, 255, 0), 2);
                }
               
                CvInvoke.PutText(img, distance.ToString("F6"), new Point(rect.X, rect.Y + 20),
                        Emgu.CV.CvEnum.FontFace.HersheyPlain, 3, new MCvScalar(255, 0, 0));
                    listBox1.Items.Add(distance.ToString());               
            }

            pictureBox1.Image = img.ToBitmap();

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
1 Answers

I found the solution. Turns out that the size and orientation of both anchors needs to be same. So I first deskewed the image/ROI, then set both image size as same to match same character size, then I extracted contours and then used shapematch to get better results.

Related