MFC中HBITMAP如何转化成opencv的IplImage格式呢?最好能有详细的代码。
展开全部
IplImage* CShowImage1::hBitmapToIpl(HBITMAP hBmp)
{
BITMAP bmp;
GetObject(hBmp,sizeof(BITMAP),&bmp);
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
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;
}
{
BITMAP bmp;
GetObject(hBmp,sizeof(BITMAP),&bmp);
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询