Image Processing Toolbox User's Guide |
Apply 2-D spatial transformation to image
Syntax
B = imtransform(A,TFORM) B = imtransform(A,TFORM,INTERP) [B,XDATA,YDATA] = imtransform(...) [B,XDATA,YDATA] = imtransform(...,param1,val1,param2,val2,...)
Description
B = imtransform(A,TFORM)
transforms the image A
according to the 2-D spatial transformation defined by TFORM
, which is a spatial transformation structure (TFORM
) as returned by maketform
or cp2tform
. If ndims(A) > 2
, such as for an RGB image, then the same 2-D transformation is automatically applied to all 2-D planes along the higher dimensions.
When you use this syntax, imtransform
automatically shifts the origin of your output image to make as much of the transformed image visible as possible. If you are using imtransform
to do image registration, this syntax is not likely to give you the results you expect; you might want to set 'XData'
and 'YData'
explicitly.
B = imtransform(A,TFORM,INTERP)
specifies the form of interpolation to use. INTERP
can have one of these values. The default value is enclosed in braces ({}
).
Value |
Description |
'bicubic' |
Bicubic interpolation |
{'bilinear'} |
Bilinear interpolation |
'nearest' |
Nearest-neighbor interpolation |
Alternatively, INTERP
can be a RESAMPLER
structure returned by makeresampler
. This option allows more control over how resampling is performed.
[B,XDATA,YDATA] = imtransform(...)
returns the location of the output image B
in the output X-Y space. XDATA
and YDATA
are two-element vectors. The elements of XDATA
specify the x-coordinates of the first and last columns of B
. The elements of YDATA
specify the y-coordinates of the first and last rows of B
. Normally, imtransform computes XDATA
and YDATA
automatically so that B contains the entire transformed image A
. However, you can override this automatic computation; see below.
[B,XDATA,YDATA] = imtransform(...,param1,val1,param2,val2,...)
specifies parameters that control various aspects of the spatial transformation. This table lists all the parameters you can specify. Note that parameter names can be abbreviated and are not case sensitive.
Notes
B
using 'XData'
and 'YData'
, imtransform
estimates them automatically using the function findbounds
. For some commonly used transformations, such as affine or projective, for which a forward mapping is easily computable, findbounds
is fast. For transformations that do not have a forward mapping, such as the polynomial ones computed by cp2tform
, findbounds
can take significantly longer. If you can specify 'XData'
and 'YData'
directly for such transformations, imtransform
might run noticeably faster.
'XData'
and 'YData'
using findbounds
is not guaranteed in all cases to completely contain all the pixels of the transformed input image.
XDATA
and YDATA
might not exactly equal the input 'XData'
and 'YData'
parameters. This can happen either because of the need for an integer number of rows and columns, or if you specify values for 'XData'
, 'YData'
, 'XYScale'
, and 'Size'
that are not entirely consistent. In either case, the first element of XDATA
and YDATA
always equals the first element of 'XData'
and 'YData'
, respectively. Only the second elements of XDATA
and YDATA
might be different.
imtransform
assumes spatial-coordinate conventions for the transformation TFORM
. Specifically, the first dimension of the transformation is the horizontal or x-coordinate, and the second dimension is the vertical or y-coordinate. Note that this is the reverse of the array subscripting convention in MATLAB.
TFORM
must be a 2-D transformation to be used with imtransform
. For arbitrary-dimensional array transformations, see tformarray
.
Class Support
The input image A
can be of any nonsparse numeric class, real or complex, or it can be of class logical
. The class of B
is the same as the class of A
.
Example 1
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)
Example 2
A projective transformation can map a square to a quadrilateral. In this example, set up an input coordinate system so that the input image fills the unit square and then transform the image into the quadrilateral with vertices (0 0), (1 0), (1 1), (0 1) to the quadrilateral with vertices (-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.
I = imread('cameraman.tif'); udata = [0 1]; vdata = [0 1]; % input coordinate system tform = maketform('projective',[ 0 0; 1 0; 1 1; 0 1],... [-4 2; -8 -3; -3 -5; 6 3]); [B,xdata,ydata] = imtransform(I, tform, 'bicubic', ... 'udata', udata,... 'vdata', vdata,... 'size', size(I),... 'fill', 128); subplot(1,2,1), imshow(udata,vdata,I), axis on subplot(1,2,2), imshow(xdata,ydata,B), axis on
Example 3
Register an aerial photo to an orthophoto.
Load control points that were previously picked.
Create a transformation structure for a projective transformation.
Get the width and height of the orthophoto and perform the transformation.
info = imfinfo('westconcordorthophoto.png'); registered = imtransform(unregistered,t_concord,... 'XData',[1 info.Width], 'YData',[1 info.Height]); figure, imshow(registered)
See Also
checkerboard
, cp2tform
, imresize
, imrotate
, maketform
, makeresampler
, tformarray
imtophat | imview |
© 1994-2005 The MathWorks, Inc.