📄 形态学基本运算之c语言模拟.txt
字号:
形态学基本运算之C语言模拟[原创]
形态学原是对于动植物调查时采取的某种形式的研究。数学形态学是分析机和形状和结构的数学方法,它建立在集合代数的基础上,是用集合论方法定量描述集合结构的学科。
形态学的基础是集合论。其基本的运算子主要有腐蚀、膨胀、开、闭等。所谓膨胀一般指将与物体边界接触的背景像素合并到物体中的过程;简单的腐蚀运算将一个物体沿边界减少一个像素。先进行腐蚀运算再进行膨胀运算称作开运算;先膨胀再腐蚀称作闭运算。
利用开运算可以把只有少量像素相连的物体分离开,利用闭运算则可以填补物体中小的空洞。
下面的代码简单模拟了几个简单的形态学基本运算,不一定准确,欢迎同行专家给出批评意见。
#define M 15
#define N 15
void shousuo(int s[M][N])
{
int i,j;
int t[M][N];
for(i=0;i<M;i++)/*备份原数据*/
for(j=0;j<N;j++)
t[i][j]=s[i][j];
for(i=1;i<M-1;i++)/*处理内部元素*/
for(j=1;j<N-1;j++)
if(t[i][j-1]==0||t[i][j+1]==0||t[i-1][j]==0||t[i+1][j]==0)
s[i][j]=0;
for(i=1;i<M-1;i++)
{/*处理边界元素*/
if(t[i-1][0]==0||t[i+1][0]==0)
s[i][0]=0;
if(t[i-1][N-1]==0||t[i+1][N-1]==0)
s[i][N-1]=0;
}
for(j=0;j<N-1;j++)
{/*处理边界元素*/
if(t[0][j-1]==0||t[0][j+1]==0)
s[0][j]=0;
if(t[M-1][j-1]==0||t[M-1][j+1]==0)
s[M-1][j]=0;
}
if(t[0][1]==0||t[1][0]==0)/*以下几条语句处理四个角的元素*/
s[0][0]=0;
if(t[0][N-2]==0||t[1][N-1]==0)
s[0][N-1]=0;
if(t[M-2][0]==0||t[M-1][1]==0)
s[M-1][0]=0;
if(t[M-2][N-1]==0||t[M-1][N-2]==0)
s[M-1][N-1]=0;
}
void pengzhang(int s[M][N])
{
int i,j;
int t[M][N];
for(i=0;i<M;i++)
for(j=0;j<N;j++)
t[i][j]=s[i][j];
for(i=1;i<M-1;i++)
for(j=1;j<N-1;j++)
if(t[i][j-1]==1||t[i][j+1]==1||t[i-1][j]==1||t[i+1][j]==1)
s[i][j]=1;
for(i=1;i<M-1;i++)
{
if(t[i-1][0]==1||t[i+1][0]==1)
s[i][0]=1;
if(t[i-1][N-1]==1||t[i+1][N-1]==1)
s[i][N-1]=1;
}
for(j=0;j<N-1;j++)
{
if(t[0][j-1]==1||t[0][j+1]==1)
s[0][j]=1;
if(t[M-1][j-1]==1||t[M-1][j+1]==1)
s[M-1][j]=1;
}
if(t[0][1]==1||t[1][0]==1)
s[0][0]=1;
if(t[0][N-2]==1||t[1][N-1]==1)
s[0][N-1]=1;
if(t[M-2][0]==1||t[M-1][1]==1)
s[M-1][0]=1;
if(t[M-2][N-1]==1||t[M-1][N-2]==1)
s[M-1][N-1]=1;
}
void output(int array[M][N])
{
int i,j;
for(i=0;i<M;i++)
{
printf("\n");
for(j=0;j<N;j++)
if(array[i][j]==1)
printf("%2d",array[i][j]);
else
printf(" ");
}
}
void main()
{
int data[M][N]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},/*实验数据*/
{0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,1,1,0,1,1,0,0,0},
{0,0,0,0,0,0,0,1,1,0,1,1,0,0,0},
{0,0,1,1,1,0,0,1,1,1,1,1,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
int data1[M][N];
int i,j;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
data1[i][j]=data[i][j];
printf("\ndata:\n");
printf("\nThe original data is:\n");
output(data);
shousuo(data);
printf("\nAfter shousuo operation.\n");
output(data);
pengzhang(data);
printf("\nAfter pengzhang operation.\n");
output(data);
printf("\ndata1:\n");
printf("\nThe orignal data is:\n");
output(data1);
pengzhang(data1);
printf("\nAfter pengzhang operation.\n");
output(data1);
shousuo(data1);
printf("\nAfter shousuo operation.\n");
output(data1);
}
运行结果:
data:
The original data is:
1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1
1 1 1 1
1 1 1 1 1 1
1 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1
After shousuo operation.
1 1 1 1 1
1 1 1 1
1 1
1 1 1 1
1 1
1 1
1 1 1
After pengzhang operation.
1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1
1 1
1 1 1 1
1 1 1 1 1 1
1 1 1 1 1
1 1
1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1
data1:
The orignal data is:
1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1
1 1 1 1
1 1 1 1 1 1
1 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1
After pengzhang operation.
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
After shousuo operation.
1 1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -