From 195bdc8c00fc66d72ed841a3cf12caa904882584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 19 Aug 2012 13:02:03 +0200 Subject: [PATCH 1/3] complete default matrix initialization of geometric transforms --- skimage/transform/_geometric.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index 90a3ab57..c055247e 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -105,7 +105,10 @@ class ProjectiveTransform(GeometricTransform): coeffs = range(8) def __init__(self, matrix=None): - if matrix is not None and matrix.shape != (3, 3): + if matrix is None: + # default to an identity transform + matrix = np.eye(3) + if matrix.shape != (3, 3): raise ValueError("invalid shape of transformation matrix") self._matrix = matrix @@ -271,15 +274,16 @@ class AffineTransform(ProjectiveTransform): def __init__(self, matrix=None, scale=None, rotation=None, shear=None, translation=None): - self._matrix = matrix params = any(param is not None for param in (scale, rotation, shear, translation)) if params and matrix is not None: raise ValueError("You cannot specify the transformation matrix and " "the implicit parameters at the same time.") - elif matrix is not None and matrix.shape != (3, 3): - raise ValueError("Invalid shape of transformation matrix.") + elif matrix is not None: + if matrix.shape != (3, 3): + raise ValueError("Invalid shape of transformation matrix.") + self._matrix = matrix elif params: if scale is None: scale = (1, 1) @@ -298,7 +302,7 @@ class AffineTransform(ProjectiveTransform): ]) self._matrix[0:2, 2] = translation else: - # Default to an identity transform + # default to an identity transform self._matrix = np.eye(3) @property @@ -351,15 +355,16 @@ class SimilarityTransform(ProjectiveTransform): def __init__(self, matrix=None, scale=None, rotation=None, translation=None): - self._matrix = matrix params = any(param is not None for param in (scale, rotation, translation)) if params and matrix is not None: raise ValueError("You cannot specify the transformation matrix and " "the implicit parameters at the same time.") - elif matrix is not None and matrix.shape != (3, 3): - raise ValueError("Invalid shape of transformation matrix.") + elif matrix is not None: + if matrix.shape != (3, 3): + raise ValueError("Invalid shape of transformation matrix.") + self._matrix = matrix elif params: if scale is None: scale = 1 @@ -376,7 +381,7 @@ class SimilarityTransform(ProjectiveTransform): self._matrix *= scale self._matrix[0:2, 2] = translation else: - # Default to an identity transform + # default to an identity transform self._matrix = np.eye(3) def estimate(self, src, dst): @@ -480,7 +485,10 @@ class PolynomialTransform(GeometricTransform): """ def __init__(self, params=None): - if params is not None and params.shape[0] != 2: + if params is None: + # default to transformation which preserves original coordinates + params = np.array([[0, 1, 0], [0, 0, 1]]) + if params.shape[0] != 2: raise ValueError("invalid shape of transformation parameters") self._params = params From 6ea4827f9dd64c04b4d1da3c6f6ea85123abe481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 19 Aug 2012 13:04:58 +0200 Subject: [PATCH 2/3] add test case for polynomial transform initialization --- skimage/transform/tests/test_geometric.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/skimage/transform/tests/test_geometric.py b/skimage/transform/tests/test_geometric.py index d3e7f20c..b6db1dec 100644 --- a/skimage/transform/tests/test_geometric.py +++ b/skimage/transform/tests/test_geometric.py @@ -142,6 +142,13 @@ def test_polynomial_estimation(): tform2 = PolynomialTransform() tform2.estimate(SRC, DST, order=10) assert_array_almost_equal(tform2._params, tform._params) + + +def test_polynomial_init(): + tform = estimate_transform('polynomial', SRC, DST, order=10) + # init with transformation matrix + tform2 = PolynomialTransform(tform._params) + assert_array_almost_equal(tform2._params, tform._params) def test_union(): From d511bd84295a8d07f886ca8482cfea25dfca0519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 19 Aug 2012 13:06:27 +0200 Subject: [PATCH 3/3] fix comment --- skimage/transform/tests/test_geometric.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/transform/tests/test_geometric.py b/skimage/transform/tests/test_geometric.py index b6db1dec..abe8b2e7 100644 --- a/skimage/transform/tests/test_geometric.py +++ b/skimage/transform/tests/test_geometric.py @@ -142,11 +142,11 @@ def test_polynomial_estimation(): tform2 = PolynomialTransform() tform2.estimate(SRC, DST, order=10) assert_array_almost_equal(tform2._params, tform._params) - - + + def test_polynomial_init(): tform = estimate_transform('polynomial', SRC, DST, order=10) - # init with transformation matrix + # init with transformation parameters tform2 = PolynomialTransform(tform._params) assert_array_almost_equal(tform2._params, tform._params)