• IplImage 与 CBitmap类 的相互转换


    转自:http://blog.csdn.net/highyyy/archive/2011/02/26/6210408.aspx

    在VC中利用OpenCV做图像处理程序时,有时需要把IpImage 类型和CBitmap类型相互转换,这样就可以利用VC中的GDI+函数对图像进行某些特殊的显示和处理,非常方便。这里是本人项目中写的两个转换小函数,仅供参考,转载注明,这样方便发现问题的朋友联系我及时修改。

    1.IplImage转换为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转换为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 转换为IplImage

    IplImage* hBitmap2Ipl(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);
        //img->imageData = (char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));
        memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);
        return img;
    }


    本博客所有博文,若无专门说明皆为原创,转载请注明作者和出处!
  • 相关阅读:
    JDK 1.7.0 安装后,如何配置,以及如何打开JAR文件
    C++中的内存!(转载)堆 栈 全局/static变量区 常量区
    学习.net应该知道什么
    SQL Group By
    编码
    ASP生成静态Html文件技术杂谈
    如何写出优秀的ASP应用
    DropDownList的绑定方法
    如何提高WEB程序的效率
    什么才是提高ASP性能的最佳选择
  • 原文地址:https://www.cnblogs.com/ifinver/p/2828698.html
Copyright © 2020-2023  润新知