-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkmeanExp02.m
106 lines (92 loc) · 3.51 KB
/
kmeanExp02.m
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
% manually define class of some region
% the file name are stored invariable images
% this function can cluster the image region into region and then cluster
% each of them into two cluster, i.e it cluster the region into 2 and then
% cluster each region into 2 cluster again. As result we will have 4
% different cluster of regions.
images = {'../images/GATA-4/M3G4001.TIF'; ...
'../images/GATA-4/M3G4004.TIF'; ...
'../images/GATA-4/M3G4006.TIF'; ...
'../images/GATA-4/M1G40019.TIF'; ...
'../images/GATA-4/M1G40022.TIF'; ...
'../images/GATA-4/M1G40036.TIF'};
for f = 4: -1 : 4
imgFile = images{f};
[diplbl, en_im] = PreProcess(imgFile);
sz = size(en_im);
% apply measure to compute some properties for the region
measurmentIDs = {'Size', 'Perimeter', 'Feret', 'Radius', 'ConvexPerimeter', ...
'P2A', 'PodczeckShapes', 'Convexity', 'Mean', 'StdDev', ...
'Skewness', 'ExcessKurtosis', 'MaxVal', 'MinVal', 'Center', 'Inertia',...
'Mu', 'MajorAxes'};
lab = RGB2Lab(en_im);
stretchedL = percentileStretch(lab(:,:,1), [0,1]);
msr = measure(diplbl, mat2im(stretchedL),measurmentIDs, [],1);
% sort msr based on Id
tmp = msr.id;
[~, sortId] = sort(tmp);
%msr = msr(sortId);
%
msrROI = msr; %msr([class1Region; class2Region]);
% rawFeatures = [msrROI.Mean; msrROI.MinVal; msrROI.Perimeter; msrROI.Size;...
% msrROI.StdDev; msrROI.Skewness; msrROI.ExcessKurtosis]';
% rawFeatures = [msrROI.Skewness; msrROI.ExcessKurtosis]';
%
rawFeatures = [msrROI.Mean]',%; msrROI.MinVal; msrROI.Perimeter; msrROI.Size;...
% msrROI.StdDev; msrROI.Skewness; msrROI.ExcessKurtosis]';
nRawFeatures = normalizeFeature(rawFeatures,'Zero min');
K = 4; %4
opts = statset('Display','final');
[idx,ctrs,sumdist] = kmeans(nRawFeatures,K,'dist','sqEuclidean', 'start', 'cluster',...
'replicates',5,'EmptyAction','singleton','Options',opts);
% choose regino with low gray value which showthe germ mass.
germCluster = 1;
if ctrs(2,1) < ctrs(1,1)
germCluster = 2;
end
rId = msrROI.id;
sortedIdx = idx(sortId);
double_lbl = double(diplbl);
clusterdRgn= zeros(sz(1:2));
for row = 1 : sz(1)
for col = 1 : sz(2)
if double_lbl(row,col)
clusterdRgn(row,col) = sortedIdx(double_lbl(row,col));
end
end
end
% for r =1 : length(rId)
% y(double_lbl == rId(r)) = idx(r);
% end
sz = size(en_im);
for c = 1: K
figure,imshow(en_im);
RGBDist = uint8(cat(3, zeros(sz(1:2)),(clusterdRgn == c)*255, ...
zeros(sz(1:2))));
hold on
distIm = imshow(RGBDist);
if c == germCluster
title(['(' imgFile ') Cluster ' 'Germ Mass']);
else
title(['(' imgFile ') Cluster ' int2str(c) ' out of ' int2str(K)]);
end
set(distIm,'AlphaData',0.3);
end
% show region connectivity graph
germLbl = bwlabel(clusterdRgn == germCluster);
stats = regionprops(germLbl, 'Centroid');
neiborhood = 35;
tic
NRG = findNeighbors(germLbl, stats, neiborhood);
toc
% show graph
figure, imshow(en_im), %lbl_bin_FRST),
% title(['connected regions', fileName]);
for i =1 : size(stats,1)
NS = NRG{i};
for j =1 : numel(NS)
line([stats(i).Centroid(1), stats(NS(j)).Centroid(1)], ...
[stats(i).Centroid(2), stats(NS(j)).Centroid(2)]);
end
end
end