views:

90

answers:

1

How can I convert single channel IplImage (grayscale), depth=8, into a Bitmap? The following code runs, but displays the image in 256 color, not grayscale. (Color very different from the original)

btmap = gcnew Bitmap(
 cvImg->width , 
 cvImg->height , 
 cvImg->widthStep , 
 System::Drawing::Imaging::PixelFormat::Format8bppIndexed,
 (System::IntPtr)cvImg->imageData)
 ;

I believe my problem lies in the PixelFormat. Ive tried scaling the image to 16bit and setting the pixel format to 16bppGrayscale, but this crashes the form when uploading the image.

The destination is a PicturePox in a C# form.Thanks.

+1  A: 

You need to create ColorPalette instance, fill it with grayscale palette and assign to btmap->Palette property.

Edit: Actually, creating ColorPalette class is a bit tricky, it is better to modify color entries directly in btmap->Palette. Set these entries to RGB(0,0,0), RGB(1,1,1) ... RGB(255,255,255). Something like this:

ColorPalette^ palette = btmap->Palette;


array<Color>^ entries = palette->Entries;


for ( int i = 0; i < 256; ++i )

{

   entries[i] = Color::FromArgb(i, i, i);

}
Alex Farber
Sweet as. Applying that created pallete to my btmap->Palatte property works a treat. Thanks. 8hrs researching versus 20mins on stackoverflow. Thanks Alex
Shaun83