1、数学形态学的腐蚀和膨胀运算 "形态学"是描述动植物形态和结构的一门生物学分支,这里借用来指一种图像处理的方法.图像处理的形态学方法是一种"数学形态学"方法,用来提取图像成分,并据此来对图像区域的形状进行表示和描述.它的数学语言是集合理论,其中的集合代表图像中物体的形状.图像处理中常用的数学形态学方法包括腐蚀,膨胀,开,闭,边缘提取和图像细化.我依次给出Visual C++源代码: /************************************************************************* * * ErosionDIB() * *
2、Parameters: * * HDIB hDib - objective DIB handle * BOOL bHori - erosion direction * * Return Value: * * BOOL - True is success, else False * * Description: * * This function do erosion with the specified direction * ***************************************
3、/ BOOL ErosionDIB(HDIB hDib, BOOL bHori) { // start wait cursor WaitCursorBegin(); // Old DIB buffer if (hDib == NULL) { WaitCursorEnd(); return FALSE; } // only support 256 color image WORD wBitCount = DIBBitCount(hDib); if (wBitCount !=
4、8) { WaitCursorEnd(); return FALSE; } // new DIB HDIB hNewDIB = CopyHandle(hDib); if (! hNewDIB) { WaitCursorEnd(); return FALSE; } // source dib buffer LPBITMAPINFO lpSrcDIB = (LPBITMAPINFO)GlobalLock(hDib); if (! lpSrcDIB) { WaitCursorBegin(); return FA
5、LSE; } // New DIB buffer LPBITMAPINFO lpbmi = (LPBITMAPINFO)GlobalLock(hNewDIB); if (! lpbmi) { WaitCursorBegin(); return FALSE; } // start erosion... LPSTR lpPtr; LPSTR lpTempPtr; LONG x,y; BYTE num, num0; int i; LONG lHeight = DIBHeight(lpSrcDIB); LONG lWidth = DIBW
6、idth(lpSrcDIB);
DWORD dwBufferSize = GlobalSize(lpSrcDIB);
int nLineBytes = BytesPerLine(lpSrcDIB);
if(bHori)
{
for (y=0; y 7、1; x 8、 for(i=0;i<3;i++)
{
num=(unsigned char)*(lpPtr+i-1);
if(num==255)
{
*lpTempPtr=(unsigned char)255;
break;
}
}
}
else
*lpTempPtr=(unsigned char)255;
*/
lpPtr++;
lpTempPtr++;
}
}
}
else // Vertical
{ 9、
for (y=1; y 10、 if(num > num0)
num0 = num;
}
*lpTempPtr=(unsigned char)num0;
/*
num=(unsigned char)*lpPtr;
if (num==0)
{
*lpTempPtr=(unsigned char)0;
for(i=0;i<3;i++)
{
num=(unsigned char)*(lpPtr+(i-1)*nLineBytes);
if(num==255)
{
11、 *lpTempPtr=(unsigned char)255;
break;
}
}
}
else
*lpTempPtr=(unsigned char)255;
*/
lpPtr++;
lpTempPtr++;
}
}
}
// cleanup
GlobalUnlock(hDib);
GlobalUnlock(hNewDIB);
GlobalFree(hNewDIB);
WaitCursorEnd();
return TRUE;
}
/*********** 12、
*
* DilationDIB()
*
* Parameters:
*
* HDIB hDib - objective DIB handle
* BOOL bHori - dilation direction
*
* Return Value:
*
* BOOL - True is success, else False
*
* Description:
*
* This 13、 function do dilation with the specified direction
*
************************************************************************/
BOOL DilationDIB(HDIB hDib, BOOL bHori)
{
// start wait cursor
WaitCursorBegin();
// Old DIB buffer
if (hDib == NULL)
{
WaitCursorEnd();
return FA 14、LSE;
}
// only support 256 color image
WORD wBitCount = DIBBitCount(hDib);
if (wBitCount != 8)
{
WaitCursorEnd();
return FALSE;
}
// new DIB
HDIB hNewDIB = CopyHandle(hDib);
if (! hNewDIB)
{
WaitCursorEnd();
return FALSE;
}
// source dib buffer
LPBITMAPINFO l 15、pSrcDIB = (LPBITMAPINFO)GlobalLock(hDib);
if (! lpSrcDIB)
{
WaitCursorBegin();
return FALSE;
}
// New DIB buffer
LPBITMAPINFO lpbmi = (LPBITMAPINFO)GlobalLock(hNewDIB);
if (! lpbmi)
{
WaitCursorBegin();
return FALSE;
}
// start erosion...
LPSTR lpPtr;
LPSTR lpTempPtr;
16、
LONG x,y;
BYTE num, num0;
int i;
LONG lHeight = DIBHeight(lpSrcDIB);
LONG lWidth = DIBWidth(lpSrcDIB);
DWORD dwBufferSize = GlobalSize(lpSrcDIB);
int nLineBytes = BytesPerLine(lpSrcDIB);
if(bHori)
{
for(y=0;y 17、ytes)+1;
lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes)+1;
for(x=1;x 18、 num=(unsigned char)*lpPtr;
if (num==255)
{
*lpTempPtr=(unsigned char)255;
for(i=0;i<3;i++)
{
num=(unsigned char)*(lpPtr+i-1);
if(num==0)
{
*lpTempPtr=(unsigned char)0;
break;
}
}
}
else
*lpTempPtr=(uns 19、igned char)0;
*/
lpPtr++;
lpTempPtr++;
}
}
}
else
{
for(y=1;y 20、
for(i=0;i<3;i++)
{
num=(unsigned char)*(lpPtr+i-1);
if(num < num0)
num0 = num;
}
*lpTempPtr=(unsigned char)num0;
/*
num=(unsigned char)*lpPtr;
if (num==255)
{
*lpTempPtr=(unsigned char)255;
for(i=0;i<3;i++)
{
num=(un 21、signed char)*(lpPtr+(i-1)*nLineBytes);
if(num==0)
{
*lpTempPtr=(unsigned char)0;
break;
}
}
}
else
*lpTempPtr=(unsigned char)0;
*/
lpPtr++;
lpTempPtr++;
}
}
}
// cleanup
GlobalUnlock(hDib);
GlobalUnlock(hNewDIB);
GlobalFree(hNewDIB);
WaitCursorEnd();
return TRUE;
}






