11

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.
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Charlie
  • 4,827
  • 2
  • 31
  • 55
  • Possible duplicate of https://stackoverflow.com/questions/23586979/aforge-image-processing-exception – dbc Nov 17 '14 at 07:43

1 Answers1

17

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.

Community
  • 1
  • 1
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Can i convert pixel Format of anyimage – Charlie Nov 17 '14 at 07:49
  • Sure, hold on a second. – dbc Nov 17 '14 at 07:49
  • tried to convert it from here http://stackoverflow.com/questions/2016406/converting-bitmap-pixelformats-in-c-sharp but it gives exception saying graphics can not be created from indexed pixel format – Charlie Nov 17 '14 at 07:52
  • I have used "PixelFormat.Format8bppIndexed" – Charlie Nov 17 '14 at 07:54
  • Well, I'd go for `Format24bppRgb` since `Format8bppIndexed` only allows for up to 256 colors. – dbc Nov 17 '14 at 07:56
  • @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??? – RBK Jul 19 '18 at 09:01