求Bitmap 图像的缩放算法

ichbinich 2003-09-17 05:07:12
求Bitmap 图像的缩放算法,最好能给个举例。先谢过。
...全文
172 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ichbinich 2003-09-22
  • 打赏
  • 举报
回复
多谢了!
HUNTON 2003-09-18
  • 打赏
  • 举报
回复
要算法?这里有讨论四边形到四边形的变换,我在里面介绍了两种方法,你看看是不是你想要的:http://expert.csdn.net/Expert/topic/2077/2077097.xml?temp=.8471186
knightxie 2003-09-18
  • 打赏
  • 举报
回复
用插值将即可解决,否则图像会失真。
ichbinich 2003-09-17
  • 打赏
  • 举报
回复
缩小后的图象象素值为对应原图象 多个象素的中间值。 //
依照中间值得出的图像,会导致图像色彩失真吗?会导致图像变形吗?
Freshyy 2003-09-17
  • 打赏
  • 举报
回复
明白你的意思!
你是要缩放的图形学算法(原理),是吧!

原图象I(x,y)在x,y坐标袖的缩放比例 分别sx和sy,则变换后为f(x′,y′),则图象缩放后的图象象素f(x′,y′)=I(sxx,syy)。
图象的缩小实际上是一种“多对一”的映 射变换。缩放比如为整数倍,缩小后的图象象素值为对应原图象 多个象素的中间值。
  图象的放大实际上是一种“一对多”的映射变换。如放大倍数 为整数倍,放大后的一个图象象素就对应多个象素,而实际上往往 要利用该象素与相邻象素之间的灰度值以插值方式算出。
ichbinich 2003-09-17
  • 打赏
  • 举报
回复
谢谢你详尽的代码和介绍。
没理解错的话,你是用 StretchBlt( 。。。); 来实现图像缩放的,但我真正想知道的是这个函数其自身的算法,或者说,用什么一个算法可以实现这个函数,或者进一步说,不用这个函数,我用什么算法可以实现图像的缩放。
Freshyy 2003-09-17
  • 打赏
  • 举报
回复
给你提供一个算法:
HDIB ChangeDIBSize(HDIB hDIB, int nWidth, int nHeight)
{
LPBITMAPINFO lpbmi = NULL;
LPBYTE lpSourceBits, lpTargetBits, lpResult;
HDC hDC = NULL, hSourceDC, hTargetDC;
HBITMAP hSourceBitmap, hTargetBitmap, hOldTargetBitmap, hOldSourceBitmap;
DWORD dwSourceBitsSize, dwTargetBitsSize, dwTargetHeaderSize;
HDIB hNewDIB;
DWORD dwSize;

WaitCursorBegin();

// Get DIB pointer
if (! hDIB)
{
WaitCursorEnd();
return NULL;
}
LPBITMAPINFO lpSrcDIB = (LPBITMAPINFO)GlobalLock(hDIB);
if (! lpSrcDIB)
{
WaitCursorEnd();
return NULL;
}

// Allocate and fill out a BITMAPINFO struct for the new DIB
dwTargetHeaderSize = sizeof( BITMAPINFOHEADER ) + PaletteSize(lpSrcDIB);
lpbmi = (LPBITMAPINFO)malloc( dwTargetHeaderSize );
memcpy(lpbmi, lpSrcDIB, dwTargetHeaderSize);
lpbmi->bmiHeader.biWidth = nWidth;
lpbmi->bmiHeader.biHeight = nHeight;

// Gonna use DIBSections and BitBlt() to do the conversion, so make 'em
hDC = GetDC( NULL );
hTargetBitmap = CreateDIBSection( hDC, lpbmi, DIB_RGB_COLORS, (VOID **)&lpTargetBits, NULL, 0 );
hSourceBitmap = CreateDIBSection( hDC, lpSrcDIB, DIB_RGB_COLORS, (VOID **)&lpSourceBits, NULL, 0 );
hSourceDC = CreateCompatibleDC( hDC );
hTargetDC = CreateCompatibleDC( hDC );

// Flip the bits on the source DIBSection to match the source DIB
dwSourceBitsSize = lpSrcDIB->bmiHeader.biHeight * BytesPerLine((LPBYTE)&(lpSrcDIB->bmiHeader));
dwTargetBitsSize = lpbmi->bmiHeader.biHeight * BytesPerLine((LPBYTE)&(lpbmi->bmiHeader));
memcpy( lpSourceBits, FindDIBBits((LPBYTE)lpSrcDIB), dwSourceBitsSize );
lpbmi->bmiHeader.biSizeImage = dwTargetBitsSize;

// Select DIBSections into DCs
hOldSourceBitmap = (HBITMAP)SelectObject( hSourceDC, hSourceBitmap );
hOldTargetBitmap = (HBITMAP)SelectObject( hTargetDC, hTargetBitmap );

// put old bitmap in new bitmap
SetStretchBltMode( hTargetDC, COLORONCOLOR );
StretchBlt( hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, lpSrcDIB->bmiHeader.biWidth, lpSrcDIB->bmiHeader.biHeight, SRCCOPY );

// Clean up and delete the DCs
SelectObject( hSourceDC, hOldSourceBitmap );
SelectObject( hTargetDC, hOldTargetBitmap );
DeleteDC( hSourceDC );
DeleteDC( hTargetDC );
ReleaseDC( NULL, hDC );

// Flush the GDI batch, so we can play with the bits
GdiFlush();

// Allocate enough memory for the new CF_DIB, and copy bits
dwSize = dwTargetHeaderSize + dwTargetBitsSize;
hNewDIB = GlobalAlloc(GHND, dwSize);
lpResult = (LPBYTE)GlobalLock(hNewDIB);//malloc( dwTargetHeaderSize + dwTargetBitsSize );
memcpy( lpResult, lpbmi, dwTargetHeaderSize );
memcpy( FindDIBBits( (LPBYTE)lpResult ), lpTargetBits, dwTargetBitsSize );

// final cleanup
DeleteObject( hTargetBitmap );
DeleteObject( hSourceBitmap );
free( lpbmi );
GlobalUnlock(hDIB);
GlobalUnlock(hNewDIB);
WaitCursorEnd();

return hNewDIB;
}

其中调用了三个非系统函数分别如下:
LPBYTE FindDIBBits(LPBYTE lpDIB)
{
//This function calculates the address of the DIB's bits and returns a
//pointer to the DIB bits.

return (lpDIB + *(LPDWORD)lpDIB + PaletteSize(lpDIB));
}

WORD PaletteSize(LPBYTE lpDIB)
{
// calculate the size required by the palette
if (IS_WIN30_DIB (lpDIB))
return (DIBNumColors(lpDIB) * sizeof(RGBQUAD));
else
return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE));
}

WORD DIBNumColors(LPBYTE lpDIB)
{
WORD wBitCount; // DIB bit count

// If this is a Windows-style DIB, the number of colors in the
// color table can be less than the number of bits per pixel
// allows for (i.e. lpbi->biClrUsed can be set to some value).
// If this is the case, return the appropriate value.


if (IS_WIN30_DIB(lpDIB))
{
DWORD dwClrUsed;

dwClrUsed = ((LPBITMAPINFOHEADER)lpDIB)->biClrUsed;
if (dwClrUsed)

return (WORD)dwClrUsed;
}

// Calculate the number of colors in the color table based on
// the number of bits per pixel for the DIB.

if (IS_WIN30_DIB(lpDIB))
wBitCount = ((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
else
wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;

// return number of colors based on bits per pixel

switch (wBitCount)
{
case 1:
return 2;

case 4:
return 16;

case 8:
return 256;

default:
return 0;
}
}
ichbinich 2003-09-17
  • 打赏
  • 举报
回复
拜托,说具体些行不行, 我只想知道算法,哪怕你说一个也好啊,我不在中国,买不到这本书。
Freshyy 2003-09-17
  • 打赏
  • 举报
回复
何斌,马天予,王运坚 等. Visual C++ 数字图像处理[M]. 北京:人民邮电出版社, 2001
中有例程,丛数学原理到算法实现,讲得很详细!

4,446

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧