import numpy
import pytest
from ..io import data_for_registration
from ..io.input_stack import input_context
from ..io.output_stack import output_context
from ..transformation import apply_transformations
from ..transformation.homography import Homography
_HOMOGRAPHIES = {
f"homography{'_'.join(k)}": v for k, v in Homography.get_subclass_items()
}
[docs]
@pytest.mark.parametrize("homography", _HOMOGRAPHIES)
def test_apply_translation(homography):
image = numpy.random.uniform(0.0, 10, (10, 10))
transform = numpy.array([[1, 0, 6], [0, 1, -2], [0, 0, 1]], dtype=numpy.float64)
forward = _HOMOGRAPHIES[homography](passive_matrix=transform)
expected = numpy.zeros((10, 10))
expected[0:4, 2:10] = image[6:10, 0:8]
actual = forward.apply_data(image, cval=0)
numpy.testing.assert_allclose(actual, expected)
[docs]
@pytest.mark.parametrize("homography", _HOMOGRAPHIES)
def test_apply_coordinate(homography):
nx, ny = numpy.meshgrid(numpy.arange(4, dtype=numpy.float64), numpy.arange(4))
nx, ny = nx.flatten(), ny.flatten()
matrix = numpy.identity(3)
matrix[0:2, 2] = [-1, -3]
forward = _HOMOGRAPHIES[homography](passive_matrix=matrix)
res1 = forward.apply_coordinates(numpy.asarray([nx, ny]))
numpy.testing.assert_allclose(res1, [nx + 1, ny + 3])
matrix = numpy.array([[0, -1, 0], [1, 0, 0], [0, 0, 1]], dtype=numpy.float64)
forward = _HOMOGRAPHIES[homography](passive_matrix=matrix)
res1 = forward.apply_coordinates(numpy.asarray([nx, ny]))
numpy.testing.assert_allclose(res1, [ny, -nx], atol=numpy.finfo(numpy.float64).eps)