分水岭分割算法的实现

基于距离变换方式的分水岭分割算法
代码:

%-------------------------------------
%---Author:Richard Tseng
%---Date:2017.11.11
%---Topic:Water_dist 
%-------------------------------------
close all;
clc;
clear;

Image = imread('rice.png');
Bwimage = im2bw(Image,graythresh(Image));
gc = ~Bwimage;        
figure(1);
subplot(121);imshow(Bwimage);title('¶þֵͼÏñ');
subplot(122);imshow(gc);title('¶þֵͼÏñÈ¡·´');

D = bwdist(gc);   
L = watershed(~D); 
w = L == 0;          

path = '~D.txt';
out = im2matrix(path,~D);
figure(2);
subplot(121);imshow(D);title('¾àÀë±ä»»');
subplot(122);imshow(w);title('·ÖË®Áë¼¹Ïß');

g2 = Bwimage&(~w);
figure(3);
imshow(g2);title('·Ö¸îÖ®ºóµÄͼÏñ');

这里写图片描述
这里写图片描述

基于梯度的分水岭分割方法
代码:

%-------------------------------------
%---Author:Zeng FanWei
%---Date:2017.11.11
%---Topic:Water_gradient 
%-------------------------------------
close all;
clc;
clear;

Image = imread('rice.png');
h=fspecial('sobel');           
disp(h);
 fd = double(Image);
 gx = imfilter(fd,h','replicate');
 gy = imfilter(fd,h,'replicate');
 g = sqrt(gx.^2 + gy.^2);

 figure(1);
 subplot(121);imshow(Image);title('ԭͼ');
 subplot(122);imshow(uint8(g));title('ÌݶÈ');

 figure(2);
 subplot(121);imshow(uint8(gx));title('´¹Ö±±ßÔµ');
 subplot(122);imshow(uint8(gy));title('ˮƽ±ßÔµ');

 g1 = imopen(g,ones(3,3));  
 g2 = imclose(g1,ones(3,3));  
 figure(3);
 subplot(121);imshow(uint8(g1));title('¿ª²Ù×÷');
 subplot(122);imshow(uint8(g2));title('±Õ²Ù×÷');

 L = watershed(g);
 wr = L == 0;                       
 L1 = watershed(g2);
 wr1 = L1 == 0;
 figure(4);
 subplot(121);imshow(wr);title('¼¹Ïß');
 subplot(122);imshow(wr1);title('ÐÎ̬ѧÂ˲¨Ö®ºóµÄ¼¹Ïß');

 outimage = Image;
 outimage(wr1) = 255;     %½«¼¹Ïß¼Óµ½Ô­Ê¼µÄ»Ò¶ÈͼÏñÉÏ

 rm = imregionalmin(g);   

 figure(5);
 subplot(121);imshow(outimage);title('Ìݶȷ½·¨·Ö¸î½á¹û1');
 subplot(122);imshow(rm);title('ÌݶÈͼÏñÉϾֲ¿Ð¡ÇøÓòλÖÃ');

  im = imextendedmin(Image,7);   
 out1 = Image;
 out1(im) = 0;                 
im=imopen(imclose(im,ones(3,3)),ones(3,3));      Lim  = watershed(bwdist(im));   
 em = Lim == 0;                  

 figure(6);
 subplot(121);imshow(im);title('½ÏÉîµÄ¾Ö²¿Ð¡ÇøÓò');
subplot(122);imshow(em);title('¼¹ÏßÍⲿ±ê¼Ç·û'); 

 Bwimage = im2bw(Image,graythresh(Image));
 out3 = Bwimage&(~em);
 figure;
 subplot(121);imshow(Bwimage);title('¶þֵͼÏñ');
 subplot(122);imshow(out3);title('½ÏÉî¾Ö²¿×îСֵλÖ÷ָî½á¹û');


 mp = imimposemin(g,im|em);
 L2 = watershed(mp);
 out2 = Image;
 out2(L2 == 0) = 255;

 figure;
 imshow(out2);title('×îС¸²¸ÇÌݶȸĽø');

这里写图片描述
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值