posts - 36,  comments - 28,  trackbacks - 0

Gdiplus::Bitmap转 IplImage

// pIplImage 需要外部释放 Mosesyuan
 void CGeneral::BitmapToIplImage(Bitmap* pBitmap, IplImage* &pIplImg)
{
    if (!pBitmap)
    {
        return;
    }
    if(pIplImg)
    {
        cvReleaseImage(&pIplImg);
        pIplImg = NULL;
    }
    BitmapData bmpData;
    Rect rect(0,0,pBitmap->GetWidth(),pBitmap->GetHeight());
    pBitmap->LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &bmpData);
    IplImage* tempImg = cvCreateImage(cvSize(pBitmap->GetWidth(), pBitmap->GetHeight()), IPL_DEPTH_8U, 3);
    BYTE* temp = (bmpData.Stride>0)?((BYTE*)bmpData.Scan0):((BYTE*)bmpData.Scan0+bmpData.Stride*(bmpData.Height-1));
    memcpy(tempImg->imageData, temp, abs(bmpData.Stride)*bmpData.Height);
    pBitmap->UnlockBits(&bmpData);
    pIplImg = tempImg;
//判断Top-Down or Bottom-Up

    if (bmpData.Stride<0)       

        cvFlip(pIplImg, pIplImg);           
}
OpenCV中 IplImage 转 Gdiplus::Bitmap

// pBitmap 同样需要外部释放!!

void CGeneral::IplImageToBitmap(IplImage* pIplImg, Bitmap* &pBitmap)
{
    if(!pIplImg)
        return;       

    BITMAPINFOHEADER bmih;
    memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
    bmih.biSize = sizeof(BITMAPINFOHEADER);
    bmih.biWidth = pIplImg->width;
    bmih.biHeight = pIplImg->height;
    bmih.biPlanes = 1;
    bmih.biBitCount = pIplImg->depth*pIplImg->nChannels;
    bmih.biSizeImage = pIplImg->imageSize;

    BYTE* pData=new BYTE[bmih.biSizeImage];
    memcpy(pData, pIplImg->imageDataOrigin, pIplImg->imageSize);

    if (pBitmap)
    {
        delete pBitmap;
        pBitmap = NULL;
    }
    pBitmap = Gdiplus::Bitmap::FromBITMAPINFO((BITMAPINFO*)&bmih, pData);

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jtujtujtu/archive/2009/01/08/3734722.aspx

posted on 2009-11-30 22:37 Hali 阅读(1861) 评论(0)  编辑 收藏 引用 所属分类: OpenCV
只有注册用户登录后才能发表评论。

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔分类

随笔档案

休闲链接

学习链接

牛人博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜