基于距离变换方式的分水岭分割算法
代码:
%-------------------------------------
%---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('×îС¸²¸ÇÌݶȸĽø');