skimage2.segmentation.active_contour#

skimage2.segmentation.active_contour(image, snake, alpha=0.01, beta=0.1, w_line=0.0, w_edge=1, gamma=0.01, max_px_move=1.0, max_num_iter=2500, convergence=0.1, *, boundary_condition='periodic')[source]#

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:
imagendarray of shape (M, N[, 3])

Input image.

snakendarray of shape (K, 2)

Initial snake coordinates. For periodic boundary conditions, endpoints must not be duplicated.

alphafloat, optional

Snake length shape parameter. Higher values makes snake contract faster.

betafloat, optional

Snake smoothness shape parameter. Higher values makes snake smoother.

w_linefloat, optional

Controls attraction to brightness. Use negative values to attract toward dark regions.

w_edgefloat, optional

Controls attraction to edges. Use negative values to repel snake from edges.

gammafloat, optional

Explicit time stepping parameter.

max_px_movefloat, optional

Maximum pixel distance to move per iteration.

max_num_iterint, optional

Maximum iterations to optimize snake shape.

convergencefloat, optional

Convergence criteria.

boundary_conditionstr, 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(K, 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 _skimage2.draw import circle_perimeter
>>> from _skimage2.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, sigma=2)

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)
>>> dist = np.sqrt((45-snake[:, 0])**2 + (35-snake[:, 1])**2)
>>> int(np.mean(dist))
25