def check_wrap_around(ndim, axis): # create a ramp, but with the last pixel along axis equalling the first elements = 100 ramp = np.linspace(0, 12 * np.pi, elements) ramp[-1] = ramp[0] image = ramp.reshape( tuple([elements if n == axis else 1 for n in range(ndim)])) image_wrapped = np.angle(np.exp(1j * image)) index_first = tuple([0] * ndim) index_last = tuple([-1 if n == axis else 0 for n in range(ndim)]) # unwrap the image without wrap around with warnings.catch_warnings(): # We do not want warnings about length 1 dimensions warnings.simplefilter("ignore") image_unwrap_no_wrap_around = unwrap_phase(image_wrapped) print('endpoints without wrap_around:', image_unwrap_no_wrap_around[index_first], image_unwrap_no_wrap_around[index_last]) # without wrap around, the endpoints of the image should differ assert abs(image_unwrap_no_wrap_around[index_first] - image_unwrap_no_wrap_around[index_last]) > np.pi # unwrap the image with wrap around wrap_around = [n == axis for n in range(ndim)] with warnings.catch_warnings(): # We do not want warnings about length 1 dimensions warnings.simplefilter("ignore") image_unwrap_wrap_around = unwrap_phase(image_wrapped, wrap_around) print('endpoints with wrap_around:', image_unwrap_wrap_around[index_first], image_unwrap_wrap_around[index_last]) # with wrap around, the endpoints of the image should be equal assert_almost_equal(image_unwrap_wrap_around[index_first], image_unwrap_wrap_around[index_last])
def test_mask(): length = 100 ramps = [ np.linspace(0, 4 * np.pi, length), np.linspace(0, 8 * np.pi, length), np.linspace(0, 6 * np.pi, length) ] image = np.vstack(ramps) mask_1d = np.ones((length, ), dtype=np.bool) mask_1d[0] = mask_1d[-1] = False for i in range(len(ramps)): # mask all ramps but the i'th one mask = np.zeros(image.shape, dtype=np.bool) mask |= mask_1d.reshape(1, -1) mask[i, :] = False # unmask i'th ramp image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask) image_unwrapped = unwrap_phase(image_wrapped) image_unwrapped -= image_unwrapped[0, 0] # remove phase shift # The end of the unwrapped array should have value equal to the # endpoint of the unmasked ramp assert_array_almost_equal(image_unwrapped[:, -1], image[i, -1]) # Same tests, but forcing use of the 3D unwrapper by reshaping image_wrapped_3d = image_wrapped.reshape((1, ) + image_wrapped.shape) image_unwrapped_3d = unwrap_phase(image_wrapped_3d) image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0] # remove phase shift assert_array_almost_equal(image_unwrapped_3d[:, :, -1], image[i, -1])
def check_wrap_around(ndim, axis): # create a ramp, but with the last pixel along axis equalling the first elements = 100 ramp = np.linspace(0, 12 * np.pi, elements) ramp[-1] = ramp[0] image = ramp.reshape(tuple([elements if n == axis else 1 for n in range(ndim)])) image_wrapped = np.angle(np.exp(1j * image)) index_first = tuple([0] * ndim) index_last = tuple([-1 if n == axis else 0 for n in range(ndim)]) # unwrap the image without wrap around with warnings.catch_warnings(): # We do not want warnings about length 1 dimensions warnings.simplefilter("ignore") image_unwrap_no_wrap_around = unwrap_phase(image_wrapped) print('endpoints without wrap_around:', image_unwrap_no_wrap_around[index_first], image_unwrap_no_wrap_around[index_last]) # without wrap around, the endpoints of the image should differ assert abs(image_unwrap_no_wrap_around[index_first] - image_unwrap_no_wrap_around[index_last]) > np.pi # unwrap the image with wrap around wrap_around = [n == axis for n in range(ndim)] with warnings.catch_warnings(): # We do not want warnings about length 1 dimensions warnings.simplefilter("ignore") image_unwrap_wrap_around = unwrap_phase(image_wrapped, wrap_around) print('endpoints with wrap_around:', image_unwrap_wrap_around[index_first], image_unwrap_wrap_around[index_last]) # with wrap around, the endpoints of the image should be equal assert_almost_equal(image_unwrap_wrap_around[index_first], image_unwrap_wrap_around[index_last])
def test_mask(): length = 100 ramps = [np.linspace(0, 4 * np.pi, length), np.linspace(0, 8 * np.pi, length), np.linspace(0, 6 * np.pi, length)] image = np.vstack(ramps) mask_1d = np.ones((length,), dtype=np.bool) mask_1d[0] = mask_1d[-1] = False for i in range(len(ramps)): # mask all ramps but the i'th one mask = np.zeros(image.shape, dtype=np.bool) mask |= mask_1d.reshape(1, -1) mask[i, :] = False # unmask i'th ramp image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask) image_unwrapped = unwrap_phase(image_wrapped) image_unwrapped -= image_unwrapped[0, 0] # remove phase shift # The end of the unwrapped array should have value equal to the # endpoint of the unmasked ramp assert_array_almost_equal(image_unwrapped[:, -1], image[i, -1]) # Same tests, but forcing use of the 3D unwrapper by reshaping image_wrapped_3d = image_wrapped.reshape((1,) + image_wrapped.shape) image_unwrapped_3d = unwrap_phase(image_wrapped_3d) image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0] # remove phase shift assert_array_almost_equal(image_unwrapped_3d[:, :, -1], image[i, -1])
def check_unwrap(image, mask=None): image_wrapped = np.angle(np.exp(1j * image)) if not mask is None: print('Testing a masked image') image = np.ma.array(image, mask=mask) image_wrapped = np.ma.array(image_wrapped, mask=mask) image_unwrapped = unwrap_phase(image_wrapped) assert_phase_almost_equal(image_unwrapped, image)
""" import numpy as np from matplotlib import pyplot as plt from skimage import data, img_as_float, color, exposure from skimage.exposure import unwrap_phase # Load an image as a floating-point grayscale image = color.rgb2gray(img_as_float(data.chelsea())) # Scale the image to [0, 4*pi] image = exposure.rescale_intensity(image, out_range=(0, 4 * np.pi)) # Create a phase-wrapped image in the interval [-pi, pi) image_wrapped = np.angle(np.exp(1j * image)) # Perform phase unwrapping image_unwrapped = unwrap_phase(image_wrapped) plt.figure() plt.subplot(221) plt.title('Original') plt.imshow(image, cmap='gray', vmin=0, vmax=4 * np.pi) plt.colorbar() plt.subplot(222) plt.title('Wrapped phase') plt.imshow(image_wrapped, cmap='gray', vmin=-np.pi, vmax=np.pi) plt.colorbar() plt.subplot(223) plt.title('After phase unwrapping') plt.imshow(image_unwrapped, cmap='gray')