How do I apply a linear translation to my image using the Image Processing Toolbox 3.0 (R12)?
12 views (last 30 days)
Show older comments
MathWorks Support Team
on 9 Dec 2011
Edited: MathWorks Support Team
on 25 Aug 2022
How do I apply a linear translation to my image using the Image Processing Toolbox 3.0 (R12)?
I have the x and y coordinates of a linear translation that I would like to apply to my image. How do I go about making the calculations?
Accepted Answer
MathWorks Support Team
on 25 Aug 2022
Edited: MathWorks Support Team
on 25 Aug 2022
The following is an example that applies a linear translation to an image. It uses functions from the Image Processing Toolbox 3.0 (R12) and higher.
I = checkerboard; % Load initial figure
tx = 5; % Integer translation along the horizontal component
ty = 3; % Integer translation along the vertical component
% Transform image
t = maketform('affine',[1 0 ; 0 1; tx ty]);
bounds = findbounds(t,[1 1; size(I)]);
bounds(1,:) = [1 1];
J = imtransform(I,t,'XData',bounds(:,2)','YData',bounds(:,1)');
figure, imshow(J) % Display transformed image
The behavior when the translation is a non-integer value is more complicated since the image may be resampled. Depending on the interpolation scheme, i.e., the INTERP argument of IMTRANSFORM, the size of the image may increase by 1 in either or both dimensions.
For more information, please see the documentation on MAKETFORM, FINDBOUNDS, and IMTRANSFORM. Pay particular attention to the 'XData' and 'YData' parameters available for IMTRANSFORM.
Without specifying the 'XData' and 'YData' parameters, the output image will look as if no transformation was applied to it. The following extended example describes how MATLAB applies the transformation in more detail by studying the results of a sample image:
First we define the image and the transformation structure:
a = [1 1 1; 2 2 2; 3 3 3];
TRANSLATION = [1 0 0; 0 1 0; 1 0 1]; % Translate positive 1 unit in the x-direction
tform = maketform('affine',TRANSLATION);
In order to see the positive 1 translation in the x-direction reflected in the appearance of the output, you can explicitly tell IMTRANSFORM to use a range in the output space starting at 1 in x. The documentation for IMTRANSFORM describes these under the 'XData' and 'YData' parameters.
For example,
b = imtransform(a,tform,'XData',[1 5],'YData',[1 5])
gives the output
b =
0 1 1 1 0
0 2 2 2 0
0 3 3 3 0
0 0 0 0 0
0 0 0 0 0,
or
b = imtransform(a,tform,'XData',[1 3],'YData',[1 3])
gives the output
b =
0 1 1
0 2 2
0 3 3
preserving the original input size but losing data.
Therefore, you must choose the right parameter values depending on your application.
Please note that MATLAB 6.0 (R12) was originally shipped with the Image Processing Toolbox 2.2.2 (R12). However, owners of this release are eligible to download the Image Processing Toolbox 3.0 (R12). You can start the process of downloading this product at the following URL:
Alternatively, you can receive help for obtaining this version by contacting Customer Service
0 Comments
More Answers (0)
See Also
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!