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:
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);
}
}
And this is the result I get:
The listbox shows the values, 0 is supposed to be perfect match.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.