Apply 2D spatial transformation to image
imtransform
is not recommended. Use imwarp
instead.
B = imtransform(A,tform)
B = imtransform(A,tform,interp
)
[B,xdata,ydata] = imtransform(...)
[B,xdata,ydata] = imtransform(...,Name,Value)
B = imtransform(A,tform)
transforms
the image A
according to the 2D spatial transformation
defined by tform
. If ndims(A) > 2
,
such as for an RGB image, then imtransform
applies
the same 2D transformation to all 2D planes along the higher dimensions.
B = imtransform(A,tform,
specifies
the form of interpolation to use.interp
)
[B,xdata,ydata] = imtransform(...)
returns
the location of the output image B
in the output
XY space. By default, imtransform
calculates xdata
and ydata
automatically
so that B
contains the entire transformed image A
.
However, you can override this automatic calculation by specifying
values for the 'XData'
and 'YData'
arguments.
[B,xdata,ydata] = imtransform(...,Name,Value)
transforms
the image with additional options for controlling various aspects
of the spatial transformation specified by one or more Name,Value
pair
arguments.

An image of any nonsparse numeric class (real or complex) or
of class 

A spatial transformation structure returned by 

Form of interpolation to use, specified as: Default: 
Optional commaseparated pairs of Name,Value
arguments,
where Name
is the argument name and Value
is
the corresponding value. Name
must appear within
single quotes (' '
) and is not case sensitive.
You can specify several name and value pair arguments in any order
as Name1
, Value1
, ..., NameN
, ValueN
.

A twoelement, real vector that, when combined with Default:  

A twoelement, real vector that, when combined with Default:  

A twoelement, real vector that, when combined with Default: If you do not specify  

A twoelement real vector that, when combined with Default: If you do not specify  

A one or twoelement real vector. The first element of Default: If you do not specify
 

A twoelement vector of nonnegative integers that specifies
the number of rows and columns of the output image Default: If you do not specify  

An array containing one or several fill values. The For example, if
If 

Output image of any nonsparse numeric class (real or complex)
or of class  

Twoelement vector that specifies the xcoordinates
of the first and last columns of
 

Twoelement vector that specifies the ycoordinates
of the first and last rows of 
Simple Transformation. Apply a horizontal shear to an intensity image:
I = imread('cameraman.tif'); tform = maketform('affine',[1 0 0; .5 1 0; 0 0 1]); J = imtransform(I,tform); imshow(I), figure, imshow(J)
Horizontal Shear
Projective Transformation. Map a square to a quadrilateral with a projective transformation:
% Set up an input coordinate system so that the input image % fills the unit square with vertices (0 0),(1 0),(1 1),(0 1). I = imread('cameraman.tif'); udata = [0 1]; vdata = [0 1]; % Transform to a quadrilateral with vertices (4 2),(8 3), % (3 5),(6 3). tform = maketform('projective',[ 0 0; 1 0; 1 1; 0 1],... [4 2; 8 3; 3 5; 6 3]); % Fill with gray and use bicubic interpolation. % Make the output size the same as the input size. [B,xdata,ydata] = imtransform(I, tform, 'bicubic', ... 'udata', udata,... 'vdata', vdata,... 'size', size(I),... 'fill', 128); subplot(1,2,1), imshow(I,'XData',udata,'YData',vdata), ... axis on subplot(1,2,2), imshow(B,'XData',xdata,'YData',ydata), ... axis on
Projective Transformation
Image Registration. Register an aerial photo to an orthophoto.
Read an aerial photo into the MATLAB workspace and view it.
unregistered = imread('westconcordaerial.png'); figure, imshow(unregistered)
Aerial Photo
Read an orthophoto into the MATLAB workspace and view it.
figure, imshow('westconcordorthophoto.png')
Orthophoto
Load control points that were previously picked.
load westconcordpoints
Create a transformation structure for a projective transformation using the points.
t_concord = cp2tform(movingPoints,fixedPoints,'projective');
Get the width and height of the orthophoto, perform the transformation, and view the result.
info = imfinfo('westconcordorthophoto.png'); registered = imtransform(unregistered,t_concord,... 'XData',[1 info.Width], 'YData',[1 info.Height]); figure, imshow(registered)
Transformed Image
Image Registration. The imtransform
function
automatically shifts the origin of your output image to make as much
of the transformed image visible as possible. If you use imtransform
to
do image registration, the syntax B = imtransform(A,tform)
can
produce unexpected results. To control the spatial location of the
output image, set 'XData'
and 'YData'
explicitly.
Pure Translation. Calling
the imtransform
function with a purely translational
transformation, results in an output image that is exactly like the
input image unless you specify 'XData'
and 'YData'
values
in your call to imtransform
. For example, if you
want the output to be the same size as the input revealing the translation
relative to the input image, call imtransform
as
shown in the following syntax:
B = imtransform(A,T,'XData',[1 size(A,2)],... 'YData',[1 size(A,1)])
For more information about this topic, see Perform Simple 2D Translation Transformation.
Transformation Speed. When
you do not specify the outputspace location for B
using 'XData'
and 'YData'
, imtransform
estimates
the location automatically using the function findbounds
.
You can use findbounds
as a quick forwardmapping
option for some commonly used transformations, such as affine or projective.
For transformations that do not have a forward mapping, such as the
polynomial ones computed by fitgeotrans
, findbounds
can
take much longer. If you can specify 'XData'
and 'YData'
directly
for such transformations, imtransform
may run noticeably
faster.
Clipping. The automatic
estimate of 'XData'
and 'YData'
using findbounds
sometimes
clips the output image. To avoid clipping, set 'XData'
and 'YData'
directly.
Arbitrary Dimensional Transformations. Use
a 2D transformation for tform
when using imtransform
.
For arbitrarydimensional array transformations, see tformarray
.
checkerboard
 cp2tform
 imresize
 imrotate
 makeresampler
 maketform
 tformarray