【问题标题】:Unsupported Pixel Format of source or template image. AForge Imaging源或模板图像的不支持的像素格式。 AForge 成像
【发布时间】:2015-01-14 01:37:39
【问题描述】:

我在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();
  1. 我从我的计算机中随机传递了图像,它们都给出异常。
  2. 我已将在paint中编辑的空白图像传递给方法,它仍然给出异常。
  3. 还检查了 jpeg、png、bmp 格式,没有任何效果。

【问题讨论】:

标签: c# exception aforge imaging


【解决方案1】:

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

你会使用System.Drawing.Imaging.PixelFormat.Format24bppRgb

【讨论】:

  • 我可以转换任意图像的像素格式吗
  • 好的,等一下。
  • 试图从这里stackoverflow.com/questions/2016406/… 转换它,但它给出了一个异常,说不能从索引像素格式创建图形
  • 我用过“PixelFormat.Format8bppIndexed”
  • 好吧,我会选择 Format24bppRgb,因为 Format8bppIndexed 最多只能支持 256 种颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2019-06-09
  • 1970-01-01
相关资源
最近更新 更多