[c28c68]: / Code / watershedSeg.m

Download this file

65 lines (51 with data), 2.3 kB

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