Several different geometric transformation types are supported: similarity,
affine, projective and polynomial. For a tutorial on the available types of
transformations, see Types of homographies.
Geometric transformations can either be created using the explicit
parameters (e.g. scale, shear, rotation and translation) or the
transformation matrix.
First we create a transformation using explicit parameters:
These transformation objects can then be used to apply forward and inverse
coordinate transformations between the source and destination coordinate
systems:
In addition to the basic functionality mentioned above you can also
generate a transform by estimating the parameters of a geometric
transformation using the least squares method.
This can amongst other things be used for image registration or
rectification, where you have a set of control points or
homologous/corresponding points in two images.
Let’s assume we want to recognize letters on a photograph which was not
taken from the front but at a certain angle. In the simplest case of a
plane paper surface the letters are projectively distorted. Simple matching
algorithms would not be able to match such symbols. One solution to this
problem would be to warp the image so that the distortion is removed and
then apply a matching algorithm:
The above estimation relies on accurate knowledge of the location of points
and an accurate selection of their correspondence. If point locations have
an uncertainty associated with them, then weighting can be provided so that
the resulting transform prioritises an accurate fit to those points with the
highest weighting.
An alternative approach called the
RANSAC algorithm
is useful when the correspondence points are not perfectly accurate.
See the Robust matching using RANSAC tutorial
for an in-depth description of how to use this approach in scikit-image.
Failed estimation
There are situations where transform classes can fail to estimate a valid
transformation, and we recommend that you always check for this possible
case.
If estimation succeeds, the result you get back will be a valid estimated
transform. You can check if you have a valid transform by truth testing.
E.g., the estimation from the previous section tform3 is valid and truthy:
However, if estimation failed, the from_estimate method returns a
special object of type FailedEstimation. Here is an example of a failed
estimation, where all the input points are the same:
/opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/numpy/_core/numeric.py:475: RuntimeWarning:
invalid value encountered in cast
FailedEstimation('ProjectiveTransform: Scaling generated NaN values')
This object type is falsey—meaning that:
bool(bad_tform)
False
You can access the raw message string of the failure with
str(bad_tform)
'ProjectiveTransform: Scaling generated NaN values'
We recommend that you put in a routine check to confirm the estimation
succeeded:
Of course, there may be times, where you did not check the truthiness of
the estimation, and you nevertheless try to use the returned estimate. In
this case, you’ll get a FailedEstimationAccessError—a custom
subclass of a AttributeError.
Total running time of the script: (0 minutes 0.630 seconds)