Unsupported Pixel Format of source or template image. AForge Imaging

10,262

Try ExhaustiveTemplateMatching:

The class implements exhaustive template matching algorithm, which performs complete scan of source image, comparing each pixel with corresponding pixel of template.

The class processes only grayscale 8 bpp and color 24 bpp images.

So, those are the image formats you must use.

As requested, to convert to a specific pixel format, you can do this:

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

The one you would use is System.Drawing.Imaging.PixelFormat.Format24bppRgb.

Share:
10,262

Related videos on Youtube

Charlie
Author by

Charlie

Updated on June 26, 2022

Comments

  • Charlie
    Charlie almost 2 years

    I am getting the following Exception at ProcessImage(bitmap1, bitmap2);

    Unsupported Pixel Format of source or template image
    

    and this is my code:

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

    I have also passed managedImage from the below code into the method, but it still gives error:

    UnmanagedImage unmanagedImageA = UnmanagedImage.FromManagedImage(bitmap1);
    Bitmap managedImageA = unmanagedImageA.ToManagedImage();
    UnmanagedImage unmanagedImageB = UnmanagedImage.FromManagedImage(bitmap2);
    Bitmap managedImageB = unmanagedImageB.ToManagedImage();
    
    1. I have passed Images randomly from my computer, they all give exception.
    2. I have passed Blank Image edited in paint into the method,it still give exception.
    3. Also checked, jpeg, png, bmp formats, nothing work.
  • Charlie
    Charlie over 9 years
    Can i convert pixel Format of anyimage
  • dbc
    dbc over 9 years
    Sure, hold on a second.
  • Charlie
    Charlie over 9 years
    tried to convert it from here stackoverflow.com/questions/2016406/… but it gives exception saying graphics can not be created from indexed pixel format
  • Charlie
    Charlie over 9 years
    I have used "PixelFormat.Format8bppIndexed"
  • dbc
    dbc over 9 years
    Well, I'd go for Format24bppRgb since Format8bppIndexed only allows for up to 256 colors.
  • RBK
    RBK over 5 years
    @dbc I wanna use 16 bit grayscale image. Can I use it? I am trying to stitch image in Accord.net project having 16 bit images. I know it supports upto 8 bit. But my question is can I use it for 16 bits grayscale images???

Related