# What is the difference between IMWARP and IMTRANSFORM?

137 views (last 30 days)

Show older comments

### Accepted Answer

Alex Taylor
on 14 Mar 2013

Edited: Alex Taylor
on 14 Mar 2013

The new function imwarp is part of a redesign of workflows involving image registration, geometric transformations, and spatial referencing that began with the R2013a release.

The imwarp function will replace imtransform as the recommended way of applying geometric transformations to images in the Image Processing Toolbox. As you have pointed out, imwarp is significantly faster than imtransform. imwarp supports 3-D geometric transformations, where as imtransform was strictly 2-D. imwarp uses the new spatial referencing classes imref2d and imref3d that were introduced in the R2013a release to allow users to more easily work with images in a non-default or world coordinate system. The 'OutputView' Name/Value is particularly useful for controlling the world limits and resolution of the output image. For more information about the new spatial referencing objects:

http://www.mathworks.com/help/images/ref/imref2dclass.html http://www.mathworks.com/help/images/ref/imref3dclass.html

The new function imwarp expects that the input geometric transformation is a geometric transformation object. R2013a provides three new classes of geometric transformation objects: affine2d, affine3d, and projective2d.

http://www.mathworks.com/help/images/ref/affine2dclass.html http://www.mathworks.com/help/images/ref/affine3dclass.html http://www.mathworks.com/help/images/ref/projective2dclass.html

The affine and projective geometric transformation objects expose methods for applying geometric transformations to points in a forward and inverse sense and they expose a "T" property for accessing the forward transformation matrix.

There are two reasons why the answers produced by imwarp are not exactly the same as those produced by imtransform. The first reason is that the algorithm for automatically determining the world limits of the output image in imwarp when a user does not specify an 'OutputView'

out = imwarp(I,tform);

is not the same as the algorithm used by imtransform syntax:

out = imtransform(I,tform);

The second is that imwarp determines and computes the world limits of the input and output images in terms of the outer extents of the first and last pixel in each dimension where as imtransform computes input and output boundaries in terms of the center of the first and last pixel in each dimension.

Finally, I'll provide a bit of code that will hopefully make the differences between imtransform and imwarp more concrete. The following example is taken from the last example in the following post from Steve Eddins' blog:

This was the old code segment:

% Read and display image

I = imread('pout.tif');

imshow(I);

% Create geometric transformation

T = [1 -sin(pi/4) 0; sin(pi/4) 1 0; 0 0 1];

tform = maketform('affine', T);

% Define input spatial referencing

udata = [-1 1];

vdata = [-1 1];

% Define output spatial referencing

xdata = [-0.8 0.8];

ydata = [-0.8 0.8];

output_size = round(size(I)/8);

% Apply geometric transformation to image

J = imtransform(I, tform, 'UData', udata, 'VData', vdata, ...

'XData', xdata, 'YData', ydata, 'Size', output_size);

% Display transformed image

imshow(J)

In R2013a, this can be rewritten using imwarp, geometric transformation objects, and spatial referencing objects:

% Read and display image

I = imread('pout.tif');

imshow(I);

% Create geometric transformation object

T = [1 -sin(pi/4) 0; sin(pi/4) 1 0; 0 0 1];

tform = affine2d(T);

% Define input spatial referencing. World limits of input image are from

% -1 to 1 in both X and Y directions.

RI = imref2d(size(I),[-1 1],[-1 1]);

% Define output spatial referencing. World limits out output image are

% from -0.8 to 0.8 in both X and Y directions. Output grid size is 1/8

% that of the input image.

Rout = imref2d(round(size(I)/8),[-0.8 0.8],[-0.8 0.8]);

% Apply geometric transformation to image

[J,RJ] = imwarp(I,RI,tform,'OutputView',Rout);

% Display transformed image

imshow(J,RJ)

It is worth clarifying that the results produced in this example by imwarp and imtransform are not equivalent because the XData,YData,UData,VData arguments of imtransform are referenced to the center of the first and last pixel in each dimension where as the world limits defined in the spatial referencing objects passed to imwarp are referenced to the outer extents of the first and last pixel.

We plan on supporting imtransform indefinitely for the large amount of existing customer code the relies on imtransform. imtransform will be de-emphasized in the documentation and more of the toolbox will transition toward the new geometric transformation workflow that involves transformation objects and imwarp.

##### 4 Comments

Bryan
on 11 Jul 2017

### More Answers (1)

Nikolay S.
on 30 Jul 2014

Edited: Nikolay S.
on 30 Jul 2014

Hi Alex.

Very interesting answer, I was pretty confused by seeing a few commands doing virtually the same. A few questions aroused:

1) I understand that new functions (IMWARP etc..) and the old functions (IMTRANSFORM) differ in the point of application- for example rotation. In the new ones it is image center while in the old ones it is top left corner. Thus for the same T matrix, different image tranformation will be achieved. The question is, is there an easy and precise way to calculate Tnew, that will achieve the same transformation with IMWARP as Told achieved with IMTRANSFORM? This may be a decent replacement to the wrapper Eric mentioned.

2) I understand that the new transformation functions are much faster (factor of x2, x3). FOrm what I can see, it supports however only linear (affine & projectile) transformation represented by T [3,3] of [4,4] matrix. On the other hand, IMTRANSFORM, MAKETFORM support non linear transformation, as Steve has demonstrated on his wizard image. Is this correct? If so, the optimization comes with a price of loss of generality, and MAKETFORM can not be removed, as projective2d, affine2d replace it only in case of linear transformations.

Thanks!

##### 0 Comments

### See Also

### Products

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!