-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIC_withCentroid.m
126 lines (113 loc) · 3.65 KB
/
MIC_withCentroid.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
S = regionprops(bw,'Centroid') ;
[Bound,~,~,~] = bwboundaries(bw) ;
%%
X = Bound{2,1}(:,2) ;
Y = Bound{2,1}(:,1) ;
% plot(Ax9,X,Y,'b','LineWidth',1.25) ;
%% Distance
for i = 1:length(X)
D(i,1) = pdist([S.Centroid(1) S.Centroid(2) ; X(i) Y(i)]) ;
end
base_ind = find(D == max(D)) ;
XX = [X(base_ind(1)+1:end) ; X(1:base_ind(1))] ;
YY = [Y(base_ind(1)+1:end) ; Y(1:base_ind(1))] ;
DD = [D(base_ind(1)+1:end) ; D(1:base_ind(1))] ;
X = XX ; Y = YY ; D = DD ;
%% Optional
[xData, yData] = prepareCurveData([],D) ;
ft = fittype('smoothingspline') ;
opts = fitoptions('Method','SmoothingSpline') ;
opts.SmoothingParam = 0.9 ;
[Fit,~] = fit(xData,yData,ft,opts) ;
D = Fit(xData) ;
%% Main Algorithm
N = 5 ; % Maximum Cluster
M = 3 ; % Neigber
Tfactor = 0.94 ;
while N>1
r = 0 ;
whileCond1 = true ;
x = X ; y = Y ;
while whileCond1
[K,~] = kmeans([x,y],N) ;
% Show
% for i = 1:N
% plot(Ax9,X(K==i),Y(K==i),'Marker','*','Color',rand(1,3),'LineStyle','none') ;
% pause(0.1)
% end
MICPoints = [] ;
for i = 1:N
indx = find(D == min(D(K == i))) ;
MIPoints(i,:) = [x(indx(1)) y(indx(1))] ;
for j = 1:M
Lside(j,:) = [x(indx(1)-j) y(indx(1)-j)] ;
end
for j = 1:M
Rside(j,:) = [x(indx(1)+j) y(indx(1)+j)] ;
end
% Show
% MPoints = [MIPoints ; Lside ; Rside] ;
% plot(Ax9,MPoints(:,1),MPoints(:,2),'*y') ;
MICPoints = [MICPoints ; MIPoints ; Lside ; Rside] ;
end
CentMIC = CircleFitByLandau(MICPoints) ;
r = r + 1 ;
rmic(r) = CentMIC(3) ;
xmic(r) = round(CentMIC(1)) ;
ymic(r) = round(CentMIC(2)) ;
clc
% disp([num2str((((5-N)*r + r)/(5*15))*100,'%.2f') '%'])
disp(N)
disp(r)
if ~isnan(rmic(r)) % Main Constraction
LCentMIC(r) = CheckCenterInGrain(bw,xmic(r),ymic(r)) ;
if LCentMIC(r) == true
[LCircMIC(r) , emic(r)] = CheckCircleInGrain(bw,xmic(r),ymic(r),rmic(r),Tfactor) ;
end
else
LCentMIC(r) = false ;
LCircMIC(r) = false ;
emic(r) = 0 ;
end
if r == 7 & all(emic < Tfactor)
for i = 1:length(x)
D(i,1) = pdist([S.Centroid(1) S.Centroid(2) ; X(i) Y(i)]) ;
end
base_ind = find(D == max(D)) ;
XX = [x(base_ind(1)+1:end) ; x(1:base_ind(1))] ;
YY = [y(base_ind(1)+1:end) ; y(1:base_ind(1))] ;
DD = [D(base_ind(1)+1:end) ; D(1:base_ind(1))] ;
x = XX ; y = YY ; D = DD ;
end
if r > 14
whileCond1 = false ;
end
end % introier while
All_rmic = rmic(LCircMIC) ;
All_xmic = xmic(LCircMIC) ;
All_ymic = ymic(LCircMIC) ;
All_emic = emic(LCircMIC) ;
if ~isempty(All_rmic)
Nindex = find(max(All_rmic) == All_rmic) ;
rMIC(N) = All_rmic(Nindex(1)) ;
eMIC(N) = All_emic(Nindex(1)) ;
xMIC(N) = All_xmic(Nindex(1)) ;
yMIC(N) = All_ymic(Nindex(1)) ;
else
rMIC(N) = nan ;
xMIC(N) = nan ;
yMIC(N) = nan ;
eMIC(N) = nan ;
end
N = N-1 ;
end % Main Loop
%%
w1 = 1 ; w2 = 3 ;
Ne = normalize(w2*eMIC,'range') ;
Nr = normalize(w1*rMIC,'range') ;
Nc = normalize(Nr .*Ne,'range') ;
MIC_index = find(Nc == max(Nc)) ; % best
RMIC = rMIC(MIC_index) ;
EMIC = eMIC(MIC_index) ;
XMIC = xMIC(MIC_index) ;
YMIC = yMIC(MIC_index) ;