代码

论坛 期权论坛 期权     
家望   2019-6-7 07:50   2514   0
rgb=imread('pears.png');%读取原图像
I=rgb2gray(rgb);%转化为灰度图像
figure;subplot(121),imshow(I);%显示灰度图像
text(732,501,'Imagecourtesof  corel','Fontsize',7,'HorizontalAlignment','right');
hx=[1,2,1;0,0,0;-1,-2,-1];
hy=hx';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('gradientmagnitude (gradmag)');



%直接使用灰度梯度值进行分水岭算法
L=watershed(gradmag);%直接应用分水岭算法
Lrgb=label2rgb(L);figure,imshow(Lrgb);title('watershedtransform of gradient magnitude(Lrgb)');




%分别对前景和背景进行标记
se =strel('disk', 20);%图形结构元素
Io =imopen(I, se);%形态学开运算
figure;subplot(121),
imshow(Io),%显示开运算后的图像
title('Opening(Io)');


Ie =imerode(I, se);%对图像进行腐蚀
Iobr =imreconstruct(Ie, I);%形态学重建
subplot(122);imshow(Iobr), %显示重建后的图像
title('Opening-by-reconstruction(Iobr)');
Ioc =imclose(Io, se);%形态学闭运算
figure;subplot(121)
imshow(Ioc),%显示闭运算后的图像
title('Opening-closing(Ioc)');


Iobrd= imdilate(Iobr, se);%对图像进行膨胀
Iobrcbr= imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));%形态学重建
Iobrcbr= imcomplement(Iobrcbr);%图像求反
subplot(122);imshow(Iobrcbr), %显示重建求反后的的图像
title('Opening-closingby reconstruction (Iobrcbr)');
fgm =imregionalmax(Iobrcbr);%局部极大值
figure;imshow(fgm), %显示重建后局部极大值图像
title('Regionalmaxima of opening-closing by reconstruction (fgm)');


I2 =I;
I2(fgm)= 255;%局部极大值处像素值设置为255
figure;imshow(I2), %在原图上显示极大值区域
title('Regionalmaxima superimposed on original image (I2)');
se2 =strel(ones(5,5));%结构元素
fgm2 =imclose(fgm, se2);%闭运算
fgm3 =imerode(fgm2, se2);%腐蚀
fgm4 =bwareaopen(fgm3, 20);%开操作


I3 =I;
I3(fgm4)= 255;%前景处设置为255
figure;subplot(121),
imshow(I3)%显示修改后的极大值区域
title('Modifiedregional maxima');
bw =im2bw(Iobrcbr, graythresh(Iobrcbr));%转化为二值图像
subplot(122);imshow(bw), %显示二值图像
title('Thresholdedopening-closing by reconstruction');




%进行分水岭变换并显示
D =bwdist(bw);%计算距离
DL =watershed(D);%分水岭变换
bgm =DL == 0;%求取分割边界
figure;imshow(bgm), %显示分割后的边界
title('Watershedridge lines (bgm)')
gradmag2= imimposemin(gradmag, bgm | fgm4);%设置最小值
L =watershed(gradmag2);%分水岭变换


I4 =I;
I4(imdilate(L== 0, ones(3, 3)) | bgm | fgm4) = 255;% 前景及边界处设置为255
figure;subplot(121),
imshow(I4)%突出前景及边界
title('Markersand object boundaries');


Lrgb =label2rgb(L, 'jet', 'w', 'shuffle');%转化为伪彩色图像
subplot(122);imshow(Lrgb) ;%在原图上显示伪彩色图像
title('Coloredwatershed label matrix')
figure;imshow(I),
holdon;
himage= imshow(Lrgb); %
set(himage,'AlphaData', 0.3);
title('Lrgbsuperimposed transparently on original image');



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:5
帖子:1
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP