handle composition of transformations with implicit parameters with __init__

This commit is contained in:
Johannes Schönberger
2012-08-09 09:21:49 +02:00
parent 1bb896e487
commit 76931fa61b
2 changed files with 63 additions and 72 deletions
+56 -61
View File
@@ -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:
+7 -11
View File
@@ -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