形态学图像处理-腐蚀和膨胀
2011-12-20 21:41
375 查看
腐蚀:
把结构元素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);
}
}
}
膨胀:
把结构元素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);
}
}
}
参考文献:http://yzhkch.ecit.edu.cn/jsjtxx/html/image_6_1.htm
http://blog.csdn.net/vincentzhao2009/article/details/4723469
把结构元素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);
}
}
}
膨胀:
把结构元素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);
}
}
}
参考文献:http://yzhkch.ecit.edu.cn/jsjtxx/html/image_6_1.htm
http://blog.csdn.net/vincentzhao2009/article/details/4723469
相关文章推荐
- OpenCV之十 形态学图像处理(一):膨胀与腐蚀
- 形态学图像处理-腐蚀和膨胀
- Matlab 图像处理 形态学 腐蚀 膨胀 开闭运算 连通分量
- 图像处理 形态学 (腐蚀 膨胀 开闭运算 连通分量....)
- 图像处理【形态学】腐蚀与膨胀
- 图像处理之膨胀腐蚀操作
- Opencv--形态学图像处理--膨胀与腐蚀,开操作与闭操作
- 图像处理 腐蚀 膨胀 细化
- 【OpenCV入门教程之十】 形态学图像处理(一):膨胀与腐蚀
- Matlab 图像处理 形态学 腐蚀 膨胀 开闭运算 连通分量
- Opencv对图像做腐蚀和膨胀处理的结果
- OpenCV&图像处理_3:以腐蚀[eroding]和膨胀[dilating]为基础的形态学操作和图像金字塔[image pyramids]
- 图像处理——腐蚀、膨胀、开/闭运算、顶/黑帽
- 【OpenCV入门教程之十】 形态学图像处理(一):膨胀与腐蚀
- 形态学图像处理——图像膨胀&图像腐蚀
- opencv图像处理——形态学腐蚀膨胀练习示例代码
- 【OpenCV入门教程之十】 形态学图像处理(一):膨胀与腐蚀
- Javascript图像处理―图像形态学(膨胀与腐蚀)
- [学习笔记]形态学图像处理-膨胀与腐蚀
- python 简单图像处理(13) 二值图腐蚀和膨胀,开运算、闭运算