Reputation: 697
I have a 24bpp GdiPlus::Bitmap
that i need to convert into IplImage
(opencv).
Does anyone know how can this be done?
Upvotes: 3
Views: 2213
Reputation: 9525
The difficulty is that Gdiplus::Bitmap supports lots of exotic pixel formats in theory, so in full generality the conversion would be verbose. However, the basic case is as follows:
IplImage* GdiPlusBitmapToOpenCvImage(Gdiplus::Bitmap* bmp)
{
auto format = bmp->GetPixelFormat();
if (format != PixelFormat24bppRGB)
return nullptr;
Gdiplus::Rect rcLock(0, 0, bmp->GetWidth(), bmp->GetHeight());
Gdiplus::BitmapData bmpData;
bmp->LockBits(&rcLock, Gdiplus::ImageLockModeRead, format, &bmpData);
int buffSz = bmpData.Stride * bmpData.Height;
int depth = 8, channel = 3;
IplImage* cvImage = cvCreateImage(CvSize(rcLock.Width, rcLock.Height), depth, channel);
const unsigned char* src = static_cast<unsigned char*>(bmpData.Scan0);
std::copy(src, src + buffSz, cvImage->imageData);
bmp->UnlockBits(&bmpData);
return cvImage;
}
Upvotes: 0
Reputation: 1348
Here is an answer for your issue with the only difference, that BitMap converts to Mat: Convert Bitmap to Mat
Upvotes: 0
Reputation: 454
Here is how you can do it.
1.) Create a new target IplImage object with the dimensions of Source image (GdiPlus::Bitmap)
2.) Get the pixeldata handle of Source image using lockbits
3.) Copy the source pixeldata to target imagedata
4.) UnlockBits of source image
Upvotes: 1