-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
active_contour_model.py
230 lines (201 loc) · 7.69 KB
/
active_contour_model.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
import numpy as np
from scipy.interpolate import RectBivariateSpline
from .._shared.utils import _supported_float_type
from ..util import img_as_float
from ..filters import sobel
def active_contour(image, snake, alpha=0.01, beta=0.1,
w_line=0, w_edge=1, gamma=0.01,
max_px_move=1.0,
max_num_iter=2500, convergence=0.1,
*,
boundary_condition='periodic'):
"""Active contour model.
Active contours by fitting snakes to features of images. Supports single
and multichannel 2D images. Snakes can be periodic (for segmentation) or
have fixed and/or free ends.
The output snake has the same length as the input boundary.
As the number of points is constant, make sure that the initial snake
has enough points to capture the details of the final contour.
Parameters
----------
image : (N, M) or (N, M, 3) ndarray
Input image.
snake : (N, 2) ndarray
Initial snake coordinates. For periodic boundary conditions, endpoints
must not be duplicated.
alpha : float, optional
Snake length shape parameter. Higher values makes snake contract
faster.
beta : float, optional
Snake smoothness shape parameter. Higher values makes snake smoother.
w_line : float, optional
Controls attraction to brightness. Use negative values to attract
toward dark regions.
w_edge : float, optional
Controls attraction to edges. Use negative values to repel snake from
edges.
gamma : float, optional
Explicit time stepping parameter.
max_px_move : float, optional
Maximum pixel distance to move per iteration.
max_num_iter : int, optional
Maximum iterations to optimize snake shape.
convergence : float, optional
Convergence criteria.
boundary_condition : string, optional
Boundary conditions for the contour. Can be one of 'periodic',
'free', 'fixed', 'free-fixed', or 'fixed-free'. 'periodic' attaches
the two ends of the snake, 'fixed' holds the end-points in place,
and 'free' allows free movement of the ends. 'fixed' and 'free' can
be combined by parsing 'fixed-free', 'free-fixed'. Parsing
'fixed-fixed' or 'free-free' yields same behaviour as 'fixed' and
'free', respectively.
Returns
-------
snake : (N, 2) ndarray
Optimised snake, same shape as input parameter.
References
----------
.. [1] Kass, M.; Witkin, A.; Terzopoulos, D. "Snakes: Active contour
models". International Journal of Computer Vision 1 (4): 321
(1988). :DOI:`10.1007/BF00133570`
Examples
--------
>>> from skimage.draw import circle_perimeter
>>> from skimage.filters import gaussian
Create and smooth image:
>>> img = np.zeros((100, 100))
>>> rr, cc = circle_perimeter(35, 45, 25)
>>> img[rr, cc] = 1
>>> img = gaussian(img, 2, preserve_range=False)
Initialize spline:
>>> s = np.linspace(0, 2*np.pi, 100)
>>> init = 50 * np.array([np.sin(s), np.cos(s)]).T + 50
Fit spline to image:
>>> snake = active_contour(img, init, w_edge=0, w_line=1, coordinates='rc') # doctest: +SKIP
>>> dist = np.sqrt((45-snake[:, 0])**2 + (35-snake[:, 1])**2) # doctest: +SKIP
>>> int(np.mean(dist)) # doctest: +SKIP
25
"""
max_num_iter = int(max_num_iter)
if max_num_iter <= 0:
raise ValueError("max_num_iter should be >0.")
convergence_order = 10
valid_bcs = ['periodic', 'free', 'fixed', 'free-fixed',
'fixed-free', 'fixed-fixed', 'free-free']
if boundary_condition not in valid_bcs:
raise ValueError("Invalid boundary condition.\n" +
"Should be one of: "+", ".join(valid_bcs)+'.')
img = img_as_float(image)
float_dtype = _supported_float_type(image.dtype)
img = img.astype(float_dtype, copy=False)
RGB = img.ndim == 3
# Find edges using sobel:
if w_edge != 0:
if RGB:
edge = [sobel(img[:, :, 0]), sobel(img[:, :, 1]),
sobel(img[:, :, 2])]
else:
edge = [sobel(img)]
else:
edge = [0]
# Superimpose intensity and edge images:
if RGB:
img = w_line*np.sum(img, axis=2) \
+ w_edge*sum(edge)
else:
img = w_line*img + w_edge*edge[0]
# Interpolate for smoothness:
intp = RectBivariateSpline(np.arange(img.shape[1]),
np.arange(img.shape[0]),
img.T, kx=2, ky=2, s=0)
snake_xy = snake[:, ::-1]
x = snake_xy[:, 0].astype(float_dtype)
y = snake_xy[:, 1].astype(float_dtype)
n = len(x)
xsave = np.empty((convergence_order, n), dtype=float_dtype)
ysave = np.empty((convergence_order, n), dtype=float_dtype)
# Build snake shape matrix for Euler equation in double precision
eye_n = np.eye(n, dtype=float)
a = (np.roll(eye_n, -1, axis=0)
+ np.roll(eye_n, -1, axis=1)
- 2 * eye_n) # second order derivative, central difference
b = (np.roll(eye_n, -2, axis=0)
+ np.roll(eye_n, -2, axis=1)
- 4 * np.roll(eye_n, -1, axis=0)
- 4 * np.roll(eye_n, -1, axis=1)
+ 6 * eye_n) # fourth order derivative, central difference
A = -alpha * a + beta * b
# Impose boundary conditions different from periodic:
sfixed = False
if boundary_condition.startswith('fixed'):
A[0, :] = 0
A[1, :] = 0
A[1, :3] = [1, -2, 1]
sfixed = True
efixed = False
if boundary_condition.endswith('fixed'):
A[-1, :] = 0
A[-2, :] = 0
A[-2, -3:] = [1, -2, 1]
efixed = True
sfree = False
if boundary_condition.startswith('free'):
A[0, :] = 0
A[0, :3] = [1, -2, 1]
A[1, :] = 0
A[1, :4] = [-1, 3, -3, 1]
sfree = True
efree = False
if boundary_condition.endswith('free'):
A[-1, :] = 0
A[-1, -3:] = [1, -2, 1]
A[-2, :] = 0
A[-2, -4:] = [-1, 3, -3, 1]
efree = True
# Only one inversion is needed for implicit spline energy minimization:
inv = np.linalg.inv(A + gamma * eye_n)
# can use float_dtype once we have computed the inverse in double precision
inv = inv.astype(float_dtype, copy=False)
# Explicit time stepping for image energy minimization:
for i in range(max_num_iter):
# RectBivariateSpline always returns float64, so call astype here
fx = intp(x, y, dx=1, grid=False).astype(float_dtype, copy=False)
fy = intp(x, y, dy=1, grid=False).astype(float_dtype, copy=False)
if sfixed:
fx[0] = 0
fy[0] = 0
if efixed:
fx[-1] = 0
fy[-1] = 0
if sfree:
fx[0] *= 2
fy[0] *= 2
if efree:
fx[-1] *= 2
fy[-1] *= 2
xn = inv @ (gamma*x + fx)
yn = inv @ (gamma*y + fy)
# Movements are capped to max_px_move per iteration:
dx = max_px_move * np.tanh(xn - x)
dy = max_px_move * np.tanh(yn - y)
if sfixed:
dx[0] = 0
dy[0] = 0
if efixed:
dx[-1] = 0
dy[-1] = 0
x += dx
y += dy
# Convergence criteria needs to compare to a number of previous
# configurations since oscillations can occur.
j = i % (convergence_order + 1)
if j < convergence_order:
xsave[j, :] = x
ysave[j, :] = y
else:
dist = np.min(np.max(np.abs(xsave - x[None, :])
+ np.abs(ysave - y[None, :]), 1))
if dist < convergence:
break
return np.stack([y, x], axis=1)