Last active
May 7, 2016 10:30
-
-
Save icecoobe/8328992 to your computer and use it in GitHub Desktop.
Convert "HBITMAP" to "IplImage*"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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