From 96c293ca85e026499dfaef96971ddddbd18541a2 Mon Sep 17 00:00:00 2001 From: Gregor Thalhammer Date: Sun, 13 May 2012 23:38:38 +0200 Subject: [PATCH] unwrap: unified 2d-3d interface, 2d tested ok, --- ...rapper_with_mask_and_wrap_around_option.c} | 8 +- ...wrapper_with_mask_and_wrap_around_option.c | 2 +- unwrap2D/test_unwrap.py | 73 +++++++++++++++---- unwrap2D/unwrap.py | 28 +++++-- unwrap2D/unwrap2D.pyx | 2 +- unwrap2D/unwrap3D.pyx | 2 +- 6 files changed, 85 insertions(+), 30 deletions(-) rename unwrap2D/{Hussein_3D_unwrapper_with_mask_and_wrap_around_option.cpp => Hussein_3D_unwrapper_with_mask_and_wrap_around_option.c} (99%) diff --git a/unwrap2D/Hussein_3D_unwrapper_with_mask_and_wrap_around_option.cpp b/unwrap2D/Hussein_3D_unwrapper_with_mask_and_wrap_around_option.c similarity index 99% rename from unwrap2D/Hussein_3D_unwrapper_with_mask_and_wrap_around_option.cpp rename to unwrap2D/Hussein_3D_unwrapper_with_mask_and_wrap_around_option.c index 84b0b4ab..d7524d7c 100644 --- a/unwrap2D/Hussein_3D_unwrapper_with_mask_and_wrap_around_option.cpp +++ b/unwrap2D/Hussein_3D_unwrapper_with_mask_and_wrap_around_option.c @@ -1015,7 +1015,7 @@ unwrap3D(float* wrapped_volume, float* unwrapped_volume, unsigned char* input_ma int volume_width, int volume_height, int volume_depth, int wrap_around_x, int wrap_around_y, int wrap_around_z) { - params_t params = {TWOPI, 0, 0, 0, 0} + params_t params = {TWOPI, wrap_around_x, wrap_around_y, wrap_around_z, 0}; unsigned char *extended_mask; int volume_size = volume_height * volume_width * volume_depth; int No_of_Edges_initially = 3 * volume_width * volume_height * volume_depth; @@ -1025,8 +1025,8 @@ unwrap3D(float* wrapped_volume, float* unwrapped_volume, unsigned char* input_ma EDGE *edge = (EDGE *) calloc(No_of_Edges_initially, sizeof(EDGE));; extend_mask(input_mask, extended_mask, volume_width, volume_height, volume_depth, ¶ms); - initialiseVOXELs(WrappedVolume, input_mask, extended_mask, voxel, volume_width, volume_height, volume_depth); - calculate_reliability(WrappedVolume, voxel, volume_width, volume_height, volume_depth, ¶ms); + initialiseVOXELs(wrapped_volume, input_mask, extended_mask, voxel, volume_width, volume_height, volume_depth); + calculate_reliability(wrapped_volume, voxel, volume_width, volume_height, volume_depth, ¶ms); horizontalEDGEs(voxel, edge, volume_width, volume_height, volume_depth, ¶ms); verticalEDGEs(voxel, edge, volume_width, volume_height, volume_depth, ¶ms); normalEDGEs(voxel, edge, volume_width, volume_height, volume_depth, ¶ms); @@ -1041,7 +1041,7 @@ unwrap3D(float* wrapped_volume, float* unwrapped_volume, unsigned char* input_ma maskVolume(voxel, input_mask, volume_width, volume_height, volume_depth); //copy the volume from VOXELM structure to the unwrapped phase array passed to this function - returnVolume(voxel, UnwrappedVolume, volume_width, volume_height, volume_depth); + returnVolume(voxel, unwrapped_volume, volume_width, volume_height, volume_depth); free(edge); free(voxel); diff --git a/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c b/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c index 131ec517..67d61c51 100644 --- a/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c +++ b/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c @@ -684,7 +684,7 @@ unwrap2D(float* wrapped_image, float* UnwrappedImage, unsigned char* input_mask, int image_width, int image_height, int wrap_around_x, int wrap_around_y) { - params_t params = {TWOPI, 0, 0, 0}; + params_t params = {TWOPI, wrap_around_x, wrap_around_y, 0}; unsigned char *extended_mask; int image_size = image_height * image_width; int No_of_Edges_initially = 2 * image_width * image_height; diff --git a/unwrap2D/test_unwrap.py b/unwrap2D/test_unwrap.py index af94c934..e297f1ce 100644 --- a/unwrap2D/test_unwrap.py +++ b/unwrap2D/test_unwrap.py @@ -1,30 +1,51 @@ from numpy.testing import * -from unwrap import unwrap2D +from unwrap import unwrap import numpy as np -from numpy import outer, arange, ones, abs, empty, power, indices - -import numpy.ma as ma def test_unwrap2D(): - nx, ny = 32, 32 + nx, ny = 16, 32 x = np.arange(nx) y = np.arange(ny) - x.shape = (1,-1) - y.shape = (-1,1) + x.shape = (-1,1) + y.shape = (1,-1) z = np.exp(1j*x*0.2*np.pi) * np.exp(1j*y*0.1*np.pi) phi_w = np.angle(z) - phi = unwrap2D(phi_w) + phi = unwrap(phi_w, + wrap_around_axis_0 = False) mask = 0*np.ones((nx, ny), dtype = np.uint8) mask[4:16, 4:16] = 1 - phi_w_ma = ma.array(phi_w, dtype = np.float32, mask = mask) - phi_ma = unwrap2D(phi_w_ma) + phi_w_ma = np.ma.array(phi_w, dtype = np.float32, mask = mask) + phi_ma = unwrap(phi_w_ma) - return (phi_w/(np.pi*2), phi/(np.pi*2), + return (phi/(np.pi*2), + phi_w/(np.pi*2), phi/(np.pi*2), phi_w_ma/(np.pi*2), phi_ma/(np.pi*2),) +def test_unwrap3D(): + x, y, z = np.ogrid[:8, :8, :4] + + phi = 2*np.pi*(x*0.2 + y*0.1 + z*0.05) + phi_wrapped = np.angle(np.exp(1j*phi)) + phi_unwrapped = unwrap(phi_wrapped) + + mask = np.zeros_like(phi, dtype = np.uint8) + mask[4:6, 4:6, 1:3] = 1 + phi_wrapped_masked = np.ma.array(phi_wrapped, dtype = np.float32, mask = mask) + phi_unwrapped_masked = unwrap(phi_wrapped_masked) + + s = np.round(phi_unwrapped[0,0,0]/(2*np.pi)) + assert_array_almost_equal(phi/(2*np.pi), phi_unwrapped/(2*np.pi) - s, decimal = 5) + + assert_array_almost_equal(phi + 2*np.pi*s, phi_unwrapped_masked, decimal = 5) + + return (phi/(np.pi*2), + phi_wrapped/(np.pi*2), phi_unwrapped/(np.pi*2), + phi_wrapped_masked/(np.pi*2), phi_unwrapped_masked/(np.pi*2),) + + # class test_unwrap(TestCase): # def test_simple2d(self, level=1): @@ -52,15 +73,35 @@ def test_unwrap2D(): if __name__=="__main__": #NumpyTest().run() import matplotlib.pyplot as plt - p1,p2,p3,p4 = test_unwrap2D() + + p0,p1,p2,p3,p4 = test_unwrap2D() + plt.figure(1) plt.clf() - plt.subplot(221) + plt.subplot(322) + plt.imshow(p0,interpolation = 'nearest') + plt.subplot(323) plt.imshow(p1,interpolation = 'nearest') - plt.subplot(222) + plt.subplot(324) plt.imshow(p2, interpolation = 'nearest') - plt.subplot(223) + plt.subplot(325) plt.imshow(p3, interpolation = 'nearest') - plt.subplot(224) + plt.subplot(326) plt.imshow(p4, interpolation = 'nearest') plt.draw() + + + p0,p1,p2,p3,p4 = test_unwrap3D() + plt.figure(2) + plt.clf() + plt.subplot(322) + plt.imshow(p0[:,:,0],interpolation = 'nearest') + plt.subplot(323) + plt.imshow(p1[:,:,0],interpolation = 'nearest') + plt.subplot(324) + plt.imshow(p2[:,:,0], interpolation = 'nearest') + plt.subplot(325) + plt.imshow(p3[:,:,0], interpolation = 'nearest') + plt.subplot(326) + plt.imshow(p4[:,:,0], interpolation = 'nearest') + plt.draw() plt.show() diff --git a/unwrap2D/unwrap.py b/unwrap2D/unwrap.py index 8a61a779..a4fb7d2c 100644 --- a/unwrap2D/unwrap.py +++ b/unwrap2D/unwrap.py @@ -1,15 +1,29 @@ import numpy as np -from unwrap2D import _unwrap2D -def unwrap2D(wrapped_array, wrap_around_x = False, wrap_around_y = False): +def unwrap(wrapped_array, + wrap_around_axis_0 = False, + wrap_around_axis_1 = False, + wrap_around_axis_2 = False): + wrapped_array = np.require(wrapped_array, np.float32, ['C']) + if wrapped_array.ndim not in [2,3]: + raise ValueError('input array needs to have 2 or 3 dimensions') + wrapped_array_masked = np.ma.asarray(wrapped_array) unwrapped_array = np.empty_like(wrapped_array_masked.data) - - _unwrap2D(wrapped_array_masked.data, - np.ma.getmaskarray(wrapped_array_masked).astype(np.uint8), - unwrapped_array, - wrap_around_x, wrap_around_y) + if wrapped_array.ndim == 2: + import unwrap2D + unwrap2D._unwrap2D(wrapped_array_masked.data, + np.ma.getmaskarray(wrapped_array_masked).astype(np.uint8), + unwrapped_array, + bool(wrap_around_axis_0), bool(wrap_around_axis_1)) + elif wrapped_array.ndim == 3: + import unwrap3D + unwrap3D._unwrap3D(wrapped_array_masked.data, + np.ma.getmaskarray(wrapped_array_masked).astype(np.uint8), + unwrapped_array, + bool(wrap_around_axis_0), bool(wrap_around_axis_1), bool(wrap_around_axis_2)) + if np.ma.isMaskedArray(wrapped_array): return np.ma.array(unwrapped_array, mask = wrapped_array_masked.mask) else: diff --git a/unwrap2D/unwrap2D.pyx b/unwrap2D/unwrap2D.pyx index 2dd0b33c..c4ca7f31 100644 --- a/unwrap2D/unwrap2D.pyx +++ b/unwrap2D/unwrap2D.pyx @@ -13,7 +13,7 @@ def _unwrap2D(float[:,::1] array, unwrap2D(&array[0,0], &unwrapped_array[0,0], &mask[0,0], - array.shape[0], array.shape[1], + array.shape[1], array.shape[0], wrap_around_x, wrap_around_y, ) diff --git a/unwrap2D/unwrap3D.pyx b/unwrap2D/unwrap3D.pyx index 1df8a4d6..6d5507d1 100644 --- a/unwrap2D/unwrap3D.pyx +++ b/unwrap2D/unwrap3D.pyx @@ -11,7 +11,7 @@ def _unwrap3D(float[:,:,::1] array, unwrap3D(&array[0,0,0], &unwrapped_array[0,0,0], &mask[0,0,0], - array.shape[0], array.shape[1], array.shape[2], + array.shape[0], array.shape[1], array.shape[2], #TODO: check!!! wrap_around_x, wrap_around_y, wrap_around_z, )