opencv的图象格式IplImage 和CBitmap的转换

来源:互联网 发布:最新理财软件 编辑:程序博客网 时间:2024/05/16 23:51

这个函数我以前好象写过,要是能放到我的boke上,我现在也不用重新写了。

 BOOL IplImage2Bmp(HDC dc,IplImage *pImage,CBitmap *bitmap)
{
 if( pImage && pImage->depth == IPL_DEPTH_8U )
    {
        uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
        BITMAPINFO* bmi = (BITMAPINFO*)buffer;
        int bmp_w = pImage->width, bmp_h = pImage->height;

        //FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), pImage->origin );
  int width=bmp_w;
  int height=bmp_h;
  int bpp=pImage ? (pImage->depth & 255)*pImage->nChannels : 0;
  int origin=pImage->origin;
  HBITMAP hBitmap;
  char* dst_data = 0;

  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;
   }
  }
  if(bitmap->m_hObject!=NULL)
  {
   bitmap->Detach();
  }
  hBitmap=CreateDIBitmap(dc,bmih,CBM_INIT,pImage->imageData,bmi,DIB_RGB_COLORS);
  bitmap->Attach(hBitmap);
  return TRUE;
    }
 return FALSE;
 
}

原创粉丝点击