|
a |
|
b/Code/watershedSeg.m |
|
|
1 |
function watershedSeg(org, sob55); |
|
|
2 |
|
|
|
3 |
subplot(3,3,1), imshow(org,[]), title('original'); |
|
|
4 |
|
|
|
5 |
%calculate gradient magnitude and use it as segmentaion function |
|
|
6 |
sob55 = sobel55(org); |
|
|
7 |
subplot(3,3,2), imshow(sob55,[]), title('sobel'); |
|
|
8 |
|
|
|
9 |
%After applying watershed transform on gradient magnitude -- oversegmented |
|
|
10 |
W = watershed(sob55); |
|
|
11 |
Lrgb = label2rgb(W); |
|
|
12 |
subplot(3,3,3), imshow(Lrgb,[]), title('WT on gradient'); |
|
|
13 |
|
|
|
14 |
%marking the foregroung objects |
|
|
15 |
%morphological operators transform the original image into another image through the iteration |
|
|
16 |
%with other image of a certain shape and size which is known as structuring element |
|
|
17 |
structureElem = strel('disk', 7); |
|
|
18 |
|
|
|
19 |
%opening by reconstruction |
|
|
20 |
imgErode = imerode(org, structureElem); |
|
|
21 |
imgRecons = imreconstruct(imgErode, org); |
|
|
22 |
%subplot(3,3,2), imshow(imgRecons), title('Open Reconstruction') |
|
|
23 |
|
|
|
24 |
%closing by reconstr |
|
|
25 |
imgDilate = imdilate(imgRecons, structureElem); |
|
|
26 |
imgRecons2 = imreconstruct(imcomplement(imgDilate), imcomplement(imgRecons)); |
|
|
27 |
imgRecons2 = imcomplement(imgRecons2); |
|
|
28 |
%subplot(3,3,3), imshow(imgRecons2), title('close reconstruction') |
|
|
29 |
|
|
|
30 |
%calculate regional maxima |
|
|
31 |
regMaxima = imregionalmax(imgRecons2); |
|
|
32 |
subplot(3,3,4), imshow(regMaxima), title('Region maxima'); |
|
|
33 |
org2 = org; |
|
|
34 |
org2(regMaxima) = 255; |
|
|
35 |
subplot(3,3,5),imshow(org2,[]), title('maxima superimposed on original') |
|
|
36 |
|
|
|
37 |
%Clean the edges of the markers using edge reconstruction |
|
|
38 |
structureElem2 = strel(ones(5,5)); |
|
|
39 |
regMaxima2 = imclose(regMaxima, structureElem2); |
|
|
40 |
regMaxima3 = imerode(regMaxima2, structureElem2); |
|
|
41 |
regMaxima4 = bwareaopen(regMaxima3, 20); |
|
|
42 |
org3 = org; |
|
|
43 |
org3(regMaxima4) = 255; |
|
|
44 |
|
|
|
45 |
%Compute background markers |
|
|
46 |
bw = imbinarize(imgRecons2); |
|
|
47 |
subplot(3,3,6), imshow(bw,[]), title('binarized') |
|
|
48 |
|
|
|
49 |
%distance transform of binarized |
|
|
50 |
distTrans = bwdist(bw); |
|
|
51 |
distTransW = watershed(distTrans); |
|
|
52 |
bgm = distTransW == 0; |
|
|
53 |
subplot(3,3,7), imshow(bgm,[]), title('background maxima') |
|
|
54 |
|
|
|
55 |
%Compute the Watershed Transform of the Segmentation Function |
|
|
56 |
sob2 = imimposemin(sob55, bgm | regMaxima4); |
|
|
57 |
W2 = watershed(sob2); |
|
|
58 |
|
|
|
59 |
org4 = org; |
|
|
60 |
org4(imdilate(W2 == 0, ones(3, 3)) | bgm | regMaxima4) = 255; |
|
|
61 |
subplot(3,3,8),imshow(org4,[]), title('Markers/boundaries superimposed on original') |
|
|
62 |
Lrgb2 = label2rgb(W2, 'jet', 'w', 'shuffle'); |
|
|
63 |
subplot(3,3,9), imshow(Lrgb2,[]),title('watershed label') |
|
|
64 |
|