From d3024b5c738c47bdab89235bced1fb12c1656e93 Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Wed, 21 Jan 2015 16:36:58 -0600 Subject: [PATCH] No SegFault when unwrapping a 2D fully masked array The unwrap_phase function no longer causes a Segmentation fault when passed a 2D array in which all elements are masked. --- skimage/restoration/tests/test_unwrap.py | 20 ++++++++++++++++++++ skimage/restoration/unwrap_2d_ljmu.c | 9 +++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/skimage/restoration/tests/test_unwrap.py b/skimage/restoration/tests/test_unwrap.py index ca4cfe32..4353dc20 100644 --- a/skimage/restoration/tests/test_unwrap.py +++ b/skimage/restoration/tests/test_unwrap.py @@ -166,5 +166,25 @@ def test_unwrap_2d_compressed_mask(): 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 + + if __name__ == "__main__": run_module_suite() 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);