三种方法进行分水岭分割
2015-04-15 08:51
267 查看
clear,clc%三种方法进行分水岭分割
%读入图像
filename='sar1.bmp';
f=imread(filename);
Info=imfinfo(filename);
if Info.BitDepth>8
f=rgb2gray(f);
end
figure,
mesh(double(f));%显示图像,类似集水盆地
%方法1:一般分水岭分割,从结果可以看出存在过分割问题
b=im2bw(f,graythresh(f));%二值化,注意应保证集水盆地的值较低(为0),否则就要对b取反
d=bwdist(b); %求零值到最近非零值的距离,即集水盆地到分水岭的距离
l=watershed(-d); %matlab自带分水岭算法,l中的零值即为风水岭
w=l==0; %取出边缘
g=b&~w; %用w作为mask从二值图像中取值
figure
subplot(2,3,1),
imshow(f);
subplot(2,3,2),
imshow(b);
subplot(2,3,3),
imshow(d);
subplot(2,3,4),
imshow(l);
subplot(2,3,5),
imshow(w);
subplot(2,3,6),
imshow(g);
%方法2:使用梯度的两次分水岭分割,从结果可以看出还存在过分割问题(在方法1的基础上改进)
h=fspecial('sobel');%获得纵方向的sobel算子
fd=double(f);
g=sqrt(imfilter(fd,h,'replicate').^2+imfilter(fd,h','replicate').^2);%使用sobel算子进行梯度运算
l=watershed(g);%分水岭运算
wr=l==0;
g2=imclose(imopen(g,ones(3,3)),ones(3,3));%进行开闭运算对图像进行平滑
l2=watershed(g2);%再次进行分水岭运算
wr2=l2==0;f2=f;
f2(wr2)=255;
figuresubplot(2,3,1),
imshow(f);
subplot(2,3,2),imshow(g);
subplot(2,3,3),imshow(l);
subplot(2,3,4),imshow(g2);
subplot(2,3,5),imshow(l2);
subplot(2,3,6),imshow(f2);
%方法3:使用梯度加掩模的三次分水岭算法(在方法2的基础上改进)
h=fspecial('sobel');%获得纵方向的sobel算子
fd=double(f);
g=sqrt(imfilter(fd,h,'replicate').^2+imfilter(fd,h','replicate').^2);%使用sobel算子进行梯度运算
l=watershed(g);%分水岭运算
wr=l==0;
rm=imregionalmin(g); %计算图像的区域最小值定位,该函数仅仅是用来观察为何分水岭算法产生这么多集水盆地
im=imextendedmin(f,2);%上面仅是产生最小值点,而该函数则是得到最小值附近的区域,此处的附近是相差2的区域
fim=f;
fim(im)=175; %将im在原图上标识出,用以观察
lim=watershed(bwdist(im));%再次分水岭计算
em=lim==0;
g2=imimposemin(g,im|em);%在梯度图上标出im和em,im是集水盆地的中心,em是分水岭
l2=watershed(g2); %第三次分水岭计算
f2=f;f2(l2==0)=255; %从原图对分水岭进行观察
figuresubplot(3,3,1),imshow(f);
subplot(3,3,2),imshow(g);
subplot(3,3,3),imshow(l);
subplot(3,3,4),imshow(im);
subplot(3,3,5),imshow(fim);
subplot(3,3,6),imshow(lim);
subplot(3,3,7),imshow(g2);
subplot(3,3,8),imshow(l2);
subplot(3,3,9),imshow(f2);
以上是分水岭的算法,这是摘抄自百度知道的里的回复,感觉挺好,就年过来了。摘抄地
%读入图像
filename='sar1.bmp';
f=imread(filename);
Info=imfinfo(filename);
if Info.BitDepth>8
f=rgb2gray(f);
end
figure,
mesh(double(f));%显示图像,类似集水盆地
%方法1:一般分水岭分割,从结果可以看出存在过分割问题
b=im2bw(f,graythresh(f));%二值化,注意应保证集水盆地的值较低(为0),否则就要对b取反
d=bwdist(b); %求零值到最近非零值的距离,即集水盆地到分水岭的距离
l=watershed(-d); %matlab自带分水岭算法,l中的零值即为风水岭
w=l==0; %取出边缘
g=b&~w; %用w作为mask从二值图像中取值
figure
subplot(2,3,1),
imshow(f);
subplot(2,3,2),
imshow(b);
subplot(2,3,3),
imshow(d);
subplot(2,3,4),
imshow(l);
subplot(2,3,5),
imshow(w);
subplot(2,3,6),
imshow(g);
%方法2:使用梯度的两次分水岭分割,从结果可以看出还存在过分割问题(在方法1的基础上改进)
h=fspecial('sobel');%获得纵方向的sobel算子
fd=double(f);
g=sqrt(imfilter(fd,h,'replicate').^2+imfilter(fd,h','replicate').^2);%使用sobel算子进行梯度运算
l=watershed(g);%分水岭运算
wr=l==0;
g2=imclose(imopen(g,ones(3,3)),ones(3,3));%进行开闭运算对图像进行平滑
l2=watershed(g2);%再次进行分水岭运算
wr2=l2==0;f2=f;
f2(wr2)=255;
figuresubplot(2,3,1),
imshow(f);
subplot(2,3,2),imshow(g);
subplot(2,3,3),imshow(l);
subplot(2,3,4),imshow(g2);
subplot(2,3,5),imshow(l2);
subplot(2,3,6),imshow(f2);
%方法3:使用梯度加掩模的三次分水岭算法(在方法2的基础上改进)
h=fspecial('sobel');%获得纵方向的sobel算子
fd=double(f);
g=sqrt(imfilter(fd,h,'replicate').^2+imfilter(fd,h','replicate').^2);%使用sobel算子进行梯度运算
l=watershed(g);%分水岭运算
wr=l==0;
rm=imregionalmin(g); %计算图像的区域最小值定位,该函数仅仅是用来观察为何分水岭算法产生这么多集水盆地
im=imextendedmin(f,2);%上面仅是产生最小值点,而该函数则是得到最小值附近的区域,此处的附近是相差2的区域
fim=f;
fim(im)=175; %将im在原图上标识出,用以观察
lim=watershed(bwdist(im));%再次分水岭计算
em=lim==0;
g2=imimposemin(g,im|em);%在梯度图上标出im和em,im是集水盆地的中心,em是分水岭
l2=watershed(g2); %第三次分水岭计算
f2=f;f2(l2==0)=255; %从原图对分水岭进行观察
figuresubplot(3,3,1),imshow(f);
subplot(3,3,2),imshow(g);
subplot(3,3,3),imshow(l);
subplot(3,3,4),imshow(im);
subplot(3,3,5),imshow(fim);
subplot(3,3,6),imshow(lim);
subplot(3,3,7),imshow(g2);
subplot(3,3,8),imshow(l2);
subplot(3,3,9),imshow(f2);
以上是分水岭的算法,这是摘抄自百度知道的里的回复,感觉挺好,就年过来了。摘抄地
相关文章推荐
- Shell中字符串分割的三种方法
- 将PDF文件进行分割拆分的方法
- 简述c#中对字符串进行分割的几种方法
- 进行DLL注入的三种方法
- 字符串根据多个字符进行分割的一种方法(使用split而不是regex.split更不是replace)
- 进行DLL注入的三种方法
- core的三种生成方法 和 使用core进行gdb调试的方法
- struts2使用json与前台进行ajax通信的三种方法
- Shell中字符串分割的三种方法
- 三种字符串分割方法(C/C++)
- 三种对list排序的方法,并实现动态传递字段名对list进行排序
- 分水岭分割方法
- 进行DLL注入的三种方法
- 分水岭分割方法
- C#使用itextsharp对pdf文件进行分割的几种方法
- Java反射机制(获取Class对象的三种方式+获取Class中的构造函数进行对象的初始化+获取反射类的字段+获取反射类的一般方法)
- C# 对字符串进行分离 split 字符串分割 "aaaa---bbbb---ccc"方法
- 进行DLL注入的三种方法
- 进行DLL注入的三种方法
- QString按照某个标志进行分割的方法