From ba328e753137498aa639e9d34f6f1e01012c9b32 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Sun, 7 Feb 2016 20:21:10 +0000 Subject: [PATCH] MAINT: address review comments Use recommended idioms in the .py wrapper --- skimage/morphology/_skeletonize_3d.py | 28 ++++++++----------- .../morphology/tests/test_skeletonize_3d.py | 10 ++++++- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/skimage/morphology/_skeletonize_3d.py b/skimage/morphology/_skeletonize_3d.py index d041f643..768ec08b 100644 --- a/skimage/morphology/_skeletonize_3d.py +++ b/skimage/morphology/_skeletonize_3d.py @@ -1,7 +1,7 @@ from __future__ import division, print_function, absolute_import import numpy as np -from ..util import img_as_ubyte +from ..util import img_as_ubyte, crop from ._skeletonize_3d_cy import _compute_thin_image @@ -46,24 +46,19 @@ def skeletonize_3d(img): Computer Vision, Graphics, and Image Processing, 56(6):462-478, 1994. """ - # make sure the image is 3D or 2D (if it is, temporarily upcast to 3D) + # make sure the image is 3D or 2D if img.ndim < 2 or img.ndim > 3: raise ValueError('expect 2D, got ndim = %s' % img.ndim) img = np.ascontiguousarray(img) img = img_as_ubyte(img, force_copy=False) - # make an in image 3D pad w/ zeros to simplify dealing w/ boundaries - # NB: careful to not clobber the original *and* minimize copying + # make an in image 3D and pad it w/ zeros to simplify dealing w/ boundaries + # NB: careful here to not clobber the original *and* minimize copying + img_o = img if img.ndim == 2: - - if img.shape[0] == 1 or img.shape[1] == 1: - # nothing to do, image is already thin. Bail out. - return img.copy() - - img_o = np.pad(img[None, ...], pad_width=1, mode='constant') - else: - img_o = np.pad(img, pad_width=1, mode='constant') + img_o = img[np.newaxis, ...] + img_o = np.pad(img_o, pad_width=1, mode='constant') # normalize to binary maxval = img_o.max() @@ -72,9 +67,10 @@ def skeletonize_3d(img): # do the computation img_o = np.asarray(_compute_thin_image(img_o)) - # clip it back and restore the original intensity range - img_o = img_o[1:-1, 1:-1, 1:-1] - img_o = img_o.squeeze() + # crop it back and restore the original intensity range + img_o = crop(img_o, crop_width=1) + if img.ndim == 2: + img_o = img_o[0] img_o *= maxval - return img_o + return img_o \ No newline at end of file diff --git a/skimage/morphology/tests/test_skeletonize_3d.py b/skimage/morphology/tests/test_skeletonize_3d.py index d373f0d5..d5859a5d 100644 --- a/skimage/morphology/tests/test_skeletonize_3d.py +++ b/skimage/morphology/tests/test_skeletonize_3d.py @@ -27,6 +27,13 @@ def test_skeletonize_wrong_dim(): assert_raises(ValueError, skeletonize_3d, im) +def test_skeletonize_1D(): + # a corner case of an image of a shape(1, N) + im = np.ones((5, 1), dtype=np.uint8) + res = skeletonize_3d(im) + assert_equal(res, im) + + def test_skeletonize_no_foreground(): im = np.zeros((5, 5), dtype=np.uint8) result = skeletonize_3d(im) @@ -80,7 +87,8 @@ def test_input(): # check that the input is not clobbered # for 2D and 3D images of varying dtypes imgs = [np.ones((8, 8), dtype=float), np.ones((4, 8, 8), dtype=float), - np.ones((8, 8), dtype=np.uint8), np.ones((4, 8, 8), dtype=np.uint8)] + np.ones((8, 8), dtype=np.uint8), np.ones((4, 8, 8), dtype=np.uint8), + np.ones((8, 8), dtype=bool), np.ones((4, 8, 8), dtype=bool)] for img in imgs: yield check_input, img