-
Notifications
You must be signed in to change notification settings - Fork 1
/
InterfaceCurvature.m
232 lines (185 loc) · 6.64 KB
/
InterfaceCurvature.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
function [ xy , K ] = InterfaceCurvature( I , v , NN , SR )
Flag = 1; % Flag = 1 --> Display plots
% Flag = 0 --> Do not display plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Description of I/O variables: %
% %
% INPUT:- %
% %
% I : image matrix, can be 2D or 3D (RGB) %
% v : contour / level-set value, which should be in the range [0,1] %
% NN : number of neighboring points used to fit a circle %
% SR : sub-sampling rate (should be integer > 0) %
% %
% OUTPUT:- %
% %
% K : curvature vector (cell of dim equal to number of contours) %
% xy : coordinates of the points on the contour %
% %
% %
% NOTE: For reliable outputs, the image must be atleast 300x300 %
% and each contour must have atleast 500 points. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Jaya Kumar. A
% E-mail: jkumar.res@gmail.com
%
% See the accompanying "doc.pdf" for algorithm details.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Formating the data %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Converting a 3D color image to 2D grayscale image
if length(size(I))==3
I = rgb2gray(I);
end
I = double(I);
Imin = min(I(:));
Imax = max(I(:));
I = (I-Imin) / (Imax-Imin); % Normalizing the data [0,1]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Padding the data to take care of %%%
%%% interfaces that hit the boundaries %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[Lx,Ly] = size(I);
I2 = zeros(Lx+6,Ly+6);
I2( 4:Lx+3 , 4:Ly+3 ) = I;
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Contour extraction %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
N = NN;
figure;
subplot(2,2,1)
xy = contour( I2 , [v v] );
title('Contours');
xlabel('X');
ylabel('Y');
axis equal;
axis tight;
%set(gca,'YDir','reverse');
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Conttour splitting %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
Np = length( xy(1,:) );
Lc = xy(2,1); % Number of points on the 1st contour
ll = Lc+1;
iLc = 1;
while( ll < Np )
Lc = [Lc ; xy(2,ll+1)];
iLc = [iLc ; ll+1];
ll = sum( Lc+1 );
end
Nc = length( Lc ); % Number of contours
% Cells to handle multiple contours with different sets of points
x = cell(Nc,1); % X-coordinates of the contour
y = cell(Nc,1); % Y-coordinates of the contour
r = cell(Nc,1); % Radius of curvature
rr = cell(Nc,1); %
x2 = cell(Nc,1); % Sampled X-coordinates
y2 = cell(Nc,1); % Sampled Y-coordinates
a = cell(Nc,1); % X-coordinate of the circle center
b = cell(Nc,1); % Y-coordinate of the circle center
for i=1:Nc
x{i} = xy( 1 , iLc(i)+1 : iLc(i)+Lc(i) )';
y{i} = xy( 2 , iLc(i)+1 : iLc(i)+Lc(i) )';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Calculation of curvature %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:Nc
%%%%%%%%%%%%%%%%%%%%
%%% Sub-sampling %%%
%%%%%%%%%%%%%%%%%%%%
Np = length(x{i});
ll = floor( Np/SR );
xx = reshape( x{i}(1:SR*ll) , [SR ll] );
yy = reshape( y{i}(1:SR*ll) , [SR ll] );
xx = xx(1,:)';
yy = yy(1,:)';
Np = ll;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Locally fitting circles %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r{i} = zeros(Np,1);
a{i} = zeros(Np,1);
b{i} = zeros(Np,1);
for j=1:Np
%%% Maintain contour periodicity
j1 = mod( j-NN-1 , Np ) + 1;
j2 = mod( j+NN-1 , Np ) + 1;
if( j1 < j2 )
[a{i}(j),b{i}(j),r{i}(j)] = FitCircle(xx( j1:j2 ),yy( j1:j2 ));
else
[a{i}(j),b{i}(j),r{i}(j)] = FitCircle( [xx( j1:end ) ; xx(1:j2)]...
,[yy( j1:end ) ; yy(1:j2)]);
end
end
rr{i} = abs(r{i});
x2{i} = xx;
y2{i} = yy;
a{i} = real(a{i});
b{i} = real(b{i});
end
if( Flag==1 )
subplot(2,2,2);
plot(rr{1});
title('Radius of curvature');
hold on;
leg = cell(Nc,1);
leg{1} = 'Contour1';
for i=2:Nc
plot(abs(rr{i}));
leg{i} = sprintf('Contour%d',i);
end
ylim( [0 , sqrt(Lx^2+Ly^2)] );
xlabel('Contour length');
ylabel('R');
legend( leg );
subplot(2,2,4);
plot3(x2{1},y2{1},rr{1},'-*');
title('3D plot of radius of curvature');
hold on;
for i=2:Nc
plot3(x2{i},y2{i},rr{i},'-*');
end
box on;
zlim( [0 , sqrt(Lx^2+Ly^2)] );
xlabel('X');
ylabel('Y');
zlabel('R');
end
v = cell(Nc,1);
K = cell(Nc,1);
for i=1:Nc
v{i} = [(x2{i}-a{i}) (y2{i}-b{i})];
vv = sqrt( v{i}(:,1).^2 + v{i}(:,2).^2 );
v{i} = v{i} ./ [vv vv];
K{i} = v{i} ./ [rr{i} rr{i}];
end
if( Flag==1 )
subplot(2,2,3);
[m,n] = size(I2);
I3 = zeros(m,n,3);
I3(:,:,1) = I2;
I3(:,:,2) = I2;
I3(:,:,3) = I2;
%imagesc(I3);
hold on;
for i=1:Nc
plot(x2{i},y2{i});
h = quiver(x2{i},y2{i},K{i}(:,1),K{i}(:,2));
set(h,'AutoScale','on', 'AutoScaleFactor', 2)
end
axis equal;
axis tight;
xlim([0 Ly]);
ylim([0 Lx]);
title('Negative curvature vector field');
xlabel('X');
ylabel('Y');
end
xy = cell(Nc,1);
for i=1:Nc
xy{i} = [x2{i} y2{i}];
end
end