- // hbitmap convert to IplImage
- IplImage* hBitmapToIpl(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;
- // create header image
- IplImage* img = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight),depth,nChannels);
- // allocat memory for the pBuffer
- BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels];
- // copies the bitmap bits of a specified device-dependent bitmap into a buffer
- GetBitmapBits(hBmp,bmp.bmHeight*bmp.bmWidth*nChannels,pBuffer);
- // copy data to the imagedata
- memcpy(img->imageData,pBuffer,bmp.bmHeight*bmp.bmWidth*nChannels);
- delete pBuffer;
- // create the image
- IplImage *dst = cvCreateImage(cvGetSize(img),img->depth,3);
- // convert color
- cvCvtColor(img,dst,CV_BGRA2BGR);
- cvReleaseImage(&img);
- return dst;
- }