-
Notifications
You must be signed in to change notification settings - Fork 6
/
chanvese.py
286 lines (231 loc) · 8.4 KB
/
chanvese.py
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
# ------------------------------------------------------------------------
# Region Based Active Contour Segmentation
#
# seg = region_seg(I, init_mask, max_its, alpha, display)
#
# Inputs: I 2D image
# init_mask Initialization (1 = foreground, 0 = bg)
# max_its Number of iterations to run segmentation for
# alpha (optional) Weight of smoothing term
# higer = smoother. default = 0.2
# display (optional) displays intermediate outputs
# default = true
#
# Outputs: seg Final segmentation mask (1=fg, 0=bg)
#
# Description: This code implements the paper: "Active Contours Without
# Edges" By Chan Vese. This is a nice way to segment images whose
# foregrounds and backgrounds are statistically different and homogeneous.
#
# Example:
# img = imread('tire.tif');
# m = zeros(size(img));
# m(33:33+117, 44:44+128) = 1;
# seg = region_seg(img, m, 500);
#
# Coded by: Shawn Lankton (www.shawnlankton.com)
# ------------------------------------------------------------------------
#
# Gif montage (save each step in a gif folder):
# convert -delay 50 gif/levelset*.png levelset.gif
import numpy as np
import scipy.ndimage as nd
import matplotlib.pyplot as plt
import scipy.misc
from scipy.misc import pilutil
eps = np.finfo(float).eps
def chanvese(I, init_mask, max_its=200, alpha=0.2,thresh=0, color='r', display=False):
I = I.astype(np.float)
# Create a signed distance map (SDF) from mask
phi = mask2phi(init_mask)
if display:
plt.ion()
fig, axes = plt.subplots(ncols=2)
show_curve_and_phi(fig, I, phi, color)
plt.savefig('levelset_start.png', bbox_inches='tight')
# Main loop
its = 0
stop = False
prev_mask = init_mask
c = 0
while (its < max_its and not stop):
# Get the curve's narrow band
idx = np.flatnonzero(np.logical_and(phi <= 1.2, phi >= -1.2))
if len(idx) > 0:
# Intermediate output
if display:
if np.mod(its, 50) == 0:
print('iteration: {0}'.format(its))
show_curve_and_phi(fig, I, phi, color)
else:
if np.mod(its, 10) == 0:
print('iteration: {0}'.format(its))
# Find interior and exterior mean
upts = np.flatnonzero(phi <= 0) # interior points
vpts = np.flatnonzero(phi > 0) # exterior points
u = np.sum(I.flat[upts]) / (len(upts) + eps) # interior mean
v = np.sum(I.flat[vpts]) / (len(vpts) + eps) # exterior mean
# Force from image information
F = (I.flat[idx] - u)**2 - (I.flat[idx] - v)**2
# Force from curvature penalty
curvature = get_curvature(phi, idx)
# Gradient descent to minimize energy
dphidt = F / np.max(np.abs(F)) + alpha * curvature
# Maintain the CFL condition
dt = 0.45 / (np.max(np.abs(dphidt)) + eps)
# Evolve the curve
phi.flat[idx] += dt * dphidt
# Keep SDF smooth
phi = sussman(phi, 0.5)
# new_mask = phi <= 0
new_mask = np.uint8(phi <= 0)
# print(new_mask)#for testing
c = convergence(prev_mask, new_mask, thresh, c)
if c <= 5:
its = its + 1
prev_mask = new_mask
else:
stop = True
else:
break
# Final output
if display:
show_curve_and_phi(fig, I, phi, color)
plt.savefig('levelset_end.png', bbox_inches='tight')
# Make mask from SDF
seg = phi <= 0 # Get mask from levelset
out = np.uint8(seg * 255)
# print(out)
pilutil.Image.fromarray(out, mode='L')
# b is converted from an ndarray to an image
b = scipy.misc.toimage(out)
b.save('chan-vese.png')
b.show()
return seg, phi, its
# ---------------------------------------------------------------------
# ---------------------- AUXILIARY FUNCTIONS --------------------------
# ---------------------------------------------------------------------
def bwdist(a):
"""
Intermediary function. 'a' has only True/False vals,
so we convert them into 0/1 values - in reverse.
True is 0, False is 1, distance_transform_edt wants it that way.
"""
return nd.distance_transform_edt(a == 0)
# Displays the image with curve superimposed
def show_curve_and_phi(fig, I, phi, color):
fig.axes[0].cla()
fig.axes[0].imshow(I, cmap='gray')
fig.axes[0].contour(phi, 0, colors=color)
fig.axes[0].set_axis_off()
plt.draw()
fig.axes[1].cla()
fig.axes[1].imshow(phi)
fig.axes[1].set_axis_off()
plt.draw()
plt.pause(0.001)
def im2double(a):
a = a.astype(np.float)
a /= np.abs(a).max()
return a
# Converts a mask to a SDF
def mask2phi(init_a):
phi = bwdist(init_a) - bwdist(1 - init_a) + im2double(init_a) - 0.5
return phi
# Compute curvature along SDF
def get_curvature(phi, idx):
dimy, dimx = phi.shape
yx = np.array([np.unravel_index(i, phi.shape) for i in idx]) # subscripts
y = yx[:, 0]
x = yx[:, 1]
# Get subscripts of neighbors
ym1 = y - 1
xm1 = x - 1
yp1 = y + 1
xp1 = x + 1
# Bounds checking
ym1[ym1 < 0] = 0
xm1[xm1 < 0] = 0
yp1[yp1 >= dimy] = dimy - 1
xp1[xp1 >= dimx] = dimx - 1
# Get indexes for 8 neighbors
idup = np.ravel_multi_index((yp1, x), phi.shape)
iddn = np.ravel_multi_index((ym1, x), phi.shape)
idlt = np.ravel_multi_index((y, xm1), phi.shape)
idrt = np.ravel_multi_index((y, xp1), phi.shape)
idul = np.ravel_multi_index((yp1, xm1), phi.shape)
idur = np.ravel_multi_index((yp1, xp1), phi.shape)
iddl = np.ravel_multi_index((ym1, xm1), phi.shape)
iddr = np.ravel_multi_index((ym1, xp1), phi.shape)
# Get central derivatives of SDF at x,y
phi_x = -phi.flat[idlt] + phi.flat[idrt]
phi_y = -phi.flat[iddn] + phi.flat[idup]
phi_xx = phi.flat[idlt] - 2 * phi.flat[idx] + phi.flat[idrt]
phi_yy = phi.flat[iddn] - 2 * phi.flat[idx] + phi.flat[idup]
phi_xy = 0.25 * (- phi.flat[iddl] - phi.flat[idur] +
phi.flat[iddr] + phi.flat[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) ** 1.5) * (phi_x2 + phi_y2) ** 0.5
return curvature
# Level set re-initialization by the sussman method
def sussman(D, dt):
# forward/backward differences
a = D - np.roll(D, 1, axis=1)
b = np.roll(D, -1, axis=1) - D
c = D - np.roll(D, -1, axis=0)
d = np.roll(D, 1, axis=0) - D
a_p = np.clip(a, 0, np.inf)
a_n = np.clip(a, -np.inf, 0)
b_p = np.clip(b, 0, np.inf)
b_n = np.clip(b, -np.inf, 0)
c_p = np.clip(c, 0, np.inf)
c_n = np.clip(c, -np.inf, 0)
d_p = np.clip(d, 0, np.inf)
d_n = np.clip(d, -np.inf, 0)
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 = np.zeros_like(D)
D_neg_ind = np.flatnonzero(D < 0)
D_pos_ind = np.flatnonzero(D > 0)
dD.flat[D_pos_ind] = np.sqrt(
np.max(np.concatenate(
([a_p.flat[D_pos_ind]**2], [b_n.flat[D_pos_ind]**2])), axis=0) +
np.max(np.concatenate(
([c_p.flat[D_pos_ind]**2], [d_n.flat[D_pos_ind]**2])), axis=0)) - 1
dD.flat[D_neg_ind] = np.sqrt(
np.max(np.concatenate(
([a_n.flat[D_neg_ind]**2], [b_p.flat[D_neg_ind]**2])), axis=0) +
np.max(np.concatenate(
([c_n.flat[D_neg_ind]**2], [d_p.flat[D_neg_ind]**2])), axis=0)) - 1
D = D - dt * sussman_sign(D) * dD
return D
def sussman_sign(D):
return D / np.sqrt(D**2 + 1)
# Convergence Test
def convergence(p_mask, n_mask, thresh, c):
diff = p_mask - n_mask
n_diff = np.sum(np.abs(diff))
if n_diff < thresh:
c = c + 1
else:
c = 0
return c
if __name__ == "__main__":
img = nd.imread('/home/jing/Desktop/imgseg/data/skin1.jpg', flatten=True)
mask = np.zeros(img.shape)
mask[600:1200, 300:750] = 1#height,width
chanvese(img, mask, max_its=1000, display=True, alpha=1.0)
# pilutil.Image.fromarray(chanvese[0], mode='L')
# # b is converted from an ndarray to an image
# b = scipy.misc.toimage(chanvese[0])
# # b.save('chanvese.png')
# b.show()