"Fossies" - the Fresh Open Source Software Archive

Member "scikit-image-0.19.3/skimage/registration/tests/test_phase_cross_correlation.py" (12 Jun 2022, 6886 Bytes) of package /linux/misc/scikit-image-0.19.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_phase_cross_correlation.py": 0.19.2_vs_0.19.3.

    1 import numpy as np
    2 import pytest
    3 from numpy.testing import assert_allclose
    4 from scipy.ndimage import fourier_shift
    5 import scipy.fft as fft
    6 
    7 from skimage import img_as_float
    8 from skimage._shared._warnings import expected_warnings
    9 from skimage._shared.utils import _supported_float_type
   10 from skimage.data import camera, binary_blobs
   11 from skimage.registration._phase_cross_correlation import (
   12     phase_cross_correlation, _upsampled_dft
   13 )
   14 
   15 
   16 @pytest.mark.parametrize('normalization', [None, 'phase'])
   17 def test_correlation(normalization):
   18     reference_image = fft.fftn(camera())
   19     shift = (-7, 12)
   20     shifted_image = fourier_shift(reference_image, shift)
   21 
   22     # pixel precision
   23     result, _, _ = phase_cross_correlation(reference_image,
   24                                            shifted_image,
   25                                            space="fourier",
   26                                            normalization=normalization)
   27     assert_allclose(result[:2], -np.array(shift))
   28 
   29 
   30 @pytest.mark.parametrize('normalization', ['nonexisting'])
   31 def test_correlation_invalid_normalization(normalization):
   32     reference_image = fft.fftn(camera())
   33     shift = (-7, 12)
   34     shifted_image = fourier_shift(reference_image, shift)
   35 
   36     # pixel precision
   37     with pytest.raises(ValueError):
   38         phase_cross_correlation(reference_image,
   39                                 shifted_image,
   40                                 space="fourier",
   41                                 normalization=normalization)
   42 
   43 
   44 @pytest.mark.parametrize('normalization', [None, 'phase'])
   45 def test_subpixel_precision(normalization):
   46     reference_image = fft.fftn(camera())
   47     subpixel_shift = (-2.4, 1.32)
   48     shifted_image = fourier_shift(reference_image, subpixel_shift)
   49 
   50     # subpixel precision
   51     result, _, _ = phase_cross_correlation(reference_image,
   52                                            shifted_image,
   53                                            upsample_factor=100,
   54                                            space="fourier",
   55                                            normalization=normalization)
   56     assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
   57 
   58 
   59 @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
   60 def test_real_input(dtype):
   61     reference_image = camera().astype(dtype, copy=False)
   62     subpixel_shift = (-2.4, 1.32)
   63     shifted_image = fourier_shift(fft.fftn(reference_image), subpixel_shift)
   64     shifted_image = fft.ifftn(shifted_image).real.astype(dtype, copy=False)
   65 
   66     # subpixel precision
   67     result, error, diffphase = phase_cross_correlation(reference_image,
   68                                                        shifted_image,
   69                                                        upsample_factor=100)
   70     assert result.dtype == _supported_float_type(dtype)
   71     assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
   72 
   73 
   74 def test_size_one_dimension_input():
   75     # take a strip of the input image
   76     reference_image = fft.fftn(camera()[:, 15]).reshape((-1, 1))
   77     subpixel_shift = (-2.4, 4)
   78     shifted_image = fourier_shift(reference_image, subpixel_shift)
   79 
   80     # subpixel precision
   81     result, error, diffphase = phase_cross_correlation(reference_image,
   82                                                        shifted_image,
   83                                                        upsample_factor=20,
   84                                                        space="fourier")
   85     assert_allclose(result[:2], -np.array((-2.4, 0)), atol=0.05)
   86 
   87 
   88 def test_3d_input():
   89     phantom = img_as_float(binary_blobs(length=32, n_dim=3))
   90     reference_image = fft.fftn(phantom)
   91     shift = (-2., 1., 5.)
   92     shifted_image = fourier_shift(reference_image, shift)
   93 
   94     result, error, diffphase = phase_cross_correlation(reference_image,
   95                                                        shifted_image,
   96                                                        space="fourier")
   97     assert_allclose(result, -np.array(shift), atol=0.05)
   98 
   99     # subpixel precision now available for 3-D data
  100 
  101     subpixel_shift = (-2.3, 1.7, 5.4)
  102     shifted_image = fourier_shift(reference_image, subpixel_shift)
  103     result, error, diffphase = phase_cross_correlation(reference_image,
  104                                                        shifted_image,
  105                                                        upsample_factor=100,
  106                                                        space="fourier")
  107     assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
  108 
  109 
  110 def test_unknown_space_input():
  111     image = np.ones((5, 5))
  112     with pytest.raises(ValueError):
  113         phase_cross_correlation(
  114             image, image,
  115             space="frank")
  116 
  117 
  118 def test_wrong_input():
  119     # Dimensionality mismatch
  120     image = np.ones((5, 5, 1))
  121     template = np.ones((5, 5))
  122     with pytest.raises(ValueError):
  123         phase_cross_correlation(template, image)
  124 
  125     # Size mismatch
  126     image = np.ones((5, 5))
  127     template = np.ones((4, 4))
  128     with pytest.raises(ValueError):
  129         phase_cross_correlation(template, image)
  130 
  131     # NaN values in data
  132     image = np.ones((5, 5))
  133     image[0][0] = np.nan
  134     template = np.ones((5, 5))
  135     with expected_warnings(
  136         [
  137             r"invalid value encountered in true_divide"
  138             + r"|"
  139             + r"invalid value encountered in divide"
  140             + r"|\A\Z"
  141         ]
  142     ):
  143         with pytest.raises(ValueError):
  144             phase_cross_correlation(template, image, return_error=True)
  145 
  146 
  147 def test_4d_input_pixel():
  148     phantom = img_as_float(binary_blobs(length=32, n_dim=4))
  149     reference_image = fft.fftn(phantom)
  150     shift = (-2., 1., 5., -3)
  151     shifted_image = fourier_shift(reference_image, shift)
  152     result, error, diffphase = phase_cross_correlation(reference_image,
  153                                                        shifted_image,
  154                                                        space="fourier")
  155     assert_allclose(result, -np.array(shift), atol=0.05)
  156 
  157 
  158 def test_4d_input_subpixel():
  159     phantom = img_as_float(binary_blobs(length=32, n_dim=4))
  160     reference_image = fft.fftn(phantom)
  161     subpixel_shift = (-2.3, 1.7, 5.4, -3.2)
  162     shifted_image = fourier_shift(reference_image, subpixel_shift)
  163     result, error, diffphase = phase_cross_correlation(reference_image,
  164                                                        shifted_image,
  165                                                        upsample_factor=10,
  166                                                        space="fourier")
  167     assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
  168 
  169 
  170 def test_mismatch_upsampled_region_size():
  171     with pytest.raises(ValueError):
  172         _upsampled_dft(
  173             np.ones((4, 4)),
  174             upsampled_region_size=[3, 2, 1, 4])
  175 
  176 
  177 def test_mismatch_offsets_size():
  178     with pytest.raises(ValueError):
  179         _upsampled_dft(np.ones((4, 4)), 3,
  180                        axis_offsets=[3, 2, 1, 4])