cp2tform

Infer spatial transformation from control point pairs

cp2tform is not recommended. Use fitgeotrans instead.

Syntax

TFORM = cp2tform(movingPoints,fixedPoints, transformtype)
TFORM = cp2tform(CPSTRUCT, transformtype)
[TFORM, movingPoints, fixedPoints] = cp2tform(CPSTRUCT, ...)
TFORM = cp2tform(movingPoints,fixedPoints, 'polynomial', order)
TFORM = cp2tform(CPSTRUCT,'polynomial',order)
TFORM = cp2tform(movingPoints,fixedPoints,'piecewise linear')
TFORM = cp2tform(CPSTRUCT,'piecewise linear')
TFORM = cp2tform(movingPoints,fixedPoints,'lwm', N)
TFORM = cp2tform(CPSTRUCT, 'lwm', N)
[TFORM, movingPoints, fixedPoints, movingPoints_bad, fixedPoints_bad] = cp2tform(movingPoints, fixedPoints, 'piecewise linear')
[TFORM, movingPoints, fixedPoints, movingPoints_bad, fixedPoints_bad] = cp2tform(CPSTRUCT, 'piecewise linear')

Description

TFORM = cp2tform(movingPoints,fixedPoints, transformtype) infers a spatial transformation from control point pairs and returns this transformation as a TFORM structure.

TFORM = cp2tform(CPSTRUCT, transformtype) works on a CPSTRUCT structure that contains the control point matrices for the input and base images. The Control Point Selection Tool, cpselect, creates the CPSTRUCT.

[TFORM, movingPoints, fixedPoints] = cp2tform(CPSTRUCT, ...) returns the control points that were used in movingPoints and fixedPoints. Unmatched and predicted points are not used. See cpstruct2pairs.

TFORM = cp2tform(movingPoints,fixedPoints, 'polynomial', order) lets you specify the order of the polynomials to use.

TFORM = cp2tform(CPSTRUCT,'polynomial',order) works on a CPSTRUCT structure.

TFORM = cp2tform(movingPoints,fixedPoints,'piecewise linear') creates a Delaunay triangulation of the base control points, and maps corresponding input control points to the base control points. The mapping is linear (affine) for each triangle and continuous across the control points but not continuously differentiable as each triangle has its own mapping.

TFORM = cp2tform(CPSTRUCT,'piecewise linear') works on a CPSTRUCT structure.

TFORM = cp2tform(movingPoints,fixedPoints,'lwm', N) creates a mapping by inferring a polynomial at each control point using neighboring control points. The mapping at any location depends on a weighted average of these polynomials. You can optionally specify the number of points, N, used to infer each polynomial. The N closest points are used to infer a polynomial of order 2 for each control point pair. If you omit N, it defaults to 12. N can be as small as 6, but making N small risks generating ill-conditioned polynomials.

TFORM = cp2tform(CPSTRUCT, 'lwm', N) works on a CPSTRUCT structure.

[TFORM, movingPoints, fixedPoints, movingPoints_bad, fixedPoints_bad] = cp2tform(movingPoints, fixedPoints, 'piecewise linear') returns the control points that were used in movingPoints and fixedPoints and the control points that were eliminated because they were middle vertices of degenerate fold-over triangles in movingPoints_bad and fixedPoints_bad.

[TFORM, movingPoints, fixedPoints, movingPoints_bad, fixedPoints_bad] = cp2tform(CPSTRUCT, 'piecewise linear') works on a CPSTRUCT structure.

Input Arguments

movingPoints

m-by-2, double matrix containing the x- and y-coordinates of control points in the image you want to transform.

fixedPoints

m-by-2, double matrix containing the x- and y-coordinates of control points in the base image.

transformtype

Specifies the type of spatial transformation to infer. The cp2tform function requires a minimum number of control point pairs to infer a structure of each transform type. The following table lists all the transformation types supported by cp2tform in order of complexity. The 'lwm' and 'polynomial' transform types can each take an optional, additional parameter.

 Transformation Types

CPSTRUCT

Structure containing control point matrices for the input and base images. Use the Control Point Selection Tool (cpselect) to create the CPSTRUCT.

'polynomial',order

Specifies the order of polynomials to use. order can be 2, 3, or 4.

Default: 3

'piecewise linear'

Linear for each piece and continuous, not continuously differentiable.

'lwm'

Local weighted mean.

N

Number of points.

Output Arguments

TFORM

Structure containing the spatial transformation.

movingPoints

Input control points that were used to infer the spatial transformation. Unmatched and predicted points are not used.

fixedPoints

Base control points that were used to infer the spatial transformation. Unmatched and predicted points are not used.

movingPoints_bad

Input control points that were eliminated because they were determined to be outliers.

fixedPoints_bad

Base control points that were eliminated because they were determined to be outliers.

Examples

Transform an image, use the cp2tform function to return the transformation, and compare the angle and scale of the TFORM to the angle and scale of the original transformation:

I = checkerboard;
J = imrotate(I,30);
fixedPoints = [11 11; 41 71];
movingPoints = [14 44; 70 81];
cpselect(J,I,movingPoints,fixedPoints);
 
t = cp2tform(movingPoints,fixedPoints,'nonreflective similarity');
 
% Recover angle and scale by checking how a unit vector 
% parallel to the x-axis is rotated and stretched. 
u = [0 1]; 
v = [0 0]; 
[x, y] = tformfwd(t, u, v); 
dx = x(2) - x(1); 
dy = y(2) - y(1); 
angle = (180/pi) * atan2(dy, dx) 
scale = 1 / sqrt(dx^2 + dy^2)

More About

expand all

Tips

  • When transformtype is 'nonreflective similarity', 'similarity', 'affine', 'projective', or 'polynomial', and movingPoints and fixedPoints (or CPSTRUCT) have the minimum number of control points needed for a particular transformation, cp2tform finds the coefficients exactly.

  • If movingPoints and fixedPoints have more than the minimum number of control points, a least-squares solution is found. See mldivide.

  • When either movingPoints or fixedPoints has a large offset with respect to their origin (relative to range of values that it spans), cp2tform shifts the points to center their bounding box on the origin before fitting a TFORM structure. This enhances numerical stability and is handled transparently by wrapping the origin-centered TFORM within a custom TFORM that automatically applies and undoes the coordinate shift as needed. As a result, fields(T) can give different results for different coordinate inputs, even for the same transformation type.

Algorithms

cp2tform uses the following general procedure:

  1. Use valid pairs of control points to infer a spatial transformation or an inverse mapping from output space (x,y) to input space (x,y) according to transformtype.

  2. Return the TFORM structure containing spatial transformation.

The procedure varies depending on the transformtype.

Nonreflective Similarity

Nonreflective similarity transformations can include a rotation, a scaling, and a translation. Shapes and angles are preserved. Parallel lines remain parallel. Straight lines remain straight.

Let

sc = scale*cos(angle)
ss = scale*sin(angle)

[u v] = [x y 1] * [ sc -ss
                    ss  sc
                    tx  ty]

Solve for sc, ss, tx, and ty.

Similarity

Similarity transformations can include rotation, scaling, translation, and reflection. Shapes and angles are preserved. Parallel lines remain parallel. Straight lines remain straight.

Let

sc = s*cos(theta)
ss = s*sin(theta)

                   [ sc -a*-ss
 [u v] = [x y 1] *   ss  a*sc
                     tx  ty]

Solve for sc, ss, tx, ty, and a. If a = -1, reflection is included in the transformation. If a = 1, reflection is not included in the transformation.

Affine

In an affine transformation, the x and y dimensions can be scaled or sheared independently and there can be a translation. Parallel lines remain parallel. Straight lines remain straight. Nonreflective similarity transformations are a subset of affine transformations.

For an affine transformation,

[u v] = [x y 1] * Tinv

Tinv is a 3-by-2 matrix. Solve for the six elements of Tinv:

t_affine = cp2tform(movingPoints,fixedPoints,'affine');

The coefficients of the inverse mapping are stored in t_affine.tdata.Tinv.

At least three control-point pairs are needed to solve for the six unknown coefficients.

Projective

In a projective transformation, quadrilaterals map to quadrilaterals. Straight lines remain straight. Affine transformations are a subset of projective transformations.

For a projective transformation,

[up vp wp] = [x y w] * Tinv

where

u = up/wp 
v = vp/wp

Tinv is a 3-by-3 matrix.

Assuming

Tinv = [ A D G;
         B E H;
         C F I ];
u = (Ax + By + C)/(Gx + Hy + I)
v = (Dx + Ey + F)/(Gx + Hy + I)

Solve for the nine elements of Tinv:

t_proj = cp2tform(movingPoints,fixedPoints,'projective');

The coefficients of the inverse mapping are stored in t_proj.tdata.Tinv.

At least four control-point pairs are needed to solve for the nine unknown coefficients.

    Note:   An affine or projective transformation can also be expressed like this, for a 3-by-2 Tinv:

    [u v]'  =  Tinv' * [x y 1]' 
    

    Or, like this, for a 3-by-3 Tinv:

    [u v 1]'  =  Tinv' * [x y 1]'

Polynomial

In a polynomial transformation, polynomial functions of x and y determine the mapping.

Second-Order Polynomials

For a second-order polynomial transformation,

[u v] = [1  x  y  x*y  x^2  y^2] * Tinv

Both u and v are second-order polynomials of x and y. Each second-order polynomial has six terms. To specify all coefficients, Tinv has size 6-by-2.

t_poly_ord2 = cp2tform(movingPoints, fixedPoints,'polynomial');

The coefficients of the inverse mapping are stored in t_poly_ord2.tdata.

At least six control-point pairs are needed to solve for the 12 unknown coefficients.

Third-Order Polynomials

For a third-order polynomial transformation:

[u v] = [1  x  y  x*y  x^2  y^2  y*x^2  x*y^2  x^3  y^3] * Tinv

Both u and v are third-order polynomials of x and y. Each third-order polynomial has 10 terms. To specify all coefficients, Tinv has size 10-by-2.

t_poly_ord3 = cp2tform(movingPoints, fixedPoints,'polynomial',3);

The coefficients of the inverse mapping are stored in t_poly_ord3.tdata.

At least ten control-point pairs are needed to solve for the 20 unknown coefficients.

Fourth-Order Polynomials

For a fourth-order polynomial transformation:

[u v] = [1 x y x*y x^2 y^2 y*x^2 x*y^2 x^3 y^3 x^3*y x^2*y^2 x*y^3 x^4 y^4] * Tinv

Both u and v are fourth-order polynomials of x and y. Each fourth-order polynomial has 15 terms. To specify all coefficients, Tinv has size 15-by-2.

t_poly_ord4 = cp2tform(movingPoints, fixedPoints,'polynomial',4);

The coefficients of the inverse mapping are stored in t_poly_ord4.tdata.

At least 15 control-point pairs are needed to solve for the 30 unknown coefficients.

Piecewise Linear

In a piecewise linear transformation, linear (affine) transformations are applied separately to each triangular region of the image[1].

  1. Find a Delaunay triangulation of the base control points.

  2. Using the three vertices of each triangle, infer an affine mapping from base to input coordinates.

    Note   At least four control-point pairs are needed. Four pairs result in two triangles with distinct mappings.

Local Weighted Mean

For each control point in fixedPoints:

  1. Find the N closest control points.

  2. Use these N points and their corresponding points in movingPoints to infer a second-order polynomial.

  3. Calculate the radius of influence of this polynomial as the distance from the center control point to the farthest point used to infer the polynomial (using fixedPoints)[2].

    Note   At least six control-point pairs are needed to solve for the second-order polynomial. Ill-conditioned polynomials might result if too few pairs are used.

References

[1] Goshtasby, Ardeshir, "Piecewise linear mapping functions for image registration," Pattern Recognition, Vol. 19, 1986, pp. 459-466.

[2] Goshtasby, Ardeshir, "Image registration by local approximation methods," Image and Vision Computing, Vol. 6, 1988, pp. 255-261.

Was this topic helpful?