作业帮 > 综合 > 作业

帮帮我 我不知道分水岭算法在图像分割中的应用…… 代码,还有别的帮帮我

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/26 06:49:57
帮帮我 我不知道分水岭算法在图像分割中的应用…… 代码,还有别的帮帮我
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;
figure
subplot(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; %从原图对分水岭进行观察
figure
subplot(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);