Main Content

affine2d

2-D affine geometric transformation

Description

Anaffine2dobject stores information about a 2-D affine geometric transformation and enables forward and inverse transformations.

Creation

You can create anaffine2dobject using the following methods:

  • imregtform— Estimate a geometric transformation that maps a moving image to a fixed image using similarity optimization.

  • imregcorr— Estimate a geometric transformation that maps a moving image to a fixed image using phase correlation.

  • fitgeotrans— Estimate a geometric transformation that maps pairs of control points between two images.

  • randomAffine2d— Create a randomized 2-D affine transformation.

  • Theaffine2dfunction described here.

Description

tform = affine2dcreates anaffine2dobject with default property settings that correspond to the identity transformation.

example

tform = affine2d(T)sets the propertyTas the specified valid affine transformation matrix.

Properties

expand all

Forward 2-D affine transformation, specified as a nonsingular 3-by-3 numeric matrix.

The matrixTuses the convention:

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

whereThas the form:

[a b 0; c d 0; e f 1];

The default ofTis the identity transformation.

Data Types:double|single

Dimensionality of the geometric transformation for both input and output points, specified as the value2.

Object Functions

invert 逆几何为我国ormation
isRigid Determine if transformation is rigid transformation
isSimilarity Determine if transformation is similarity transformation
isTranslation Determine if transformation is pure translation
outputLimits Find output spatial limits given input spatial limits
transformPointsForward Apply forward geometric transformation
transformPointsInverse Apply inverse geometric transformation

Examples

collapse all

Create anaffine2dobject that defines a 30 degree rotation in the counterclockwise direction around the origin.

theta = 30; tform = affine2d([...cosd(theta) sind(theta) 0;...-sind(theta) cosd(theta) 0;...0 0 1])
tform = affine2d with properties: T: [3x3 double] Dimensionality: 2

Apply the forward geometric transformation to a point (10,0).

[x,y] = transformPointsForward(tform,10,0)
x = 8.6603
y = 5

Validate the transformation by plotting the original point (in blue) and the transformed point (in red).

plot(10,0,'bo',x,y,'ro') axis([0 12 0 12]) axissquare

Figure contains an axes. The axes contains 2 objects of type line.

Read and display an image.

I = imread('kobi.png'); imshow(I)

Figure contains an axes. The axes contains an object of type image.

Create anaffine2dtransformation object that rotates images. TherandomAffine2d随机函数选择一个旋转角从一个有限公司ntinuous uniform distribution within the interval [35, 55] degrees.

tform1 = randomAffine2d('Rotation',[35 55]);

Rotate the image and display the result.

J = imwarp(I,tform1); imshow(J)

Figure contains an axes. The axes contains an object of type image.

The transformation object,tform1, rotates all images by the same amount. To rotate an image by a different randomly selected amount, create a newaffine2dtransformation object.

tform2 = randomAffine2d('Rotation',[-10 10]); J2 = imwarp(I,tform2); imshow(J2)

Figure contains an axes. The axes contains an object of type image.

This example shows how to create a geometric transformation that can be used to align two images.

Create a checkerboard image and rotate it to create a misaligned image.

I = checkerboard(40); J = imrotate(I,30); imshowpair(I,J,'montage')

Figure contains an axes. The axes contains an object of type image.

Define some matching control points on the fixed image (the checkerboard) and moving image (the rotated checkerboard). You can define points interactively using the Control Point Selection tool.

fixedPoints = [41 41; 281 161]; movingPoints = [56 175; 324 160];

Create a geometric transformation that can be used to align the two images, returned as anaffine2dgeometric transformation object.

tform = fitgeotrans(movingPoints,fixedPoints,'NonreflectiveSimilarity')
tform = affine2d with properties: T: [3x3 double] Dimensionality: 2

Use thetformestimate to resample the rotated image to register it with the fixed image. The regions of color (green and magenta) in the false color overlay image indicate error in the registration. This error comes from a lack of precise correspondence in the control points.

Jregistered = imwarp(J,tform,'OutputView',imref2d(size(I))); figure imshowpair(I,Jregistered)

Figure contains an axes. The axes contains an object of type image.

Recover angle and scale of the transformation by checking how a unit vector parallel to the x-axis is rotated and stretched.

u = [0 1]; v = [0 0]; [x, y] = transformPointsForward(tform, u, v); dx = x(2) - x(1); dy = y(2) - y(1); angle = (180/pi) * atan2(dy, dx)
angle = 29.7686
scale = 1 / sqrt(dx^2 + dy^2)
scale = 1.0003

Extended Capabilities

Introduced in R2013a