AForge图像比较

时间:2014-02-06 17:45:05

标签: c# bitmap aforge

在调用比较扩展方法时,尝试使用AForge.Imagingi am getting Template's size should be smaller or equal to source image's size比较2个位图。

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);
        }

我缺少什么?

2 个答案:

答案 0 :(得分:2)

根据您收到的错误以及ExhaustiveTemplateMatching来电的文档,image2似乎大于image1。我不认为你的扩展方法有任何错误。

总的来说,您的问题似乎与image1image2本身有关。一种可能的解决方案是添加逻辑以确定哪个图像更大,然后将其传递为sourceImage参数,并将另一个传递为templateImage

我不知道这个方法如何处理image1较高的情况,但是image2虽然更宽......

免责声明:我从未使用过AForge;我只是从整体C#知识和简要介绍方法文档中收集到的信息。

答案 1 :(得分:1)

模板图片大小(宽度和高度)必须小于您要比较的图像。

要做的第一件事就是这样:

if(templateImage.Height > uploadedImage.Height || templateImage.Width > uploadedImage.Width)
    uploadedImage = ResizeImage(uploadedImage, uploadedImage.Height, templateImage.Width)

你可以找到很多ResizeImage的实现我发现这个有趣(https://stackoverflow.com/a/6501997/3852812),你只需用Math.Max替换Math.Min