Skip to content

Instantly share code, notes, and snippets.

@icecoobe
Last active May 7, 2016 10:30
Show Gist options
  • Save icecoobe/8328992 to your computer and use it in GitHub Desktop.
Save icecoobe/8328992 to your computer and use it in GitHub Desktop.
Convert "HBITMAP" to "IplImage*"
IplImage* hBitmap2Ipl(HBITMAP hBmp)
{
BITMAP bmp;
::GetObject(hBmp,sizeof(BITMAP),&bmp);
// get channels which equal 1 2 3 or 4
// bmBitsPixel :
// Specifies the number of bits
// required to indicate the color of a pixel.
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
// get depth color bitmap or grayscale
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
IplImage* img = cvCreateImageHeader( cvSize(bmp.bmWidth, bmp.bmHeight), depth, nChannels );
// Copy bitmap data to IplImage
img->imageData =
(char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));
memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment