我是
靠谱客的博主
酷炫八宝粥,最近开发中收集的这篇文章主要介绍
DIB与IplImage图像格式的相互转换,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
DIB转换为IplImage:
DIB->bmp->IPLImage.
Void DIB2IplImage(CDIB m_dib)
{
/// DIB2CBitmap
unsigned char *pBits = (unsigned char *)malloc(m_dib.height*m_dib.bytes);
memcpy(pBits,m_dib.m_pBits,m_dib.height*m_dib.bytes);
CBitmap* bitMap = new CBitmap;
bitMap->CreateBitmap(m_dib.width,m_dib.height,1,m_dib.m_nBits,m_dib.m_pVoid);
free(pBits);
//
IplImage* img=CBitmap2IplImage(bitMap);
delete[] bitMap;
}
IplImage* CBitmap2IplImage(CBitmap* pBitmap)
{
HBITMAP hBmp = (HBITMAP)(*pBitmap);//
BITMAP bmp;
::GetBitmapBits(hBmp,sizeof(BITMAP),&bmp);
int nChannels = 3;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
IplImage* pImg = 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(pImg->imageData, pBuffer, bmp.bmHeight*bmp.bmWidth*nChannels);//将缓冲区信息复制给IplImage
img->imageData=(char*)pBuffer;
delete[] pBuffer;
return pImg;
}
IplImage转换为DIB:
/IplImage转化为Dib
Void IplImage2DIB(IplImage* pImg)
{
CBitmap* bmp=IplImage2CBitmap(pImg);//IplImage转化为CBitmap
CDIB dib;
HANDLE hDib=dib.BMPToDIB(bmp,BI_RGB,0);//CBitmap转化为hDib
if (m_dib.CreateFromHandle(hDib,m_dib.bytes))
pDoc->m_Dib=m_dib;
}
CBitmap* CFda_Dlg1::IplImage2CBitmap(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 CFda_Dlg1::FillBitmapInfo( BITMAPINFO* bmi,int width, int height,int bpp, intorigin)
{
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;
}
}
}
HANDLE CDIB::BMPToDIB( CBitmap* bitmap, DWORD dwCompression, CPalette* pPal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
ASSERT( bitmap->GetSafeHandle() );
if( dwCompression == BI_BITFIELDS )
return NULL;
hPal = (HPALETTE) pPal->GetSafeHandle();
if(hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
bitmap->GetObject(sizeof(bm),(LPSTR)&bm);
// 初始化位图头
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// 计算位图头和颜色表的大小
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// 从设备得到位图
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
//为位图头和颜色表分配足够的内存
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi;
GetDIBits(hDC, (HBITMAP)bitmap->GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
//从新分配缓冲区以便于可以保存所有数据
dwLen += bi.biSizeImage;
if(handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else
{
GlobalFree(hDIB);
// 从新选择调色板
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
//得到DIB位数
lpbi = (LPBITMAPINFOHEADER)hDIB;
BOOL bGotBits;
if(bi.biBitCount == 24)
{
bGotBits = bitmap->GetBitmapBits((DWORD)bi.biSizeImage,(LPBYTE)lpbi+bi.biSize);
}
else
{
//最终得到DIB
bGotBits = GetDIBits( hDC,
(HBITMAP)bitmap->GetSafeHandle(),
0L, // 开始扫描线
(DWORD)bi.biHeight, // 扫描线符
(LPBYTE)lpbi // 位图比特地址
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // 位图头地址
(DWORD)DIB_RGB_COLORS); // RGB颜色表
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}
}
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return hDIB;
}
详情请参考http://blog.sina.com.cn/s/blog_ad81d431010158r3.html
最后
以上就是酷炫八宝粥为你收集整理的DIB与IplImage图像格式的相互转换的全部内容,希望文章能够帮你解决DIB与IplImage图像格式的相互转换所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复