-
Notifications
You must be signed in to change notification settings - Fork 0
/
AspectRatio.m
69 lines (43 loc) · 1.81 KB
/
AspectRatio.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
Ax8 = axes ;
hold(Ax8,'on') ;
%%
[~,Lab,~,~] = bwboundaries(BW) ;
bw = Lab == 1 ;
[Bound,~,~,~] = bwboundaries(bw) ;
imshow(bw,'Parent',Ax8) ;
X = Bound{2,1}(:,2) ;
Y = Bound{2,1}(:,1) ;
plot(Ax8,X,Y,'b','LineWidth',1.25) ;
%%
S = regionprops(~bw,'MajoraxisLength','MinoraxisLength','Orientation','Centroid','MaxFeretProperties','MinFeretProperties') ;
%% Method
Coord = [X' ; Y'] ;
DistMatrix = squareform(pdist(Coord')) ;
[r,c] = find(DistMatrix == max(DistMatrix(:))) ;
r_index = r(1) ;
c_index = c(1) ;
% Major
Loc_major = [r_index c_index] ;
plot(Ax8,[X(Loc_major(1)) X(Loc_major(2))] , [Y(Loc_major(1)) Y(Loc_major(2))],'g','LineWidth',1.25)
Major = sqrt((X(Loc_major(1))-X(Loc_major(2)))^2 + (Y(Loc_major(1))-Y(Loc_major(2)))^2) ;
% Major = DistMatrix(r(1),r(2)) ;
%%
Minor_slope = -1 / ((Y(r_index)-Y(c_index))/(X(r_index)-X(c_index))) ;
Xcent = mean([X(r_index) X(c_index)]) ;
Ycent = mean([Y(r_index) Y(c_index)]) ;
% plot(Ax8,Xcent,Ycent,'*r')
f_minor = @(x) (Minor_slope*(x-Xcent) + Ycent) ;
x_minor = X ;
y_minor = f_minor(x_minor) ;
D_minor = abs(Y-y_minor) ;
D = max(D_minor)-D_minor ;
[Peak , Loc_minor] = findpeaks(D,'MinPeakDistance',10,'MinPeakHeight',max(D)-1,'MinPeakProminence',1,'MinPeakWidth',5) ;
% plot(Ax8,X(55),Y(55),'*r')
% plot(Ax8,X(358),Y(358),'*r')
plot(Ax8,[X(Loc_minor(1)) X(Loc_minor(2))],[Y(Loc_minor(1)) Y(Loc_minor(2))],'m','LineWidth',1.25) ;
Minor = sqrt((X(Loc_minor(1))-X(Loc_minor(2)))^2 + (Y(Loc_minor(1))-Y(Loc_minor(2)))^2) ;
AspectRatioFactor = Minor/Major ;
%%
T1 = title(Ax8,{['$Aspect Ratio = \frac{Diameter_{Minor}}{Diameter_{Major}}$' ],['$Aspect Ratio = ' num2str(AspectRatioFactor) '$']},'interpreter','latex','fontsize',16) ;
T1.Units = 'normalized' ;
T1.Position = T1.Position + [0 -0.1 0] ;