MAINT: skel3d: do not copy more than necessary

This commit is contained in:
Evgeni Burovski
2016-02-04 11:07:19 +00:00
parent 81d47328ea
commit 9648d71360
2 changed files with 27 additions and 8 deletions
+8 -8
View File
@@ -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))