%读取图像并求其边界
I=imread('pout.tif');%读取原图像
figure;subplot(121);
imshow(I);title('原图');
text(732,501,'Image courtesy of Corel',...
'FontSize',7,'HorizontalAlignment','right');
hy=fspecial('sobel');%soble算子
hx=hy';
Iy=imfilter(double(I),hy,'replicate');%滤波求y方向边缘
Ix=imfilter(double(I),hx,'replicate');%滤波求x方向边缘
gradmag=sqrt(Ix.^2+Iy.^2);%求模
subplot(122);imshow(gradmag,[]);%显示梯度,title('Gradient magnitude (gradmag)')
%直接使用梯度模值进行分水岭算法
L=watershed(gradmag);
figure;imshow(L);%显示分割后的图像
title('Watershed transform of gradient magnitude (L)');
%直接使用梯度模值图像进行分水岭算法得到的结果往往会存在过分割的现象,因此通常需要分别对前景对象和背景对象进行标记,以获得更好哦的分割效果。
%分别对前景和背景进行标记
se=strel('disk',20);%圆形结构元素
Io=imopen(I,se);%形态学开操作
figure;subplot(121);
imshow(Io);%显示执行开操作后的图像
title('Opening (Io)');
Ie=imerode(I,se);%对图像进行腐蚀
Iobr=imreconstruct(Ie,I);%形态学重建
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余2页未读,立即下载