形态学图像处理-腐蚀和膨胀

腐蚀:

      把结构元素S 平移x 后得到Sx ,若Sx 包含于X ,我们记下这个x 点,所有满足上述条件的x 点组成的集合称做X 被S 腐蚀(Erosion)的结果。用公式表示为:

         未命名

      腐蚀的方法是,拿S 的原点和X上的点一个一个地对比,如果S 上的所有点都在X 的范围内,则S 的原点对应的点保留,否则将该点去掉。以下是详细的代码,并且附上测试图片:

#include 
#include 
#include 
#include 
#include

unsigned char **get_matrix_space(int m,int n) 

int i; 
unsigned char **a; 
a=(unsigned char **)calloc(m,sizeof(unsigned char *)); 
for(i=0;ireturn a; 
}

main(){ 
FILE *fs,*fd; 
unsigned char c1,c2,**ps,**pd,**get_matrix_space(int,int); 
int width,height,L,i,j,l,m,match; 
    if((fs= fopen("sourcea.pgm","rb")) ==NULL){ 
        printf("can't open %s/n","source.pgm"); 
        exit(1); 
    } 
    if((fd = fopen("destination.pgm","wb")) ==NULL){ 
        printf("can't open %s/n","destination.pgm"); 
        exit(1); 
    }

fscanf(fs,"%c%c/n%d%d/n%d/n",&c1,&c2,&width,&height,&L); 
ps=get_matrix_space(height,width); 
pd=get_matrix_space(height,width);

for(i=0;i    for(j=0;j        fread(&ps[i][j],sizeof(unsigned char),1,fs); 
        pd[i][j] = 0; 
    } 
}

/// 
match = 1; 
for(i=0;i    for(j=0;j        for(m=i;m            for(l=j;l                if(ps[m][l]!=255){ 
                    match = 0; 
                } 
            } 
        } 
        if(match != 0){ 
            pd[i][j]=255; 
        } 
        match = 1; 
    } 

// 
fprintf(fd,"%c%c/n%d %d/n%d/n",'P','5',width,height,L); 
for(i=0;i    for(j=0;j        fwrite(&pd[i][j],sizeof(unsigned char),1,fd); 
    } 

}

source destination

      膨胀:

      把结构元素S 平移x 后得到Sx ,若Sx X 相交不为空,我们记下这个x 点,所有满足上述条件的x 点组成的集合称做X 被S 膨胀((dilation))的结果。用公式表示为:

     未命名

      膨胀的方法是,拿S 的原点和X 上的点一个一个地对比,如果S 上有一个点落在X 的范围内,则S 的原点对应的点就为图像。以下是详细的代码,并且附上测试图片:

#include 
#include 
#include 
#include 
#include

unsigned char **get_matrix_space(int m,int n) 

int i; 
unsigned char **a;

a=(unsigned char **)calloc(m,sizeof(unsigned char *)); 
for(i=0;ireturn a; 
}

main(){ 
FILE *fs,*fd; 
unsigned char c1,c2,**ps,**pd,**v,**get_matrix_space(int,int); 
int width,height,L,i,j,l,m,windowSize,match; 
    if((fs= fopen("source.pgm","rb")) ==NULL){ 
        printf("can't open %s/n","source.pgm"); 
        exit(1); 
    } 
    if((fd = fopen("destination.pgm","wb")) ==NULL){ 
        printf("can't open %s/n","destination.pgm"); 
        exit(1); 
    }

fscanf(fs,"%c%c/n%d%d/n%d/n",&c1,&c2,&width,&height,&L); 
ps=get_matrix_space(height,width); 
pd=get_matrix_space(height,width);

for(i=0;i    for(j=0;j        fread(&ps[i][j],sizeof(unsigned char),1,fs); 
    } 

match = 0; 
// 
//用2x2方块作为膨胀的结构元素。 
for(i=0;i    for(j=0;j        for(l=i;l            for(m=j;m                if(ps[l][m]==255){ 
                    match = 1; 
                } 
            } 
        } 
        if(match == 1){ 
            pd[i][j] = 255; 
            match = 0; 
        } 
    } 

// 
fprintf(fd,"%c%c/n%d %d/n%d/n",'P','5',width,height,L); 
for(i=0;i    for(j=0;j        fwrite(&pd[i][j],sizeof(unsigned char),1,fd); 
    } 

}

source destination

参考文献:http://yzhkch.ecit.edu.cn/jsjtxx/html/image_6_1.htm

                 http://blog.csdn.net/vincentzhao2009/article/details/4723469


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值