Bitmap与IplImage之间的转换
在MFC编程中,用OpenCV来处理图像时,可能会进行Bitmap与IplImage之间的转换;所以在此留个记号,以免下次再用到的时候,还要去找。
?
IplImage* BitmapToIplImage(HBITMAP hBmp){BITMAP bmp; GetObject(hBmp, sizeof(BITMAP), &bmp);int depth = (bmp.bmBitsPixel == 1) ? IPL_DEPTH_1U : IPL_DEPTH_8U;int nChannels = (bmp.bmBitsPixel == 1) ? 1 : bmp.bmBitsPixel/8; IplImage* img = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight), depth, nChannels); BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels]; GetBitmapBits(hBmp, bmp.bmHeight*bmp.bmWidth*nChannels, pBuffer);memcpy(img->imageData, pBuffer, bmp.bmHeight*bmp.bmWidth*nChannels); delete pBuffer;IplImage *dst = cvCreateImage(cvGetSize(img), img->depth,3); cvCvtColor(img, dst, CV_BGRA2BGR); cvReleaseImage(&img); return dst;}
?如果要从CBitmap转为IplImage,可以先将CBitmap转为BITMAP,再由BITMAP转为IplImage;
// CBitmap 转为 BITMAPCBitmap bitmap;bitmap.LoadBitmap(IDB_BITMAP);BITMAP bmp;bitmap.GetBitmap(&bmp);// CBitmap与HBITMAP间的转换// CBitmap转为HBITMAPCBitmap bitmap;bitmap.LoadBitmap(IDB_BITMAP);HBITMAP bmp = HBITMAP(bitmap);// HBITMAP转为CBitmapHBITMAP hbitmap; CBitmap bitmap;bitmap.Attach(hbitmap);?