How to use Watershed function to find multiple regions in a 2D array?
2 views (last 30 days)
Show older comments
Hi There,
- I'm a newbie to Matlab and I'm wondering if there is a better or easier way to find multiple peak regions (I understand watershed follows the idea to find sinks so I just multiplied the data by -1). I'm following the method from link Marker-Controlled Watershed Segmentation Here is my code :
rgb = imread('vil_matlab_test.png');
I = rgb2gray(rgb);
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
se = strel('disk', 20);
Io = imopen(I, se);
Ie = imerode(I, se);
Iobr = imreconstruct(Ie, I);
Ioc = imclose(Io, se);
Iobrd = imdilate(Iobr, se);
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
fgm = imregionalmax(Iobrcbr);
I2 = I;
I2(fgm) = 255;
se2 = strel(ones(5,5));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20);
I3 = I;
I3(fgm4) = 255;
bw = imbinarize(Iobrcbr);
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
figure
The input figure looks like :
I'm expecting to find at least two regions, but the result looks like :
Can anyone tell me how to correct this? Or is there other better way to do this? Thanks!
0 Comments
Answers (0)
See Also
Categories
Find more on Parametric Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!