分水岭算法 matlab的三种实现方法

本文转自:

http://blog.csdn.net/caiye917015406/article/details/7726628

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);

以上是分水岭的算法,这是摘抄自百度知道的里的回复,感觉挺好,就年过来了。摘抄地址http://zhidao.baidu.com/question/254301763.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值