skimage2.transform.ProjectiveTransform#
- class skimage2.transform.ProjectiveTransform(matrix=None, *, dimensionality=None)[source]#
Bases:
_HMatrixTransformProjective transformation.
Apply a projective transformation (homography) on coordinates.
In what follows,
xrefers to coordinates from the first coordinate axis, andyrefers to coordinates from the second coordinate axis.For each homogeneous coordinate vector \(\mathbf{v} = [x, y, 1]^T\), its target position is calculated by multiplying with the given matrix, \(H\):
[[a0 a1 a2] [b0 b1 b2] [c0 c1 1 ]]
to give the output vector \(H \mathbf{v}\):
[[x * a0 + y * a1 + a2] [x * b0 + y * b1 + b2] [x * c0 + y * c1 + 1]]
For example, to rotate by theta degrees, the matrix \(\mathbf{H}\) should be:
[[cos(theta) -sin(theta) 0] [sin(theta) cos(theta) 0] [0 0 1]].
Whether this will appear as a counter-clockwise or clockwise rotation in terms of an image array, will depend on your input coordinates; if the first column in your coordinate array has coordinates in terms of the first image axis (first array axis), then the
xcoordinates above correspond to the first (row,i) axis in the image array. In that case the rotation applied to the coordinates is counter-clockwise. If the first column has coordinates from the second axis of the image array, instead of the first, then the rotation is clockwise.To translate
x(first coordinate axis) by 10 andyby 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.
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
skimage2namespace, 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 (theskimagenamespace), 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 inskimage2, you will get an opposite direction rotation from the same function in theskimage.transformmodule.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.
In what follows,
xrefers to coordinates from the first coordinate axis, andyrefers to coordinates from the second coordinate axis.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.
In what follows,
xrefers to coordinates from the first coordinate axis, andyrefers to coordinates from the second coordinate axis.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'#