Reputation: 3855
Trying to compare 2 Bitmaps using AForge.Imaging
, i am getting Template's size should be smaller or equal to source image's size
when calling the Compare Extention Method.
public static Boolean Compare(this Bitmap image1, Bitmap image2, double comparisionLevel, float threshold)
{
return new ExhaustiveTemplateMatching(threshold)
.ProcessImage(image1.To24bppRgbFormat(), image2.To24bppRgbFormat())[0]
.Similarity >= comparisionLevel;
}
public static Bitmap To24bppRgbFormat(this Bitmap img)
{
return img.Clone(new Rectangle(0, 0, img.Width, img.Height),
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
What am i missing?
Upvotes: 1
Views: 4308
Reputation: 832
The template image size (Width and Height) must be smaller than the image you are triyng to compare.
The first thing to do is something like this :
if(templateImage.Height > uploadedImage.Height || templateImage.Width > uploadedImage.Width)
uploadedImage = ResizeImage(uploadedImage, uploadedImage.Height, templateImage.Width)
You can find a lot of implementations of a ResizeImage i find this one interesting (https://stackoverflow.com/a/6501997/3852812), you just have to replace Math.Min with Math.Max
Upvotes: 1
Reputation: 8047
Based on the error you are getting, and the documentation for the ExhaustiveTemplateMatching call, it looks like image2
is larger than image1
. I don't think your extension method has any errors in it.
Overall, it looks like your issue is with image1
and image2
themselves. One possible solution is to add logic to determine which image is larger, and then pass that one in as the sourceImage
parameter and pass the other as the templateImage
.
I have no idea how this method handles cases where image1 is taller, but image2 is wider though...
Disclaimer: I have never used AForge; I am just gleaning I can from overall C# knowledge and a brief look at the method documentation.
Upvotes: 2