skimage2.transform.ProjectiveTransform#
- class skimage2.transform.ProjectiveTransform(matrix=None, *, dimensionality=None)[source]#
Bases:
_HMatrixTransformProjective transformation.
Apply a projective transformation (homography) on coordinates.
For each homogeneous coordinate \(\mathbf{x} = [x, y, 1]^T\), its target position is calculated by multiplying with the given matrix, \(H\), to give \(H \mathbf{x}\):
[[a0 a1 a2] [b0 b1 b2] [c0 c1 1 ]].
E.g., to rotate by theta degrees clockwise, the matrix should be:
[[cos(theta) -sin(theta) 0] [sin(theta) cos(theta) 0] [0 0 1]]
or, to translate x by 10 and y by 20:
[[1 0 10] [0 1 20] [0 0 1 ]].
- Parameters:
- matrix(D+1, D+1) array_like, optional
Homogeneous transformation matrix.
- dimensionalityint, optional
Fallback number of dimensions when
matrixnot specified.
- Attributes:
- params(D+1, D+1) array
Homogeneous transformation matrix.
Examples
>>> import numpy as np >>> import _skimage2 as ski2
Define a transform with an homogeneous transformation matrix:
>>> tform = ski2.transform.ProjectiveTransform(np.diag([2., 3., 1.])) >>> tform.params array([[2., 0., 0.], [0., 3., 0.], [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.ProjectiveTransform.from_estimate(src, dst) >>> np.allclose(tform.params, [[ -16.56, 5.82, 895.81], ... [ -10.31, -8.29, 2075.43], ... [ -0.05, 0.02, 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)isFalse:>>> # 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.ProjectiveTransform.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 ...
- property dimensionality#
The dimensionality of the transformation.
- estimate(src, dst, weights=None)[source]#
Estimate the transformation from a set of corresponding points.
Deprecated since version 0.26:
estimateis deprecated since version 0.26 and will be removed in version 2.2. Please useProjectiveTransform.from_estimateclass 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.
The transformation is defined as:
X = (a0*x + a1*y + a2) / (c0*x + c1*y + 1) Y = (b0*x + b1*y + b2) / (c0*x + c1*y + 1)
These equations can be transformed to the following form:
0 = a0*x + a1*y + a2 - c0*x*X - c1*y*X - X 0 = b0*x + b1*y + b2 - c0*x*Y - c1*y*Y - Y
which exist for each set of corresponding points, so we have a set of N * 2 equations. The coefficients appear linearly so we can write A x = 0, where:
A = [[x y 1 0 0 0 -x*X -y*X -X] [0 0 0 x y 1 -x*Y -y*Y -Y] ... ... ] x.T = [a0 a1 a2 b0 b1 b2 c0 c1 c3]
In case of total least-squares the solution of this homogeneous system of equations is the right singular vector of A which corresponds to the smallest singular value normed by the coefficient c3.
Weights can be applied to each pair of corresponding points to indicate, particularly in an overdetermined system, if point pairs have higher or lower confidence or uncertainties associated with them. From the matrix treatment of least squares problems, these weight values are normalized, square-rooted, then built into a diagonal matrix, by which A is multiplied.
In case of the affine transformation the coefficients c0 and c1 are 0. Thus the system of equations is:
A = [[x y 1 0 0 0 -X] [0 0 0 x y 1 -Y] ... ... ] x.T = [a0 a1 a2 b0 b1 b2 c3]
- Parameters:
- srcarray_like of shape (N, 2)
Source coordinates.
- dstarray_like of shape (N, 2)
Destination coordinates.
- weightsarray_like of shape (N,), optional
Relative weight values for each pair of points.
- Returns:
- successbool
True, if model estimation succeeds.
- classmethod from_estimate(src, dst, weights=None)[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.
The transformation is defined as:
X = (a0*x + a1*y + a2) / (c0*x + c1*y + 1) Y = (b0*x + b1*y + b2) / (c0*x + c1*y + 1)
These equations can be transformed to the following form:
0 = a0*x + a1*y + a2 - c0*x*X - c1*y*X - X 0 = b0*x + b1*y + b2 - c0*x*Y - c1*y*Y - Y
which exist for each set of corresponding points, so we have a set of N * 2 equations. The coefficients appear linearly so we can write A x = 0, where:
A = [[x y 1 0 0 0 -x*X -y*X -X] [0 0 0 x y 1 -x*Y -y*Y -Y] ... ... ] x.T = [a0 a1 a2 b0 b1 b2 c0 c1 c3]
In case of total least-squares the solution of this homogeneous system of equations is the right singular vector of A which corresponds to the smallest singular value normed by the coefficient c3.
Weights can be applied to each pair of corresponding points to indicate, particularly in an overdetermined system, if point pairs have higher or lower confidence or uncertainties associated with them. From the matrix treatment of least squares problems, these weight values are normalized, square-rooted, then built into a diagonal matrix, by which A is multiplied.
In case of the affine transformation the coefficients c0 and c1 are 0. Thus the system of equations is:
A = [[x y 1 0 0 0 -X] [0 0 0 x y 1 -Y] ... ... ] x.T = [a0 a1 a2 b0 b1 b2 c3]
- Parameters:
- srcarray_like of shape (N, 2)
Source coordinates.
- dstarray_like of shape (N, 2)
Destination coordinates.
- weightsarray_like of shape (N,), optional
Relative weight values for each pair of points.
- Returns:
- tfSelf or
FailedEstimation An instance of the transformation if the estimation succeeded. Otherwise, we return a special
FailedEstimationobject to signal a failed estimation. Testing the truth value of the failed estimation object will returnFalse. E.g.tf = ProjectiveTransform.from_estimate(...) if not tf: raise RuntimeError(f"Failed estimation: {tf}")
- tfSelf or
- 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.
- scaling = 'rms'#