我在ProcessImage(bitmap1, bitmap2)
收到以下例外情况;
Unsupported Pixel Format of source or template image
这是我的代码:
public static double FindComparisonRatioBetweenImages(
System.Drawing.Image one, System.Drawing.Image two)
{
Bitmap bitmap1 = new Bitmap(one);
Bitmap bitmap2 = new Bitmap(two);
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0);
TemplateMatch[] matchings = null;
matchings = tm.ProcessImage(bitmap1, bitmap2); // Exception occurs here!
return matchings[0].Similarity;
}
我还从下面的代码中将managedImage
传递给了方法,但它仍然会出错:
UnmanagedImage unmanagedImageA = UnmanagedImage.FromManagedImage(bitmap1);
Bitmap managedImageA = unmanagedImageA.ToManagedImage();
UnmanagedImage unmanagedImageB = UnmanagedImage.FromManagedImage(bitmap2);
Bitmap managedImageB = unmanagedImageB.ToManagedImage();
答案 0 :(得分:15)
该类实现了详尽的模板匹配算法,该算法对源图像进行完整扫描,将每个像素与模板的相应像素进行比较。
该类仅处理灰度8 bpp和彩色24 bpp图像。
因此,这些是您必须使用的图像格式。
根据要求,要转换为特定的像素格式,您可以执行以下操作:
public static Bitmap ConvertToFormat(this Image image, PixelFormat format)
{
Bitmap copy = new Bitmap(image.Width, image.Height, format);
using (Graphics gr = Graphics.FromImage(copy))
{
gr.DrawImage(image, new Rectangle(0, 0, copy.Width, copy.Height));
}
return copy;
}