Bitmap与IplImage之间的转换

  • //Karl_bmp.h
  • //1.IplImage 2 CBitmap
  • CBitmap * IplImage2CBitmap(const IplImage *pImage)
  • {
  • if( pImage && pImage->depth == IPL_DEPTH_8U )
  • {
  • HDC hDC=GetDC()->GetSafeHdc();
  • uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
  • BITMAPINFO* bmi = (BITMAPINFO*)buffer;
  • int bmp_w = pImage->width, bmp_h = pImage->height;
  • FillBitmapInfo( bmi, bmp_w, bmp_h, pImage->depth*pImage->nChannels, pImage->origin );
  • char *pBits=NULL;
  • HBITMAP hBitmap=CreateDIBSection(hDC,bmi,DIB_RGB_COLORS,(void**)&pBits,NULL,0);
  • memcpy(pBits,pImage->imageData,pImage->imageSize);
  • CBitmap *pBitmap=new CBitmap;
  • pBitmap->Attach(hBitmap);
  • return pBitmap;
  • }
  • else
  • return NULL;
  • }
  • void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin)
  • {
  • assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
  • BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
  • memset( bmih, 0, sizeof(*bmih));
  • bmih->biSize = sizeof(BITMAPINFOHEADER);
  • bmih->biWidth = width;
  • bmih->biHeight = origin ? abs(height) : -abs(height);
  • bmih->biPlanes = 1;
  • bmih->biBitCount = (unsigned short)bpp;
  • bmih->biCompression = BI_RGB;
  • if( bpp == 8 )
  • {
  • RGBQUAD* palette = bmi->bmiColors;
  • int i;
  • for( i = 0; i < 256; i++ )
  • {
  • palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
  • palette[i].rgbReserved = 0;
  • }
  • }
  • }
  • //2.CBitmap 2 IplImage
  • IplImage *CBitmap2IplImage(const CBitmap *pBitmap)
  • {
  • DIBSECTION ds;
  • pBitmap->GetObject(sizeof(ds),&ds);
  • IplImage *pImage=cvCreateImage(cvSize(ds.dsBm.bmWidth,ds.dsBm.bmHeight),8,ds.dsBmih.biBitCount/8);
  • memcpy(pImage->imageData,ds.dsBm.bmBits,pImage->imageSize);
  • return pImage;
  • }
  • //3.HBITMAP 2 IplImage
  • IplImage* hBitmap2Ipl(HBITMAP hBmp)
  • {
  • BITMAP bmp;
  • ::GetObject(hBmp,sizeof(BITMAP),&bmp);//hBmp-->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); //cvCreateImageHeader
  • //pBuffer = (char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));
  • memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);
  • IplImage *dst = cvCreateImage(cvGetSize(img),img->depth,3);
  • cvCvtColor(img,dst,CV_BGRA2BGR);
  • cvReleaseImage(&img);
  • return dst;
  • }
  • //4.IplImage 2 HBITMAP
  • HBITMAP IplImage2hBitmap(IplImage* pImg)
  • {
  • BYTE tmp[sizeof(BITMAPINFO)+1024];
  • BITMAPINFO *bmi = (BITMAPINFO*)tmp;
  • HBITMAP hBmp;
  • int i;
  • memset(bmi,0,sizeof(BITMAPINFO));
  • bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  • bmi->bmiHeader.biWidth = pImg->width;
  • bmi->bmiHeader.biHeight = pImg->height;
  • bmi->bmiHeader.biPlanes = 1;
  • bmi->bmiHeader.biBitCount = pImg->nChannels * pImg->depth;
  • bmi->bmiHeader.biCompression = BI_RGB;
  • bmi->bmiHeader.biSizeImage = 0;//if biCompression is BI_RGB,this can be 0
  • bmi->bmiHeader.biClrImportant =0 ;
  • switch(pImg->nChannels * pImg->depth)
  • {
  • case 8 :
  • for(i=0 ; i < 256 ; i++){
  • bmi->bmiColors[i].rgbBlue = i;
  • bmi->bmiColors[i].rgbGreen= i;
  • bmi->bmiColors[i].rgbRed= i;
  • }
  • break;
  • case 32:
  • case 24:
  • ((DWORD*) bmi->bmiColors)[0] = 0x00FF0000;
  • ((DWORD*) bmi->bmiColors)[1] = 0x0000FF00;
  • ((DWORD*) bmi->bmiColors)[2] = 0x000000FF;
  • break;
  • }
  • hBmp = ::CreateDIBSection(NULL,bmi,DIB_RGB_COLORS,NULL,0,0);
  • SetDIBits(NULL,hBmp,0,pImg->height,pImg->imageData,bmi,DIB_RGB_COLORS);
  • return hBmp;
  • }
  • //5.HBITMAP 2 CBitmap
  • CBitmap HBITMAP2CBitmap(HBITMAP hbitmap)
  • {
  • CBitmap cbitmap;
  • cbitmap.Attach(hbitmap);
  • return cbitmap;
  • }
  • 6.CBitmap 2 HBITMAP
  • HBITMAP CBitmap2HBITMAP(CBitmap bitmap )
  • {
  • HBITMAP bmp = HBITMAP(bitmap);
  • //bmp=(HBITMAP)bitmap.GetSafeHandle();
  • return bmp;
  • }
  • //7.BITMAP 2 CBitmap
  • CBitmap BITMAP2CBitmap(BITMAP bmp)
  • {
  • CBitmap bitmap;
  • bitmap.GetBitmap(&bmp);
  • return bitmap;
  • }
  • //8.HBITMAP 2 BITMAP
  • BITMAP HBITMAP2BITMAP(HBITMAP hBmp)
  • {
  • BITMAP bmp;
  • ::GetObject(hBmp,sizeof(BITMAP),&bmp);//
  • return bmp;
  • }

Bitmap转为iplimage代码

  1. IplImage* BitmapToIplImage(HBITMAP hBmp)
  2. {
  3. BITMAP bmp;
  4. GetObject(hBmp, sizeof(BITMAP), &bmp);
  5. int depth = (bmp.bmBitsPixel == 1) ? IPL_DEPTH_1U : IPL_DEPTH_8U;
  6. int nChannels = (bmp.bmBitsPixel == 1) ? 1 : bmp.bmBitsPixel/8;
  7. IplImage* img = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight), depth, nChannels);
  8. BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels];
  9. GetBitmapBits(hBmp, bmp.bmHeight*bmp.bmWidth*nChannels, pBuffer);
  10. memcpy(img->imageData, pBuffer, bmp.bmHeight*bmp.bmWidth*nChannels);
  11. delete pBuffer;
  12. IplImage *dst = cvCreateImage(cvGetSize(img), img->depth,3);
  13. cvCvtColor(img, dst, CV_BGRA2BGR);
  14. cvReleaseImage(&img);
  15. return dst;
  16. }

如果要从CBitmap转为IplImage,可以先将CBitmap转为BITMAP,再由BITMAP转为IplImage;

Cbitmap 转为 bitmap代码

  1. // CBitmap 转为 BITMAP
  2. CBitmap bitmap;
  3. bitmap.LoadBitmap(IDB_BITMAP);
  4. BITMAP bmp;
  5. bitmap.GetBitmap(&bmp);
  6. // CBitmap与HBITMAP间的转换
  7. // CBitmap转为HBITMAP
  8. CBitmap bitmap;
  9. bitmap.LoadBitmap(IDB_BITMAP);
  10. HBITMAP bmp = HBITMAP(bitmap);
  11. // HBITMAP转为CBitmap
  12. HBITMAP hbitmap;
  13. CBitmap bitmap;
  14. bitmap.Attach(hbitmap);

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券