skimage2.transform.SimilarityTransform#

class skimage2.transform.SimilarityTransform(matrix=None, *, scale=None, rotation=None, translation=None, dimensionality=None)[source]#

Bases: EuclideanTransform

Similarity transformation.

In what follows, x refers to coordinates from the first coordinate axis, and y refers to coordinates from the second coordinate axis.

The transform has the following form in 2D:

X = a0 * x - b0 * y + a1 =
  = s * x * cos(rotation) - s * y * sin(rotation) + a1

Y = b0 * x + a0 * y + b1 =
  = s * x * sin(rotation) + s * y * cos(rotation) + b1

where s is a scale factor and the homogeneous transformation matrix is:

[[a0 -b0  a1]
 [b0  a0  b1]
 [0   0   1 ]]

The similarity transformation extends the Euclidean transformation with a single scaling factor in addition to the rotation and translation parameters.

The implicit parameters are applied in the following order:

  1. Scale

  2. Rotation

  3. Translation

Parameters:
matrixarray_like of shape (K, K), with K=D+1 (D being the dimensionality), optional

Homogeneous transformation matrix.

scalefloat, optional

Scale factor. Implemented only for 2D and 3D.

rotationfloat, optional

Rotation angle, in radians. See Notes for discussion of clockwise vs anti-clockwise.

Implemented only for 2D and 3D. For 3D, this is given in ZYX Euler angles.

translationarray_like of shape (D,), optional

x, y[, z] translation parameters. Implemented only for 2D and 3D.

dimensionalityint, optional

The dimensionality of the transform, corresponding to D above. Ignored if matrix is not None, and set to matrix.shape[0] - 1. Otherwise, must be one of 2 or 3.

Attributes:
paramsndarray of shape (K, K), with K=D+1 (D being the dimensionality)

Homogeneous transformation matrix.

Notes

Our axis convention will change from Scikit-image 1 to 2, and this changes the direction of (positive) rotation. In functions applying transformations, in the skimage2 namespace, we use the NumPy or “ij” convention, where the first and second axis of the image correspond to the first and second axis of the array (row, column). Correspondingly, a positive rotation is counter-clockwise. In scikit-image 1 (the skimage namespace), we used the “xy” convention, where the first axis is columns and the second rows. Correspondingly, positive rotation is clockwise.

Note, therefore, that if you apply a transform using warp, or other functions that apply transforms in skimage2, you will get an opposite direction rotation from the same function in the skimage.transform module.

Examples

>>> import numpy as np
>>> import _skimage2 as ski2

Define a transform with an homogeneous transformation matrix:

>>> tform = ski2.transform.SimilarityTransform(np.diag([2., 3., 1.]))
>>> tform.params
array([[2., 0., 0.],
       [0., 3., 0.],
       [0., 0., 1.]])

Define a transform with parameters:

>>> tform = ski2.transform.SimilarityTransform(
...             rotation=0.2, translation=[1, 2])
>>> np.round(tform.params, 2)
array([[ 0.98, -0.2 ,  1.  ],
       [ 0.2 ,  0.98,  2.  ],
       [ 0.  ,  0.  ,  1.  ]])

You can estimate a transformation to map between source and destination points:

>>> src = np.array([[150, 150],
...                 [250, 100],
...                 [150, 200]])
>>> dst = np.array([[200, 200],
...                 [300, 150],
...                 [150, 400]])
>>> tform = ski2.transform.SimilarityTransform.from_estimate(src, dst)
>>> np.allclose(tform.params, [[ 1.79, 0.21, -142.86],
...                            [-0.21, 1.79,   21.43],
...                            [ 0.  , 0.  ,    1.  ]], atol=0.01)
True

Apply the transformation to some image data.

>>> img = ski2.data.astronaut()
>>> warped = ski2.transform.warp(img, inverse_map=tform.inverse)

The estimation can fail - for example, if all the input or output points are the same. If this happens, you will get a transform that is not “truthy” - meaning that bool(tform) is False:

>>> # A successfully estimated model is truthy (applying ``bool()``
>>> # gives ``True``):
>>> if tform:
...     print("Estimation succeeded.")
Estimation succeeded.
>>> # Not so for a degenerate transform with identical points.
>>> bad_src = np.ones((3, 2))
>>> bad_tform = ski2.transform.SimilarityTransform.from_estimate(
...      bad_src, dst)
>>> if not bad_tform:
...     print("Estimation failed.")
Estimation failed.

Trying to use this failed estimation transform result will give a suitable error:

>>> bad_tform.params
Traceback (most recent call last):
  ...
FailedEstimationAccessError: No attribute "params" for failed estimation ...
__init__(matrix=None, *, scale=None, rotation=None, translation=None, dimensionality=None)[source]#
property dimensionality#

The dimensionality of the transformation.

estimate(src, dst)[source]#

Estimate the transformation from a set of corresponding points.

Deprecated since version 0.26: estimate is deprecated since version 0.26 and will be removed in version 2.2. Please use SimilarityTransform.from_estimate class constructor instead.

You can determine the over-, well- and under-determined parameters with the total least-squares method.

Number of source and destination coordinates must match.

Parameters:
srcarray_like of shape (N, 2)

Source coordinates.

dstarray_like of shape (N, 2)

Destination coordinates.

Returns:
successbool

True, if model estimation succeeds.

classmethod from_estimate(dst) → Self | FailedEstimation[source]#

Estimate the transformation from a set of corresponding points.

You can determine the over-, well- and under-determined parameters with the total least-squares method.

Number of source and destination coordinates must match.

Parameters:
srcarray_like of shape (N, 2)

Source coordinates.

dstarray_like of shape (N, 2)

Destination coordinates.

Returns:
tfSelf or FailedEstimation

An instance of the transformation if the estimation succeeded. Otherwise, we return a special FailedEstimation object to signal a failed estimation. Testing the truth value of the failed estimation object will return False. E.g.

tf = SimilarityTransform.from_estimate(...)
if not tf:
    raise RuntimeError(f"Failed estimation: {tf}")
classmethod identity(dimensionality=None)[source]#

Identity transform

Parameters:
dimensionality{None, int}, optional

Dimensionality of identity transform.

Returns:
tformtransform

Transform such that np.all(tform(pts) == pts).

property inverse#

Return a transform object representing the inverse.

residuals(src, dst)[source]#

Determine residuals of transformed destination coordinates.

For each transformed source coordinate the Euclidean distance to the respective destination coordinate is determined.

Parameters:
srcndarray of shape (N, 2)

Source coordinates.

dstndarray of shape (N, 2)

Destination coordinates.

Returns:
residualsndarray of shape (N,)

Residual for coordinate.

property rotation#
property scale#
scaling = 'rms'#
property translation#