diff --git a/skimage/restoration/tests/test_unwrap.py b/skimage/restoration/tests/test_unwrap.py index e628f4fe..f5ff2c76 100644 --- a/skimage/restoration/tests/test_unwrap.py +++ b/skimage/restoration/tests/test_unwrap.py @@ -131,6 +131,7 @@ def test_mask(): # The end of the unwrapped array should have value equal to the # endpoint of the unmasked ramp assert_array_almost_equal_nulp(image_unwrapped[:, -1], image[i, -1]) + assert np.ma.isMaskedArray(image_unwrapped) # Same tests, but forcing use of the 3D unwrapper by reshaping with expected_warnings(['length 1 dimension']): @@ -138,7 +139,7 @@ def test_mask(): image_wrapped_3d = image_wrapped.reshape(shape) image_unwrapped_3d = unwrap_phase(image_wrapped_3d) # remove phase shift - image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0] + image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0] assert_array_almost_equal_nulp(image_unwrapped_3d[:, :, -1], image[i, -1]) @@ -156,5 +157,51 @@ def test_unwrap_3d_middle_wrap_around(): unwrap = unwrap_phase(image, wrap_around=[False, True, False]) assert np.all(unwrap == 0) + +def test_unwrap_2d_compressed_mask(): + # ValueError when image is masked array with a compressed mask (no masked + # elments). GitHub issue #1346 + image = np.ma.zeros((10, 10)) + unwrap = unwrap_phase(image) + assert np.all(unwrap == 0) + + +def test_unwrap_2d_all_masked(): + # Segmentation fault when image is masked array with a all elements masked + # GitHub issue #1347 + # all elements masked + image = np.ma.zeros((10, 10)) + image[:] = np.ma.masked + unwrap = unwrap_phase(image) + assert np.ma.isMaskedArray(unwrap) + assert np.all(unwrap.mask) + + # 1 unmasked element, still zero edges + image = np.ma.zeros((10, 10)) + image[:] = np.ma.masked + image[0, 0] = 0 + unwrap = unwrap_phase(image) + assert np.ma.isMaskedArray(unwrap) + assert np.sum(unwrap.mask) == 99 # all but one masked + assert unwrap[0, 0] == 0 + + +def test_unwrap_3d_all_masked(): + # all elements masked + image = np.ma.zeros((10, 10, 10)) + image[:] = np.ma.masked + unwrap = unwrap_phase(image) + assert np.ma.isMaskedArray(unwrap) + assert np.all(unwrap.mask) + + # 1 unmasked element, still zero edges + image = np.ma.zeros((10, 10, 10)) + image[:] = np.ma.masked + image[0, 0, 0] = 0 + unwrap = unwrap_phase(image) + assert np.ma.isMaskedArray(unwrap) + assert np.sum(unwrap.mask) == 999 # all but one masked + assert unwrap[0, 0, 0] == 0 + if __name__ == "__main__": run_module_suite() diff --git a/skimage/restoration/unwrap.py b/skimage/restoration/unwrap.py index c6810229..14f1eafa 100644 --- a/skimage/restoration/unwrap.py +++ b/skimage/restoration/unwrap.py @@ -88,13 +88,14 @@ def unwrap_phase(image, wrap_around=False, seed=None): 'algorithm') if np.ma.isMaskedArray(image): - mask = np.require(image.mask, np.uint8, ['C']) - image = image.data + mask = np.require(np.ma.getmaskarray(image), np.uint8, ['C']) else: mask = np.zeros_like(image, dtype=np.uint8, order='C') - image_not_masked = np.asarray(image, dtype=np.double, order='C') - image_unwrapped = np.empty_like(image, dtype=np.double, order='C') + image_not_masked = np.asarray( + np.ma.getdata(image), dtype=np.double, order='C') + image_unwrapped = np.empty_like(image, dtype=np.double, order='C', + subok=False) if image.ndim == 1: unwrap_1d(image_not_masked, image_unwrapped) diff --git a/skimage/restoration/unwrap_2d_ljmu.c b/skimage/restoration/unwrap_2d_ljmu.c index 605c7993..690a8d77 100644 --- a/skimage/restoration/unwrap_2d_ljmu.c +++ b/skimage/restoration/unwrap_2d_ljmu.c @@ -718,10 +718,11 @@ void unwrap2D(double *wrapped_image, double *UnwrappedImage, horizontalEDGEs(pixel, edge, image_width, image_height, ¶ms); verticalEDGEs(pixel, edge, image_width, image_height, ¶ms); - // sort the EDGEs depending on their reiability. The PIXELs with higher - // relibility (small value) first - quicker_sort(edge, edge + params.no_of_edges - 1); - + if (params.no_of_edges != 0) { + // sort the EDGEs depending on their reiability. The PIXELs with higher + // relibility (small value) first + quicker_sort(edge, edge + params.no_of_edges - 1); + } // gather PIXELs into groups gatherPIXELs(edge, ¶ms); diff --git a/skimage/restoration/unwrap_3d_ljmu.c b/skimage/restoration/unwrap_3d_ljmu.c index be47f926..fdd9847a 100644 --- a/skimage/restoration/unwrap_3d_ljmu.c +++ b/skimage/restoration/unwrap_3d_ljmu.c @@ -1136,9 +1136,11 @@ void unwrap3D(double *wrapped_volume, double *unwrapped_volume, ¶ms); normalEDGEs(voxel, edge, volume_width, volume_height, volume_depth, ¶ms); - // sort the EDGEs depending on their reiability. The VOXELs with higher - // relibility (small value) first - quicker_sort(edge, edge + params.no_of_edges - 1); + if (params.no_of_edges != 0) { + // sort the EDGEs depending on their reiability. The VOXELs with higher + // relibility (small value) first + quicker_sort(edge, edge + params.no_of_edges - 1); + } // gather VOXELs into groups gatherVOXELs(edge, ¶ms);