mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-19 11:27:45 +08:00
Use consistent params_ attribute across all estimators
This commit is contained in:
@@ -114,17 +114,17 @@ class ProjectiveTransform(GeometricTransform):
|
||||
matrix = np.eye(3)
|
||||
if matrix.shape != (3, 3):
|
||||
raise ValueError("invalid shape of transformation matrix")
|
||||
self.matrix_ = matrix
|
||||
self.params_ = matrix
|
||||
|
||||
@property
|
||||
def _matrix(self):
|
||||
warnings.warn('`_matrix` attribute is deprecated, '
|
||||
'use `matrix_` instead.')
|
||||
return self.matrix_
|
||||
'use `params_` instead.')
|
||||
return self.params_
|
||||
|
||||
@property
|
||||
def _inv_matrix(self):
|
||||
return np.linalg.inv(self.matrix_)
|
||||
return np.linalg.inv(self.params_)
|
||||
|
||||
def _apply_mat(self, coords, matrix):
|
||||
coords = np.array(coords, copy=False, ndmin=2)
|
||||
@@ -140,7 +140,7 @@ class ProjectiveTransform(GeometricTransform):
|
||||
return dst[:, :2]
|
||||
|
||||
def __call__(self, coords):
|
||||
return self._apply_mat(coords, self.matrix_)
|
||||
return self._apply_mat(coords, self.params_)
|
||||
|
||||
def inverse(self, coords):
|
||||
"""Apply inverse transformation.
|
||||
@@ -242,7 +242,7 @@ class ProjectiveTransform(GeometricTransform):
|
||||
H.flat[list(self._coeffs) + [8]] = - V[-1, :-1] / V[-1, -1]
|
||||
H[2, 2] = 1
|
||||
|
||||
self.matrix_ = H
|
||||
self.params_ = H
|
||||
|
||||
def __add__(self, other):
|
||||
"""Combine this transformation with another.
|
||||
@@ -255,7 +255,7 @@ class ProjectiveTransform(GeometricTransform):
|
||||
tform = self.__class__
|
||||
else:
|
||||
tform = ProjectiveTransform
|
||||
return tform(other.matrix_.dot(self.matrix_))
|
||||
return tform(other.params_.dot(self.params_))
|
||||
else:
|
||||
raise TypeError("Cannot combine transformations of differing "
|
||||
"types.")
|
||||
@@ -306,7 +306,7 @@ class AffineTransform(ProjectiveTransform):
|
||||
elif matrix is not None:
|
||||
if matrix.shape != (3, 3):
|
||||
raise ValueError("Invalid shape of transformation matrix.")
|
||||
self.matrix_ = matrix
|
||||
self.params_ = matrix
|
||||
elif params:
|
||||
if scale is None:
|
||||
scale = (1, 1)
|
||||
@@ -318,34 +318,34 @@ class AffineTransform(ProjectiveTransform):
|
||||
translation = (0, 0)
|
||||
|
||||
sx, sy = scale
|
||||
self.matrix_ = np.array([
|
||||
self.params_ = 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
|
||||
self.params_[0:2, 2] = translation
|
||||
else:
|
||||
# default to an identity transform
|
||||
self.matrix_ = np.eye(3)
|
||||
self.params_ = np.eye(3)
|
||||
|
||||
@property
|
||||
def scale(self):
|
||||
sx = math.sqrt(self.matrix_[0, 0] ** 2 + self.matrix_[1, 0] ** 2)
|
||||
sy = math.sqrt(self.matrix_[0, 1] ** 2 + self.matrix_[1, 1] ** 2)
|
||||
sx = math.sqrt(self.params_[0, 0] ** 2 + self.params_[1, 0] ** 2)
|
||||
sy = math.sqrt(self.params_[0, 1] ** 2 + self.params_[1, 1] ** 2)
|
||||
return sx, sy
|
||||
|
||||
@property
|
||||
def rotation(self):
|
||||
return math.atan2(self.matrix_[1, 0], self.matrix_[0, 0])
|
||||
return math.atan2(self.params_[1, 0], self.params_[0, 0])
|
||||
|
||||
@property
|
||||
def shear(self):
|
||||
beta = math.atan2(- self.matrix_[0, 1], self.matrix_[1, 1])
|
||||
beta = math.atan2(- self.params_[0, 1], self.params_[1, 1])
|
||||
return beta - self.rotation
|
||||
|
||||
@property
|
||||
def translation(self):
|
||||
return self.matrix_[0:2, 2]
|
||||
return self.params_[0:2, 2]
|
||||
|
||||
|
||||
class PiecewiseAffineTransform(GeometricTransform):
|
||||
@@ -520,7 +520,7 @@ class SimilarityTransform(ProjectiveTransform):
|
||||
elif matrix is not None:
|
||||
if matrix.shape != (3, 3):
|
||||
raise ValueError("Invalid shape of transformation matrix.")
|
||||
self.matrix_ = matrix
|
||||
self.params_ = matrix
|
||||
elif params:
|
||||
if scale is None:
|
||||
scale = 1
|
||||
@@ -529,16 +529,16 @@ class SimilarityTransform(ProjectiveTransform):
|
||||
if translation is None:
|
||||
translation = (0, 0)
|
||||
|
||||
self.matrix_ = np.array([
|
||||
self.params_ = np.array([
|
||||
[math.cos(rotation), - math.sin(rotation), 0],
|
||||
[math.sin(rotation), math.cos(rotation), 0],
|
||||
[ 0, 0, 1]
|
||||
])
|
||||
self.matrix_[0:2, 0:2] *= scale
|
||||
self.matrix_[0:2, 2] = translation
|
||||
self.params_[0:2, 0:2] *= scale
|
||||
self.params_[0:2, 2] = translation
|
||||
else:
|
||||
# default to an identity transform
|
||||
self.matrix_ = np.eye(3)
|
||||
self.params_ = np.eye(3)
|
||||
|
||||
def estimate(self, src, dst):
|
||||
"""Set the transformation matrix with the explicit parameters.
|
||||
@@ -604,7 +604,7 @@ class SimilarityTransform(ProjectiveTransform):
|
||||
# singular value
|
||||
a0, a1, b0, b1 = - V[-1, :-1] / V[-1, -1]
|
||||
|
||||
self.matrix_ = np.array([[a0, -b0, a1],
|
||||
self.params_ = np.array([[a0, -b0, a1],
|
||||
[b0, a0, b1],
|
||||
[ 0, 0, 1]])
|
||||
|
||||
@@ -612,18 +612,18 @@ class SimilarityTransform(ProjectiveTransform):
|
||||
def scale(self):
|
||||
if math.cos(self.rotation) == 0:
|
||||
# sin(self.rotation) == 1
|
||||
scale = self.matrix_[0, 1]
|
||||
scale = self.params_[0, 1]
|
||||
else:
|
||||
scale = self.matrix_[0, 0] / math.cos(self.rotation)
|
||||
scale = self.params_[0, 0] / math.cos(self.rotation)
|
||||
return scale
|
||||
|
||||
@property
|
||||
def rotation(self):
|
||||
return math.atan2(self.matrix_[1, 0], self.matrix_[1, 1])
|
||||
return math.atan2(self.params_[1, 0], self.params_[1, 1])
|
||||
|
||||
@property
|
||||
def translation(self):
|
||||
return self.matrix_[0:2, 2]
|
||||
return self.params_[0:2, 2]
|
||||
|
||||
|
||||
class PolynomialTransform(GeometricTransform):
|
||||
@@ -1071,14 +1071,14 @@ def warp(image, inverse_map=None, map_args={}, output_shape=None, order=1,
|
||||
|
||||
# inverse_map is a homography
|
||||
elif isinstance(inverse_map, HOMOGRAPHY_TRANSFORMS):
|
||||
matrix = inverse_map.matrix_
|
||||
matrix = inverse_map.params_
|
||||
|
||||
# inverse_map is the inverse of a homography
|
||||
elif (hasattr(inverse_map, '__name__')
|
||||
and inverse_map.__name__ == 'inverse'
|
||||
and get_bound_method_class(inverse_map) \
|
||||
in HOMOGRAPHY_TRANSFORMS):
|
||||
matrix = np.linalg.inv(six.get_method_self(inverse_map)._matrix)
|
||||
matrix = np.linalg.inv(six.get_method_self(inverse_map).params_)
|
||||
|
||||
if matrix is not None:
|
||||
matrix = matrix.astype(np.double)
|
||||
|
||||
@@ -56,19 +56,19 @@ def test_similarity_estimation():
|
||||
# exact solution
|
||||
tform = estimate_transform('similarity', SRC[:2, :], DST[:2, :])
|
||||
assert_array_almost_equal(tform(SRC[:2, :]), DST[:2, :])
|
||||
assert_equal(tform.matrix_[0, 0], tform.matrix_[1, 1])
|
||||
assert_equal(tform.matrix_[0, 1], - tform.matrix_[1, 0])
|
||||
assert_equal(tform.params_[0, 0], tform.params_[1, 1])
|
||||
assert_equal(tform.params_[0, 1], - tform.params_[1, 0])
|
||||
|
||||
# over-determined
|
||||
tform2 = estimate_transform('similarity', SRC, DST)
|
||||
assert_array_almost_equal(tform2.inverse(tform2(SRC)), SRC)
|
||||
assert_equal(tform2.matrix_[0, 0], tform2.matrix_[1, 1])
|
||||
assert_equal(tform2.matrix_[0, 1], - tform2.matrix_[1, 0])
|
||||
assert_equal(tform2.params_[0, 0], tform2.params_[1, 1])
|
||||
assert_equal(tform2.params_[0, 1], - tform2.params_[1, 0])
|
||||
|
||||
# via estimate method
|
||||
tform3 = SimilarityTransform()
|
||||
tform3.estimate(SRC, DST)
|
||||
assert_array_almost_equal(tform3.matrix_, tform2.matrix_)
|
||||
assert_array_almost_equal(tform3.params_, tform2.params_)
|
||||
|
||||
|
||||
def test_similarity_init():
|
||||
@@ -83,7 +83,7 @@ def test_similarity_init():
|
||||
assert_array_almost_equal(tform.translation, translation)
|
||||
|
||||
# init with transformation matrix
|
||||
tform2 = SimilarityTransform(tform.matrix_)
|
||||
tform2 = SimilarityTransform(tform.params_)
|
||||
assert_array_almost_equal(tform2.scale, scale)
|
||||
assert_array_almost_equal(tform2.rotation, rotation)
|
||||
assert_array_almost_equal(tform2.translation, translation)
|
||||
@@ -111,7 +111,7 @@ def test_affine_estimation():
|
||||
# via estimate method
|
||||
tform3 = AffineTransform()
|
||||
tform3.estimate(SRC, DST)
|
||||
assert_array_almost_equal(tform3.matrix_, tform2.matrix_)
|
||||
assert_array_almost_equal(tform3.params_, tform2.params_)
|
||||
|
||||
|
||||
def test_affine_init():
|
||||
@@ -128,7 +128,7 @@ def test_affine_init():
|
||||
assert_array_almost_equal(tform.translation, translation)
|
||||
|
||||
# init with transformation matrix
|
||||
tform2 = AffineTransform(tform.matrix_)
|
||||
tform2 = AffineTransform(tform.params_)
|
||||
assert_array_almost_equal(tform2.scale, scale)
|
||||
assert_array_almost_equal(tform2.rotation, rotation)
|
||||
assert_array_almost_equal(tform2.shear, shear)
|
||||
@@ -155,14 +155,14 @@ def test_projective_estimation():
|
||||
# via estimate method
|
||||
tform3 = ProjectiveTransform()
|
||||
tform3.estimate(SRC, DST)
|
||||
assert_array_almost_equal(tform3.matrix_, tform2.matrix_)
|
||||
assert_array_almost_equal(tform3.params_, tform2.params_)
|
||||
|
||||
|
||||
def test_projective_init():
|
||||
tform = estimate_transform('projective', SRC, DST)
|
||||
# init with transformation matrix
|
||||
tform2 = ProjectiveTransform(tform.matrix_)
|
||||
assert_array_almost_equal(tform2.matrix_, tform.matrix_)
|
||||
tform2 = ProjectiveTransform(tform.params_)
|
||||
assert_array_almost_equal(tform2.params_, tform.params_)
|
||||
|
||||
|
||||
def test_polynomial_estimation():
|
||||
@@ -231,7 +231,7 @@ def test_invalid_input():
|
||||
def test_deprecated_params_attributes():
|
||||
for t in ('projective', 'affine', 'similarity'):
|
||||
tform = estimate_transform(t, SRC, DST)
|
||||
assert_equal(tform._matrix, tform.matrix_)
|
||||
assert_equal(tform._matrix, tform.params_)
|
||||
|
||||
tform = estimate_transform('polynomial', SRC, DST, order=3)
|
||||
assert_equal(tform._params, tform.params_)
|
||||
|
||||
@@ -243,7 +243,7 @@ def test_invalid():
|
||||
|
||||
def test_inverse():
|
||||
tform = SimilarityTransform(scale=0.5, rotation=0.1)
|
||||
inverse_tform = SimilarityTransform(matrix=np.linalg.inv(tform._matrix))
|
||||
inverse_tform = SimilarityTransform(matrix=np.linalg.inv(tform.params_))
|
||||
image = np.arange(10 * 10).reshape(10, 10).astype(np.double)
|
||||
assert_array_equal(warp(image, inverse_tform), warp(image, tform.inverse))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user