From 5c20ef721861bb60c6e3c12ff236ca4c9293274a Mon Sep 17 00:00:00 2001 From: Julius Bier Kirekgaard Date: Wed, 9 Sep 2015 15:44:35 +0100 Subject: [PATCH] pep8 and py3 compliance. more comments --- doc/examples/plot_active_contours.py | 11 ++-- skimage/segmentation/__init__.py | 2 +- skimage/segmentation/active_contour_model.py | 52 +++++++++++-------- .../tests/test_active_contour_model.py | 15 +++--- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/doc/examples/plot_active_contours.py b/doc/examples/plot_active_contours.py index 911bc174..c4f30c04 100644 --- a/doc/examples/plot_active_contours.py +++ b/doc/examples/plot_active_contours.py @@ -1,7 +1,8 @@ """ -==================================================== +==================== Active Contour Model -==================================================== +==================== + The active contour model is a method to fit open or closed splines to lines or edges in an image. It works by minimising an energy that is in part defined by the image and part by the spline's shape: length and smoothness. The @@ -18,8 +19,8 @@ smooth images a bit before analyzing, as done in the following examples. International Journal of Computer Vision 1 (4): 321 (1988). We initialize a circle around the astronaut's face and use the default boundary -condition `bc='periodic'` to fit a closed curve. The default parameters -`w_line=0, w_edge=1` will make the curve search towards edges, such as the +condition ``bc='periodic'`` to fit a closed curve. The default parameters +``w_line=0, w_edge=1`` will make the curve search towards edges, such as the boundaries of the face. """ @@ -39,7 +40,7 @@ y = 100 + 100*np.sin(s) init = np.array([x, y]).T snake = active_contour(gaussian_filter(img, 3), - init, alpha=0.015, beta=10, gamma=0.001) + init, alpha=0.015, beta=10, gamma=0.001) fig = plt.figure(figsize=(7, 7)) ax = fig.add_subplot(111) diff --git a/skimage/segmentation/__init__.py b/skimage/segmentation/__init__.py index 103ca5a4..63f7d66d 100644 --- a/skimage/segmentation/__init__.py +++ b/skimage/segmentation/__init__.py @@ -9,7 +9,7 @@ from ._join import join_segmentations, relabel_from_one, relabel_sequential __all__ = ['random_walker', - 'active_contour', + 'active_contour', 'felzenszwalb', 'slic', 'quickshift', diff --git a/skimage/segmentation/active_contour_model.py b/skimage/segmentation/active_contour_model.py index c6ee27f2..b4d61904 100644 --- a/skimage/segmentation/active_contour_model.py +++ b/skimage/segmentation/active_contour_model.py @@ -5,6 +5,7 @@ import scipy.linalg from scipy.interpolate import RectBivariateSpline, interp2d from skimage.filters import sobel + def active_contour(image, snake, alpha=0.01, beta=0.1, w_line=0, w_edge=1, gamma=0.01, bc='periodic', max_px_move=1.0, @@ -18,27 +19,29 @@ def active_contour(image, snake, alpha=0.01, beta=0.1, Parameters ---------- image : (N, M) or (N, M, 3) ndarray - Input image + Input image. snake : (N, 2) ndarray - Initialisation of snake. + Initialisation coordinates of snake. For periodic snakes, it should + not include duplicate endpoints. alpha : float, optional - Snake length shape parameter + Snake length shape parameter. Higher values makes snake contract + faster. beta : float, optional - Snake smoothness shape parameter + Snake smoothness shape parameter. Higher values makes snake smoother. w_line : float, optional Controls attraction to brightness. Use negative values to attract to - dark regions + dark regions. w_edge : float, optional Controls attraction to edges. Use negative values to repel snake from edges. gamma : float, optional - Excpliti time stepping parameter. + Explicit time stepping parameter. bc : {'periodic', 'free', 'fixed'}, optional Boundary conditions for worm. '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. + 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. max_px_move : float, optional Maximum pixel distance to move per iteration. max_iterations : int, optional @@ -54,29 +57,36 @@ def active_contour(image, snake, alpha=0.01, beta=0.1, References ---------- .. [1] Kass, M.; Witkin, A.; Terzopoulos, D. "Snakes: Active contour - models". International Journal of Computer Vision 1 (4): 321 (1988). + models". International Journal of Computer Vision 1 (4): 321 + (1988). Examples -------- >>> from skimage.draw import circle_perimeter >>> from skimage.filters import gaussian_filter - >>> # Create and smooth image: + + Create and smooth image: + >>> img = np.zeros((100, 100)) >>> rr, cc = circle_perimeter(35, 45, 25) >>> img[rr, cc] = 1 >>> img = gaussian_filter(img, 2) - >>>> # Initiliaze spline: + + Initiliaze spline: + >>> s = np.linspace(0, 2*np.pi,100) >>> init = 50*np.array([np.cos(s), np.sin(s)]).T+50 - >>> # Fit spline to image: + + Fit spline to image: + >>> snake = active_contour_model(img, init, w_edge=0, w_line=1) >>> int(np.mean(np.sqrt((45-snake[:, 0])**2 + (35-snake[:, 1])**2))) 25 """ - scipy_version = map(int, scipy.__version__.split('.')) - new_scipy = scipy_version[0]>0 or \ - (scipy_version[0]==0 and scipy_version[1]>=14) + scipy_version = list(map(int, scipy.__version__.split('.'))) + new_scipy = scipy_version[0] > 0 or \ + (scipy_version[0] == 0 and scipy_version[1] >= 14) if not new_scipy: warnings.warn('You are using an old version of scipy. ' 'Upgrading to a version newer than 0.14.0 ' @@ -130,16 +140,16 @@ def active_contour(image, snake, alpha=0.01, beta=0.1, xsave = np.empty((convergence_order, len(x))) ysave = np.empty((convergence_order, len(x))) - # Build snake shape matrix + # Build snake shape matrix for Euler equation n = len(x) a = np.roll(np.eye(n), -1, axis=0) \ + np.roll(np.eye(n), -1, axis=1) \ - - 2*np.eye(n) + - 2*np.eye(n) # second order derivative, central difference b = np.roll(np.eye(n), -2, axis=0) \ + np.roll(np.eye(n), -2, axis=1) \ - 4*np.roll(np.eye(n), -1, axis=0) \ - 4*np.roll(np.eye(n), -1, axis=1) \ - + 6*np.eye(n) + + 6*np.eye(n) # fourth order derivative, central difference A = -alpha*a + beta*b # Impose boundary conditions different from periodic: @@ -216,7 +226,7 @@ def active_contour(image, snake, alpha=0.01, beta=0.1, ysave[j, :] = y else: dist = np.min(np.max(np.abs(xsave-x[None, :]) - + np.abs(ysave-y[None, :]), 1)) + + np.abs(ysave-y[None, :]), 1)) if dist < convergence: break diff --git a/skimage/segmentation/tests/test_active_contour_model.py b/skimage/segmentation/tests/test_active_contour_model.py index b53995bd..b95f87d7 100644 --- a/skimage/segmentation/tests/test_active_contour_model.py +++ b/skimage/segmentation/tests/test_active_contour_model.py @@ -5,6 +5,7 @@ from skimage.filters import gaussian_filter from skimage.segmentation import active_contour from numpy.testing import assert_equal, assert_allclose, assert_raises + def test_periodic_reference(): img = data.astronaut() img = rgb2gray(img) @@ -13,7 +14,7 @@ def test_periodic_reference(): y = 100 + 100*np.sin(s) init = np.array([x, y]).T snake = active_contour(gaussian_filter(img, 3), init, - alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001) + alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001) refx = [299, 298, 298, 298, 298, 297, 297, 296, 296, 295] refy = [98, 99, 100, 101, 102, 103, 104, 105, 106, 108] assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx) @@ -81,16 +82,16 @@ def test_end_points(): y = 100 + 100*np.sin(s) init = np.array([x, y]).T snake = active_contour(gaussian_filter(img, 3), init, - bc='periodic', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001, - max_iterations=100) + bc='periodic', alpha=0.015, beta=10, w_line=0, w_edge=1, + gamma=0.001, max_iterations=100) assert np.sum(np.abs(snake[0, :]-snake[-1, :])) < 2 snake = active_contour(gaussian_filter(img, 3), init, - bc='free', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001, - max_iterations=100) + bc='free', alpha=0.015, beta=10, w_line=0, w_edge=1, + gamma=0.001, max_iterations=100) assert np.sum(np.abs(snake[0, :]-snake[-1, :])) > 2 snake = active_contour(gaussian_filter(img, 3), init, - bc='fixed', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001, - max_iterations=100) + bc='fixed', alpha=0.015, beta=10, w_line=0, w_edge=1, + gamma=0.001, max_iterations=100) assert_allclose(snake[0, :], [x[0], y[0]], atol=1e-5)