Esempio n. 1
0
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 = cp.asarray(camera())
    shift = (-7, 12)
    shifted = cp.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])

    reference_image = cp.asarray(reference_image)
    shifted = cp.asarray(shifted)
    measured_shift = masked_register_translation(
        reference_image,
        shifted,
        cp.ones(ref_mask.shape, dtype=ref_mask.dtype),
        cp.ones(shifted_mask.shape, dtype=shifted_mask.dtype),
    )
    cp.testing.assert_array_equal(measured_shift, -cp.asarray(shift))
Esempio n. 2
0
def test_4d_input_subpixel():
    phantom = img_as_float(cp.asarray(binary_blobs(length=32, n_dim=4)))
    reference_image = fft.fftn(phantom)
    subpixel_shift = (-2.3, 1.7, 5.4, -3.2)
    shifted_image = fourier_shift(reference_image, subpixel_shift)
    result, error, diffphase = phase_cross_correlation(
        reference_image, shifted_image, upsample_factor=10, space="fourier"
    )
    assert_allclose(result, -cp.asarray(subpixel_shift), atol=0.05)
Esempio n. 3
0
def test_3d_input():
    phantom = img_as_float(cp.asarray(binary_blobs(length=32, n_dim=3)))
    reference_image = fft.fftn(phantom)
    shift = (-2.0, 1.0, 5.0)
    shifted_image = fourier_shift(reference_image, shift)

    result, error, diffphase = phase_cross_correlation(
        reference_image, shifted_image, space="fourier"
    )
    assert_allclose(result, -cp.asarray(shift), atol=0.05)

    # subpixel precision now available for 3-D data

    subpixel_shift = (-2.3, 1.7, 5.4)
    shifted_image = fourier_shift(reference_image, subpixel_shift)
    result, error, diffphase = phase_cross_correlation(
        reference_image, shifted_image, upsample_factor=100, space="fourier"
    )
    assert_allclose(result, -cp.asarray(subpixel_shift), atol=0.05)
Esempio n. 4
0
def test_subpixel_precision():
    reference_image = fft.fftn(cp.asarray(camera()))
    subpixel_shift = (-2.4, 1.32)
    shifted_image = fourier_shift(reference_image, subpixel_shift)

    # subpixel precision
    result, error, diffphase = phase_cross_correlation(
        reference_image, shifted_image, upsample_factor=100, space="fourier"
    )
    assert_allclose(result[:2], -cp.asarray(subpixel_shift), atol=0.05)
Esempio n. 5
0
def test_size_one_dimension_input():
    # take a strip of the input image
    reference_image = fft.fftn(cp.asarray(camera())[:, 15]).reshape((-1, 1))
    subpixel_shift = (-2.4, 4)
    shifted_image = fourier_shift(reference_image, subpixel_shift)

    # subpixel precision
    result, error, diffphase = phase_cross_correlation(
        reference_image, shifted_image, upsample_factor=20, space="fourier"
    )
    assert_allclose(result[:2], -cp.asarray((-2.4, 0)), atol=0.05)
Esempio n. 6
0
def test_correlation():
    reference_image = fft.fftn(cp.asarray(camera()))
    shift = (-7, 12)
    shifted_image = fourier_shift(
        reference_image, shift
    )  # TODO: replace with CUDA fourier_shift

    # pixel precision
    result, error, diffphase = phase_cross_correlation(
        reference_image, shifted_image, space="fourier"
    )
    assert_allclose(result[:2], -cp.asarray(shift))
Esempio n. 7
0
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 = cp.asarray(camera())
    shift = (-7, 12)
    shifted = cp.real(
        fft.ifft2(fourier_shift(fft.fft2(reference_image), shift)))
    trivial_mask = cp.ones_like(reference_image)

    nonmasked_result, *_ = phase_cross_correlation(reference_image, shifted)
    masked_result = masked_register_translation(reference_image,
                                                shifted,
                                                trivial_mask,
                                                overlap_ratio=0.1)

    cp.testing.assert_array_equal(nonmasked_result, masked_result)
Esempio n. 8
0
def test_masked_registration_random_masks():
    """masked_register_translation should be able to register translations
    between images even with random masks."""
    # See random number generator for reproducible results
    np.random.seed(23)

    reference_image = cp.asarray(camera())
    shift = (-7, 12)
    shifted = np.real(
        fft.ifft2(fourier_shift(fft.fft2(reference_image), shift)))

    # 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])
    ref_mask = cp.asarray(ref_mask)
    shifted_mask = cp.asarray(shifted_mask)

    measured_shift = masked_register_translation(reference_image, shifted,
                                                 ref_mask, shifted_mask)
    cp.testing.assert_array_equal(measured_shift, -cp.asarray(shift))