def test_masked_registration_random_masks_non_equal_sizes(): """masked_register_translation should be able to register translations between images that are not the same size even with random masks.""" # See random number generator for reproducible results np.random.seed(23) reference_image = camera() shift = (-7, 12) shifted = np.real( fft.ifft2(fourier_shift(fft.fft2(reference_image), shift))) # Crop the shifted image shifted = shifted[64:-64, 64:-64] # Random masks with 75% of pixels being valid ref_mask = np.random.choice([True, False], reference_image.shape, p=[3 / 4, 1 / 4]) shifted_mask = np.random.choice([True, False], shifted.shape, p=[3 / 4, 1 / 4]) measured_shift = masked_register_translation( reference_image, shifted, reference_mask=np.ones_like(ref_mask), moving_mask=np.ones_like(shifted_mask)) assert_equal(measured_shift, -np.array(shift))
def test_masked_registration_padfield_data(): """ Masked translation registration should behave like in the original publication """ # Test translated from MATLABimplementation `MaskedFFTRegistrationTest` # file. You can find the source code here: # http://www.dirkpadfield.com/Home/MaskedFFTRegistrationCode.zip shifts = [(75, 75), (-130, 130), (130, 130)] for xi, yi in shifts: fixed_image = imread( fetch('registration/tests/data/OriginalX{:d}Y{:d}.png' ''.format(xi, yi))) moving_image = imread( fetch('registration/tests/data/TransformedX{:d}Y{:d}.png' ''.format(xi, yi))) # Valid pixels are 1 fixed_mask = (fixed_image != 0) moving_mask = (moving_image != 0) # Note that shifts in x and y and shifts in cols and rows shift_y, shift_x = masked_register_translation( fixed_image, moving_image, reference_mask=fixed_mask, moving_mask=moving_mask, overlap_ratio=0.1) # Note: by looking at the test code from Padfield's # MaskedFFTRegistrationCode repository, the # shifts were not xi and yi, but xi and -yi assert_equal((shift_x, shift_y), (-xi, yi))
def test_masked_registration_vs_phase_cross_correlation(): """masked_register_translation should give the same results as phase_cross_correlation in the case of trivial masks.""" reference_image = camera() shift = (-7, 12) shifted = np.real( fft.ifft2(fourier_shift(fft.fft2(reference_image), shift))) trivial_mask = np.ones_like(reference_image) nonmasked_result, *_ = phase_cross_correlation(reference_image, shifted) masked_result = masked_register_translation(reference_image, shifted, reference_mask=trivial_mask, overlap_ratio=1 / 10) assert_equal(nonmasked_result, masked_result)
def test_masked_registration_3d_contiguous_mask(): """masked_register_translation should be able to register translations between volumes with contiguous masks.""" ref_vol = brain()[:, ::2, ::2] offset = (1, -5, 10) # create square mask ref_mask = np.zeros_like(ref_vol, dtype=bool) ref_mask[:-2, 75:100, 75:100] = True ref_shifted = real_shift(ref_vol, offset) measured_offset = masked_register_translation( ref_vol, ref_shifted, reference_mask=ref_mask, moving_mask=ref_mask ) assert_equal(offset, -np.array(measured_offset))