sMaN
sMaN

Reputation: 3937

How to convert Single Channel IplImage* to Bitmap^

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.

Upvotes: 0

Views: 2811

Answers (2)

saeid zamani
saeid zamani

Reputation: 11

 int intStride = (AfterHist.width * AfterHist.nChannels + 3) & -4;
            Bitmap BMP = new Bitmap(AfterHist.width,
                            AfterHist.height, intStride,
                            PixelFormat.Format24bppRgb, AfterHist.imageData);

this way is correct to create a bitmap of a IPLimage.

Upvotes: 1

Alex F
Alex F

Reputation: 43331

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

}

Upvotes: 1

Related Questions