源或模板图像的不受支持的像素格式.AForge Imaging

Cha*_*lie 8 c# imaging exception aforge

我得到以下例外ProcessImage(bitmap1, bitmap2);

Unsupported Pixel Format of source or template image
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我也managedImage从下面的代码传递到方法,但它仍然给出错误:

UnmanagedImage unmanagedImageA = UnmanagedImage.FromManagedImage(bitmap1);
Bitmap managedImageA = unmanagedImageA.ToManagedImage();
UnmanagedImage unmanagedImageB = UnmanagedImage.FromManagedImage(bitmap2);
Bitmap managedImageB = unmanagedImageB.ToManagedImage();
Run Code Online (Sandbox Code Playgroud)
  1. 我已经从我的计算机中随机传递了图像,它们都是例外.
  2. 我已经将在paint中编辑的Blank Image传递给方法,它仍然给出异常.
  3. 还检查了,jpeg,png,bmp格式,没什么用.

dbc*_*dbc 16

试试ExhaustiveTemplateMatching:

该类实现了详尽的模板匹配算法,该算法对源图像进行完整扫描,将每个像素与模板的对应像素进行比较.

该类仅处理灰度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;
}
Run Code Online (Sandbox Code Playgroud)

你会用的是System.Drawing.Imaging.PixelFormat.Format24bppRgb.