Documentation

imtranslate

Translate image

Syntax

  • B = imtranslate(A,translation)
    example
  • [B,RB] = imtranslate(A,RA,translation)
  • ___ = imtranslate(___,method)
  • ___ = imtranslate(___,Name,Value)
    example

Description

example

B = imtranslate(A,translation) translates image A according to the translation specified by the translation vector translation. The function returns B, the translated image. If A has more than two dimensions and translation is a two-element vector, imtranslate applies a 2-D translation to A, one plane at a time.

[B,RB] = imtranslate(A,RA,translation) translates the spatially referenced image A with its associated spatial referencing object RA. The translation vector, translation, is in the world coordinate system. The function returns the translated spatially referenced image B, with its associated spatially referencing object, RB.

___ = imtranslate(___,method) specifies the interpolation method to use.

example

___ = imtranslate(___,Name,Value) translates the input image using name-value pairs to control various aspects of the translation.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Examples

collapse all

Translate 2-D Image

Read image into the workspace.

I = imread('pout.tif');

Translate the image.

J = imtranslate(I,[25.3, -10.1],'FillValues',255);

Display the original image and the translated image.

figure
imshow(I);
title('Original Image');
set(gca,'Visible','on');
figure
imshow(J);
title('Translated Image');
set(gca,'Visible','on');

Translate 2-D Image and View Entire Translated Image

Read image into the workspace.

I = imread('pout.tif');

Translate the image. Use the OutputView parameter to specify that you want the entire translated image to be visible.

J = imtranslate(I,[25.3, -10.1],'FillValues',255,'OutputView','full');

Display the original image and the translated image.

figure
imshow(I);
title('Original Image');
set(gca,'Visible','on');
figure
imshow(J);
title('Full Translated Image');
set(gca,'Visible','on');

Translate 3-D MRI Dataset

Load MRI data into the workspace and display it.

s = load('mri');
mriVolume = squeeze(s.D);
sizeIn = size(mriVolume);
hFigOriginal = figure;
hAxOriginal  = axes;
slice(double(mriVolume),sizeIn(2)/2,sizeIn(1)/2,sizeIn(3)/2);
grid on, shading interp, colormap gray

Apply a translation in the X,Y direction.

mriVolumeTranslated = imtranslate(mriVolume,[40,30,0],'OutputView','full');

Visualize axial slice plane taken through center of volume.

sliceIndex = round(sizeIn(3)/2);
axialSliceOriginal   = mriVolume(:,:,sliceIndex);
axialSliceTranslated = mriVolumeTranslated(:,:,sliceIndex);
imshowpair(axialSliceOriginal,axialSliceTranslated,'montage');
set(gca,'Visible','on')

Related Examples

Input Arguments

collapse all

A — Image to be transformednonsparse, numeric array | logical array

Image to be transformed, specified as a nonsparse, numeric array of any class, except uint64 and int64, or a logical array.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical

RA — Spatial referencing information associated with the input image Aspatial referencing object

Spatial referencing information associated with the input image A, specified as a spatial referencing object, imref2d or imref3d.

translation — Translation vector2-element or 3-element, nonsparse, real-valued numeric vector

Translation vector, specified as a 2-element or 3-element, nonsparse, real-valued numeric vector, such as [Tx Ty], for 2-D inputs, and [Tx Ty Tz], for 3-D inputs. Values can be fractional.

Example: J = imtranslate(I,[5.3, -10.1],'FillValues',255);

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

method — Interpolation method'linear' (default) | 'nearest' | 'cubic'

Interpolation method, specified by one of the following text strings:

MethodDescription
'cubic'Cubic interpolation. Note: This method can produce pixel values outside the original range.
'linear'Linear interpolation
'nearest'Nearest neighbor interpolation

Example: J = imtranslate(I,[5.3, -10.1],'nearest','FillValues',255);

Data Types: char

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: mriVolumeTranslated = imtranslate(mriVolume,[40,30,0],'OutputView','full');

'OutputView' — Output world limits'same' (default) | 'full'

Output world limits, specified as one of the following text strings.

Text StringDescription
'same'Output world limits are the same as the input image.
'full'Output world limits are the bounding rectangle that includes both the input image and the translated output image.

Example: mriVolumeTranslated = imtranslate(mriVolume,[40,30,0],'OutputView','full');

Data Types: char

'FillValues' — Fill values used for output pixels outside the input image0 (default) | numeric array

Fill values used for output pixels outside the input image, specified as a numeric array containing one or several fill values. imtranslate uses fill values for output pixels when the corresponding inverse transformed location in the input image is completely outside the input image boundaries.

  • If A is 2-D,FillValues must be a scalar.

  • If A is 3-D and translation is a 3-element vector, FillValues must be a scalar.

  • If A is N-D and translation is a 2-element vector, FillValues can be either scalar or an array whose size matches dimensions 3-to-N of A. For example, if A is a uint8 RGB image that is 200-by-200-by-3, FillValues can be a scalar or a 3-by-1 array.

  • If A is 4-D with size 200-by-200-by-3-by-10, FillValues can be a scalar or a 3-by-10 array.

    Some example fill values:

    Fill ValueDescription
    0Fill with black
    [0;0;0] Fill with black
    255Fill with white
    [255;255;255]Fill with white
    [0;0;255]Fill with blue
    [255;255;0]Fill with yellow

Example: J = imtranslate(I,[5.3, -10.1],'FillValues',255);

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

Output Arguments

collapse all

B — Transformed imagenonsparse, real-valued, numeric array | logical array

Transformed image, returned as a nonsparse, real-valued, numeric array or logical array. The class of B is the same as the class of A.

RB — Spatial referencing information associated with the output imagespatial referencing object

Spatial referencing information associated with the output image, returned as a spatial referencing object, imref2d or imref3d.

More About

expand all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, note the following:

  • The function supports only 2-D translation vectors, translation. 3-D translations are not supported.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

When using this function with the MATLAB Function Block, the translation input argument must be a compile-time constant.

Tips

  • imtranslate is optimized for integrally valued translation vectors.

  • When 'OutputView' is 'full' and translation is a fractional number of pixels, the world limits of the output spatial referencing object RB are expanded to the nearest full pixel increment such that RB contains both the original and translated images at the same resolution as the input image A. The additional image extent in each is added on one side of the image, in the direction that the translation vector points. For example, when translation is fractional and positive in both X and Y, then the maximum of XWorldLimits and YWorldLimits is expanded to enclose the 'full' bounding rectangle at the resolution of the input image.

See Also

| |

Introduced in R2014a

Was this topic helpful?