资源描述
数学形态学的腐蚀和膨胀运算
"形态学"是描述动植物形态和结构的一门生物学分支,这里借用来指一种图像处理的方法.图像处理的形态学方法是一种"数学形态学"方法,用来提取图像成分,并据此来对图像区域的形状进行表示和描述.它的数学语言是集合理论,其中的集合代表图像中物体的形状.图像处理中常用的数学形态学方法包括腐蚀,膨胀,开,闭,边缘提取和图像细化.我依次给出Visual C++源代码:
/*************************************************************************
*
* ErosionDIB()
*
* 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
*
************************************************************************/
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 != 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 FALSE;
}
// 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 = DIBWidth(lpSrcDIB);
DWORD dwBufferSize = GlobalSize(lpSrcDIB);
int nLineBytes = BytesPerLine(lpSrcDIB);
if(bHori)
{
for (y=0; y<lHeight; y++)
{
lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes)+1;
lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes)+1;
for (x=1; x<lWidth-1; x++)
{
num0 = num = 0 ;
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==0)
{
*lpTempPtr=(unsigned char)0;
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
{
for (y=1; y<lHeight-1; y++)
{
lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes);
lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes);
for (x=0; x<lWidth; x++)
{
num0 = num = 0 ;
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==0)
{
*lpTempPtr=(unsigned char)0;
for(i=0;i<3;i++)
{
num=(unsigned char)*(lpPtr+(i-1)*nLineBytes);
if(num==255)
{
*lpTempPtr=(unsigned char)255;
break;
}
}
}
else
*lpTempPtr=(unsigned char)255;
*/
lpPtr++;
lpTempPtr++;
}
}
}
// cleanup
GlobalUnlock(hDib);
GlobalUnlock(hNewDIB);
GlobalFree(hNewDIB);
WaitCursorEnd();
return TRUE;
}
/*************************************************************************
*
* DilationDIB()
*
* Parameters:
*
* HDIB hDib - objective DIB handle
* BOOL bHori - dilation direction
*
* Return Value:
*
* BOOL - True is success, else False
*
* Description:
*
* This 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 FALSE;
}
// 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 lpSrcDIB = (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;
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<lHeight;y++)
{
lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes)+1;
lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes)+1;
for(x=1;x<lWidth-1;x++)
{
num0 = num = 255;
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=(unsigned char)*(lpPtr+i-1);
if(num==0)
{
*lpTempPtr=(unsigned char)0;
break;
}
}
}
else
*lpTempPtr=(unsigned char)0;
*/
lpPtr++;
lpTempPtr++;
}
}
}
else
{
for(y=1;y<lHeight-1;y++)
{
lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes);
lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes);
for(x=0;x<lWidth;x++)
{
num0 = num = 255;
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=(unsigned 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;
}
展开阅读全文