From 76931fa61be74581910d91f936564a5bc0a8335f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Thu, 9 Aug 2012 09:21:49 +0200 Subject: [PATCH] handle composition of transformations with implicit parameters with __init__ --- skimage/transform/_geometric.py | 117 +++++++++++----------- skimage/transform/tests/test_geometric.py | 18 ++-- 2 files changed, 63 insertions(+), 72 deletions(-) diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index f5ad71f4..d394e0cf 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -214,44 +214,41 @@ class AffineTransform(ProjectiveTransform): ---------- matrix : (3, 3) array, optional Homogeneous transformation matrix. + scale : (sx, sy) as array, list or tuple, optional + scale factors + rotation : float, optional + rotation angle in counter-clockwise direction, optional + shear : float, optional + shear angle in counter-clockwise direction + translation : (tx, ty) as array, list or tuple, optional + translation parameters """ coeffs = range(6) - def compose_implicit(self, scale=None, rotation=None, shear=None, - translation=None): - """Set the transformation matrix with the implicit transformation - parameters. + def __init__(self, matrix=None, scale=None, rotation=None, shear=None, + translation=None): + params = (scale, rotation, shear, translation) + if matrix is not None: + self._matrix = matrix + elif any(param is not None for param in params): + if scale is None: + scale = (1, 1) + if rotation is None: + rotation = 0 + if shear is None: + shear = 0 + if translation is None: + translation = (0, 0) - Parameters - ---------- - scale : (sx, sy) as array, list or tuple - scale factors - rotation : float - rotation angle in counter-clockwise direction - shear : float - shear angle in counter-clockwise direction - translation : (tx, ty) as array, list or tuple - translation parameters - - """ - if scale is None: - scale = (1, 1) - if rotation is None: - rotation = 0 - if shear is None: - shear = 0 - if translation is None: - translation = (0, 0) - - sx, sy = scale - self._matrix = np.array([ - [sx * math.cos(rotation), - sy * math.sin(rotation + shear), 0], - [sx * math.sin(rotation), sy * math.cos(rotation + shear), 0], - [ 0, 0, 1] - ]) - self._matrix[0:2, 2] = translation + sx, sy = scale + self._matrix = np.array([ + [sx * math.cos(rotation), - sy * math.sin(rotation + shear), 0], + [sx * math.sin(rotation), sy * math.cos(rotation + shear), 0], + [ 0, 0, 1] + ]) + self._matrix[0:2, 2] = translation @property def scale(self): @@ -292,9 +289,36 @@ class SimilarityTransform(ProjectiveTransform): ---------- matrix : (3, 3) array, optional Homogeneous transformation matrix. + scale : float, optional + scale factor + rotation : float, optional + rotation angle in counter-clockwise direction + translation : (tx, ty) as array, list or tuple, optional + x, y translation parameters """ + def __init__(self, matrix=None, scale=None, rotation=None, + translation=None): + params = (scale, rotation, translation) + if matrix is not None: + self._matrix = matrix + elif any(param is not None for param in params): + if scale is None: + scale = 1 + if rotation is None: + rotation = 0 + if translation is None: + translation = (0, 0) + + self._matrix = np.array([ + [math.cos(rotation), - math.sin(rotation), 0], + [math.sin(rotation), math.cos(rotation), 0], + [ 0, 0, 1] + ]) + self._matrix *= scale + self._matrix[0:2, 2] = translation + def estimate(self, src, dst): """Set the transformation matrix with the explicit parameters. @@ -338,35 +362,6 @@ class SimilarityTransform(ProjectiveTransform): [b0, a0, b1], [ 0, 0, 1]]) - def compose_implicit(self, scale=None, rotation=None, translation=None): - """Set the transformation matrix with the implicit transformation - parameters. - - Parameters - ---------- - scale : float, optional - scale factor - rotation : float, optional - rotation angle in counter-clockwise direction - translation : (tx, ty) as array, list or tuple, optional - x, y translation parameters - - """ - if scale is None: - scale = 1 - if rotation is None: - rotation = 0 - if translation is None: - translation = (0, 0) - - self._matrix = np.array([ - [math.cos(rotation), - math.sin(rotation), 0], - [math.sin(rotation), math.cos(rotation), 0], - [ 0, 0, 1] - ]) - self._matrix *= scale - self._matrix[0:2, 2] = translation - @property def scale(self): if math.cos(self.rotation) == 0: diff --git a/skimage/transform/tests/test_geometric.py b/skimage/transform/tests/test_geometric.py index c7305956..b25b1aef 100644 --- a/skimage/transform/tests/test_geometric.py +++ b/skimage/transform/tests/test_geometric.py @@ -53,11 +53,11 @@ def test_similarity_estimation(): def test_similarity_implicit(): - tform = SimilarityTransform() scale = 0.1 rotation = 1 translation = (1, 1) - tform.compose_implicit(scale, rotation, translation) + tform = SimilarityTransform(scale=scale, rotation=rotation, + translation=translation) assert_array_almost_equal(tform.scale, scale) assert_array_almost_equal(tform.rotation, rotation) assert_array_almost_equal(tform.translation, translation) @@ -74,12 +74,12 @@ def test_affine_estimation(): def test_affine_implicit(): - tform = AffineTransform() scale = (0.1, 0.13) rotation = 1 shear = 0.1 translation = (1, 1) - tform.compose_implicit(scale, rotation, shear, translation) + tform = AffineTransform(scale=scale, rotation=rotation, shear=shear, + translation=translation) assert_array_almost_equal(tform.scale, scale) assert_array_almost_equal(tform.rotation, rotation) assert_array_almost_equal(tform.shear, shear) @@ -102,13 +102,9 @@ def test_polynomial(): def test_union(): - tform1 = SimilarityTransform() - tform1.compose_implicit(scale=0.1, rotation=0.3) - tform2 = SimilarityTransform() - tform2.compose_implicit(scale=0.1, rotation=0.9) - tform3 = SimilarityTransform() - tform3.compose_implicit(scale=0.1**2, rotation=0.3+0.9) - + tform1 = SimilarityTransform(scale=0.1, rotation=0.3) + tform2 = SimilarityTransform(scale=0.1, rotation=0.9) + tform3 = SimilarityTransform(scale=0.1 ** 2, rotation=0.3 + 0.9) tform = tform1 + tform2