1

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?

FPGA
  • 3,525
  • 10
  • 44
  • 73
  • 1
    I'm not seeing anything offhand that looks like it would cause an error. Are you sure that your extension method is the source of the error? It's possible that the extension method is working fine, but `ProcessImage` doesn't like the values being passed to it. I would recommend setting the results of `img.Clone` to a local variable and then returning that, then try debugging your application. If `To24bppRgbFormat` doesn't throw an exception, you can then see what value it's actually returning, which might help you solve your problem. – Jon Senchyna Feb 06 '14 at 17:53

2 Answers2

2

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.

Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
  • That was helpful thanks, i thought it handles all of this internally, at least i know whats wrong now – FPGA Feb 06 '14 at 18:02
  • A simple area check of the two images might work for you, although there's still the issue of one of them being taller and the other wider. AForge may not even support those types of comparisons. – Jon Senchyna Feb 06 '14 at 18:08
  • yes that was the problem comparing 2 images of the same size or an image to itself works just fine – FPGA Feb 06 '14 at 18:10
1

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

Community
  • 1
  • 1
Kabindas
  • 802
  • 9
  • 6