mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-06 04:44:19 +08:00
MAINT: skel3d: do not copy more than necessary
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user