-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_chanvese.m
342 lines (290 loc) · 11.2 KB
/
multi_chanvese.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
%%%%% multi-chanvese %%%%%%
function [seg,phi,its] = multi_chanvese(I,init_mask,kai,sz,max_its,alpha,thresh,display)
%-- default value for parameter alpha is .1
if(~exist('alpha','var'))
alpha = .2;
end
%-- default value for parameter thresh is 0
if(~exist('thresh','var'))
thresh = 0;
end
%-- default behavior is to display intermediate outputs
if(~exist('display','var'))
display = true;
end
color='r';
%-- ensures image is 2D double matrix
I = im2graydouble(I);
%-- Create a signed distance map (SDF) from mask
phi = mask2phi(init_mask);
%--main loop
its = 0; stop = 0;
prev_mask = init_mask; c = 0;
nbr=[1,1;1,0;1,-1;0,1;0,-1;-1,1;-1,0;-1,-1];
NB_width=1.2; %%%% increment by 0.2
increa = 0.2;
epsilon_merge = 0.001;
[dimx,dimy]=size(kai);
phi_temp=zeros(dimx,dimy);
numObj = uint16(max(kai(:)));
se2= strel('disk',2);
while ((its < max_its) && ~stop)
idx = find(phi < NB_width & phi > -NB_width); % get the curve's narrow band
num_idx = numel(idx);
if ~isempty(idx)
%-- intermediate output
if (display>0)
showCurveAndPhi(phi,I,color);
title([num2str(its),' iteration']);
drawnow
% if ( mod(its,5)==1 )
% keyboard
% end
end
%-- find interior and exterior mean
%upts = find(phi<=0); % interior points
vpts = find(phi>0); % exterior points
%u = sum(I(upts))/(length(upts)+eps); % interior mean
v = sum(I(vpts))/(length(vpts)+eps); % exterior mean
%F = (I(idx)-u).^2-(I(idx)-v).^2; % force from image information
curvature = get_curvature(phi,idx); % force from curvature penalty
%dphidt = F./max(abs(F)) + alpha*curvature; % gradient descent to minimize energy
if(its==20)
keyboard
end
dphidt=zeros(1,num_idx);
%F=zeros(1,num_idx);
%regularTerm=zeros(1,num_idx);
for obj_iter = 1:1:numObj
upts = (kai==obj_iter);
tmp_sz = nnz(upts);
u=sum(I(upts))/(tmp_sz+eps);
imgk = imdilate(upts,se2);
tmp = find(imgk(idx)>0);
F=(I(idx(tmp))-u).^2 - (I(idx(tmp))-v).^2;
regularTerm=zeros(numel(tmp),1);
if(tmp_sz<sz(obj_iter)*0.75)
regularTerm(:,1)=alpha*curvature(tmp)-0.1/(1+exp(10*tmp_sz/sz(obj_iter)-8.5));
elseif(tmp_sz>sz(obj_iter)*1.25)
regularTerm(:,1)=alpha*curvature(tmp)+0.1/(1+exp(-10*tmp_sz/sz(obj_iter)+11.5));
end
try
dphidt(tmp)=F./max(abs(F))+regularTerm;
catch
keyboard
end
end
%-- maintain the CFL condition
dt = .45/(max(abs(dphidt))+eps);
%-- evolve the curve
%phi(idx) = phi(idx) + dt.*dphidt;
phi_temp(idx) = dt.*dphidt;
%%%% extract narrow band points iteratively%%%%
for bd_iter = increa:increa:NB_width
nb = find((phi<bd_iter & phi>=bd_iter-increa) | (phi>-bd_iter & phi<=-bd_iter+increa));
num_nb = numel(nb);
if(num_nb==0)
continue;
end
[nbx,nby]=ind2sub([dimx,dimy],nb);
for i=1:1:num_nb
tx=nbx(i);ty=nby(i);
if(phi(tx,ty)>0 && phi_temp(tx,ty)<=0)
a=kai(max(tx-1,1):1:min(tx+1,dimx),max(ty-1,1):1:min(ty+1,dimy));
a(2,2)=0;
tmp_obj=unique(nonzeros(a));
On=numel(tmp_obj);
if(On==1)
kai(tx,ty)=tmp_obj;
phi(tx,ty)=phi_temp(tx,ty);
continue;
end
if(On==0)
phi(tx,ty)=epsilon_merge;
continue;
%disp('expanding to an isolated point');
%keyboard
end
a(2,2)=1;
b=(a==0);
cc=bwconncomp(b,8);
if(cc.NumObjects~=1)
phi(tx,ty)=epsilon_merge;
continue;
end
a(2,2)=0;
b=(a>0);
cc=bwconncomp(b,4);
if(cc.NumObjects~=1) %%% not simple
phi(tx,ty)=epsilon_merge;
continue;
end
kai(tx,ty)=tmp_obj(1);
phi(tx,ty)=phi_temp(tx,ty);
elseif(phi(tx,ty)<=0 && phi_temp(tx,ty)>0)
a=kai(max(tx-1,1):1:min(tx+1,dimx),max(ty-1,1):1:min(ty+1,dimy));
a(2,2)=0;
tmp_obj=unique(nonzeros(a));
if(numel(tmp_obj)>1)
disp('unexpected touching');
keyboard;
elseif(numel(tmp_obj)==0)
disp('isolated point');
keyboard
end
b=(a>0);
cc=bwconncomp(b,4);
if(cc.NumObjects~=1) %%% not simple
phi(tx,ty)=-epsilon_merge;
continue;
end
a(2,2)=1;
b=(a==0);
cc=bwconncomp(b,8);
if(cc.NumObjects~=1)
phi(tx,ty)=-epsilon_merge;
continue;
end
phi(tx,ty)=phi_temp(tx,ty);
kai(tx,ty)=0;
else
phi(tx,ty)=phi_temp(tx,ty);
end
end
end
%-- Keep SDF smooth
phi = sussman(phi, .5);
new_mask = phi<=0;
c = convergence(prev_mask,new_mask,thresh,c);
if c <= 5
its = its + 1;
prev_mask = new_mask;
else stop = 1;
end
else
break;
end
end
%-- final output
showCurveAndPhi(phi,I,color);
title([num2str(its),' iteration']);
drawnow
%-- make mask from SDF
seg = phi<=0; %-- Get mask from levelset
%---------------------------------------------------------------------
%-- AUXILIARY FUNCTIONS ----------------------------------------------
%---------------------------------------------------------------------
%-- Displays the image with curve superimposed
function showCurveAndPhi(phi,I,cl)
figure(2);
imagesc(I,[0, 255]); axis off; axis equal; colormap(gray);
hold on; contour(phi,[0 0],cl,'Linewidth',1); hold off;
% delete(h);
% test = isequal(size(c,2),0);
% while (test==false)
% s = c(2,1);
% if ( s == (size(c,2)-1) )
% t = c;
% figure(1)
% hold on; plot(t(1,2:end)',t(2,2:end)',cl,'Linewidth',3);
% test = true;
% else
% t = c(:,2:s+1);
% figure(1)
% hold on; plot(t(1,1:end)',t(2,1:end)',cl,'Linewidth',3);
% c = c(:,s+2:end);
% end
% end
%-- converts a mask to a SDF
function phi = mask2phi(init_a)
phi=bwdist(init_a)-bwdist(1-init_a)+im2double(init_a)-.5;
%-- compute curvature along SDF
function curvature = get_curvature(phi,idx)
[dimy, dimx] = size(phi);
[y x] = ind2sub([dimy,dimx],idx); % get subscripts
%-- get subscripts of neighbors
ym1 = y-1; xm1 = x-1; yp1 = y+1; xp1 = x+1;
%-- bounds checking
ym1(ym1<1) = 1; xm1(xm1<1) = 1;
yp1(yp1>dimy)=dimy; xp1(xp1>dimx) = dimx;
%-- get indexes for 8 neighbors
idup = sub2ind(size(phi),yp1,x);
iddn = sub2ind(size(phi),ym1,x);
idlt = sub2ind(size(phi),y,xm1);
idrt = sub2ind(size(phi),y,xp1);
idul = sub2ind(size(phi),yp1,xm1);
idur = sub2ind(size(phi),yp1,xp1);
iddl = sub2ind(size(phi),ym1,xm1);
iddr = sub2ind(size(phi),ym1,xp1);
%-- get central derivatives of SDF at x,y
phi_x = -phi(idlt)+phi(idrt);
phi_y = -phi(iddn)+phi(idup);
phi_xx = phi(idlt)-2*phi(idx)+phi(idrt);
phi_yy = phi(iddn)-2*phi(idx)+phi(idup);
phi_xy = -0.25*phi(iddl)-0.25*phi(idur)...
+0.25*phi(iddr)+0.25*phi(idul);
phi_x2 = phi_x.^2;
phi_y2 = phi_y.^2;
%-- compute curvature (Kappa)
curvature = ((phi_x2.*phi_yy + phi_y2.*phi_xx - 2*phi_x.*phi_y.*phi_xy)./...
(phi_x2 + phi_y2 +eps).^(3/2)).*(phi_x2 + phi_y2).^(1/2);
%-- Converts image to one channel (grayscale) double
function img = im2graydouble(img)
[dimy, dimx, c] = size(img);
if(isfloat(img)) % image is a double
if(c==3)
img = rgb2gray(uint8(img));
end
else % image is a int
if(c==3)
img = rgb2gray(img);
end
img = double(img);
end
%-- level set re-initialization by the sussman method
function D = sussman(D, dt)
% forward/backward differences
a = D - shiftR(D); % backward
b = shiftL(D) - D; % forward
c = D - shiftD(D); % backward
d = shiftU(D) - D; % forward
a_p = a; a_n = a; % a+ and a-
b_p = b; b_n = b;
c_p = c; c_n = c;
d_p = d; d_n = d;
a_p(a < 0) = 0;
a_n(a > 0) = 0;
b_p(b < 0) = 0;
b_n(b > 0) = 0;
c_p(c < 0) = 0;
c_n(c > 0) = 0;
d_p(d < 0) = 0;
d_n(d > 0) = 0;
dD = zeros(size(D));
D_neg_ind = find(D < 0);
D_pos_ind = find(D > 0);
dD(D_pos_ind) = sqrt(max(a_p(D_pos_ind).^2, b_n(D_pos_ind).^2) ...
+ max(c_p(D_pos_ind).^2, d_n(D_pos_ind).^2)) - 1;
dD(D_neg_ind) = sqrt(max(a_n(D_neg_ind).^2, b_p(D_neg_ind).^2) ...
+ max(c_n(D_neg_ind).^2, d_p(D_neg_ind).^2)) - 1;
D = D - dt .* sussman_sign(D) .* dD;
%-- whole matrix derivatives
function shift = shiftD(M)
shift = shiftR(M')';
function shift = shiftL(M)
shift = [ M(:,2:size(M,2)) M(:,size(M,2)) ];
function shift = shiftR(M)
shift = [ M(:,1) M(:,1:size(M,2)-1) ];
function shift = shiftU(M)
shift = shiftL(M')';
function S = sussman_sign(D)
S = D ./ sqrt(D.^2 + 1);
% Convergence Test
function c = convergence(p_mask,n_mask,thresh,c)
diff = p_mask - n_mask;
n_diff = sum(abs(diff(:)));
if n_diff < thresh
c = c + 1;
else
c = 0;
end