腐蚀:对一幅图像取位移再求交得到,用于从一幅图像中去除小而无用的目标
膨胀:对一幅图像取位移再求并得到,连接图像中的断续点和填补图像中的空洞。
腐蚀与膨胀不是逆运算,因为由腐蚀得到的图像不是都能通过膨胀复原
开启:先腐蚀在膨胀 可看作是腐蚀图像的膨胀恢复
闭合:先膨胀在腐蚀 可看作是膨胀图像的腐蚀恢复
//腐蚀OR膨胀数组
const KERNEL kernel_erosion[]=
{
//垂直方向
{0,1,0,
0,1,0,
0,1,0,
1,3
},
//水平方向
{0,0,0,
1,1,1,
0,0,0,
1,3
},
//十方向
{0,1,0,
1,1,1,
0,1,0,
1,3
},
//8方向
{1,1,1,
1,1,1,
1,1,1,
1,3
}
};
腐蚀代码:
/*************************************************************************
* 函数名称:Erosion()
* 参数:BYTE* bmp,LONG width,LONG height ------图像参数
KERNEL array 腐蚀方向数组
* 返回值:无
* 说明:对图像进行不同方向的腐蚀处理
************************************************************************/
voidErosion(BYTE *bmp,LONG width,LONG height,KERNEL array);
voidMyProcess::Erosion(BYTE *bmp,LONG width,LONG height,KERNEL kernel)
{
LONG i,j,k,m;//循环变量
LONG door=128;//像素阈值
BYTE *temp_bmp =new BYTE[(width+6)*(height+2)];
BmpFilter(temp_bmp,bmp,width,height);
//腐蚀处理,取各方向最大值
for(i=1;i<height+1;i++)
for(j=3;j<width+3;j++)
{
for(k=-1;k<kernel.Dimention-1;k++)
for(m=-1;m<kernel.Dimention-1;m++)
{
int a = temp_bmp[(i+k)*(width+6)+j+m*3];
int b = kernel.Element[k+1][m+1];
if(a*b>door) bmp[(i-1)*width+j-3]=255;
}
}
delete[] temp_bmp;
}
膨胀代码:
/*************************************************************************
* 函数名称:Dilation()
* 参数:BYTE* bmp,LONG width,LONG height ------图像参数
KERNEL array 膨胀方向数组
* 返回值:无
* 说明:对图像进行不同方向的膨胀处理
************************************************************************/
voidDilation(BYTE *bmp,LONG width,LONG height,KERNEL array);
voidMyProcess::Dilation(BYTE *bmp,LONG width,LONG height,KERNEL kernel)
{
LONG i,j,k,m;//循环变量
LONG door=128;//像素阈值
BYTE *temp_bmp =new BYTE[(width+6)*(height+2)];
BmpFilter(temp_bmp,bmp,width,height);
//膨胀处理,取各方向最小值
for(i=1;i<height+1;i++)
for(j=3;j<width+3;j++)
{
for(k=-1;k<kernel.Dimention-1;k++)
for(m=-1;m<kernel.Dimention-1;m++)
{
int a = temp_bmp[(i+k)*(width+6)+j+m*3];
int b = kernel.Element[k+1][m+1];
if(a*b<=door) bmp[(i-1)*width+j-3]=0;
}
}
delete[] temp_bmp;
}