diff --git a/skimage/morphology/_skeletonize_3d.py b/skimage/morphology/_skeletonize_3d.py index 0aa08979..2a419bf4 100644 --- a/skimage/morphology/_skeletonize_3d.py +++ b/skimage/morphology/_skeletonize_3d.py @@ -50,19 +50,19 @@ def skeletonize_3d(img): if img.ndim < 2 or img.ndim > 3: raise ValueError('expect 2D, got ndim = %s' % img.ndim) - img = img_as_ubyte(img) img = np.ascontiguousarray(img) + img = img_as_ubyte(img, force_copy=False) - img = img.copy() + # make an in image 3D pad w/ zeros to simplify dealing w/ boundaries + # NB: careful to not clobber the original *and* minimize copying if img.ndim == 2: - img = img[None, ...] + img_o = np.pad(img[None, ...], pad_width=1, mode='constant') + else: + img_o = np.pad(img, pad_width=1, mode='constant') # normalize to binary - maxval = img.max() - img[img != 0] = 1 - - # pad w/ zeros to simplify dealing w/ boundaries - img_o = np.pad(img, pad_width=1, mode='constant') + maxval = img_o.max() + img_o[img_o != 0] = 1 # do the computation img_o = np.asarray(_compute_thin_image(img_o)) diff --git a/skimage/morphology/tests/test_skeletonize_3d.py b/skimage/morphology/tests/test_skeletonize_3d.py index a3f90b9c..dd1996f0 100644 --- a/skimage/morphology/tests/test_skeletonize_3d.py +++ b/skimage/morphology/tests/test_skeletonize_3d.py @@ -76,6 +76,25 @@ def test_dtype_conv(): img_as_ubyte(img).max()) # the intensity range is preserved +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)] + for img in imgs: + yield check_input, img + + +def check_input(img): + orig = img.copy() + with warnings.catch_warnings(): + # UserWarning for possible precision loss, expected + warnings.simplefilter('ignore', UserWarning) + res = skeletonize_3d(img) + assert_equal(img, orig) + + def test_skeletonize_num_neighbours(): # an empty image image = np.zeros((300, 300))