Accelerating the pace of engineering and science

Documentation Center

• Trial Software

imwarp

Apply geometric transformation to image

Syntax

• B = imwarp(A,tform) example
• [B,RB] = imwarp(A,RA,tform)
• B = imwarp(___,Interp)
• [B,RB] = imwarp(___,Name,Value)

Description

example

B = imwarp(A,tform) transforms the image A according to the geometric transformation defined by tform, which is a geometric transformation object. B is the output image.

[B,RB] = imwarp(A,RA,tform) transforms the spatially referenced image, specified by the image data A and the associated spatial referencing object RA. The output is a spatially referenced image specified by the image data B and the associated spatial referencing object RB.

B = imwarp(___,Interp) specifies the form of interpolation to use.

[B,RB] = imwarp(___,Name,Value) specifies parameters that control various aspects of the geometric transformation. Parameter names can be abbreviated, and case does not matter.

Code Generation: imwarp supports the generation of efficient, production-quality C/C++ code from MATLAB. When generating code, the geometric transformation object input, tform, must be either affine2d or projective2d. Additionally, the interpolation method and optional parameter names must be string constants. To see a complete list of all the list of toolbox functions that support code generation, see List of Supported Functions with Usage Notes.

Examples

expand all

Apply Horizontal Shear to Image

```I = imread('cameraman.tif');
```

Create 2-D geometric transformation object.

```tform = affine2d([1 0 0; .5 1 0; 0 0 1])
```
```tform =

affine2d with properties:

T: [3x3 double]
Dimensionality: 2```

Apply the transformation to the image.

```J = imwarp(I,tform);
figure, imshow(I), figure, imshow(J)```

Apply Rotation Transformation to 3-D MRI Dataset

Visualize 3 slice planes through center of transformed volume.

```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```

Create a rotation transformation about the Y axis

``` theta = pi/8;
t = [cos(theta)  0      -sin(theta)   0
0             1              0     0
sin(theta)    0       cos(theta)   0
0             0              0     1]

tform = affine3d(t);
```

Apply the transformation.

`mriVolumeRotated = imwarp(mriVolume,tform);`

Visualize 3 slice planes through center of transformed volume

```sizeOut = size(mriVolumeRotated);
hFigRotated = figure;
hAxRotated  = axes;
slice(double(mriVolumeRotated),sizeOut(2)/2,sizeOut(1)/2,sizeOut(3)/2);
grid on, shading interp, colormap gray```

Link views of both axes together

` linkprop([hAxOriginal,hAxRotated],'view');`

Set view to see affect of rotation

` set(hAxRotated,'View',[-3.5 20.0]);`

Input Arguments

expand all

A — Image to be transformednonsparse, real-valued array of any numeric class or logical

Image to be transformed, specified as a nonsparse, real-valued array of any numeric class or logical.

tform — 2-D or 3-D geometric transformation to performgeometric transformation object

2-D or 3-D geometric transformation to perform, specified as a geometric transformation object.

• If tform is 2-D and ndims(A) > 2, such as for an RGB image, imwarp applies the same 2-D transformation to all 2-D planes along the higher dimensions.

• If tform is 3-D, A must be a 3-D image volume.

RA — Spatial referencing information associated with the image to be transformedspatial referencing object

Spatial referencing information associated with the image to be transformed, specified as a spatial referencing object.

• If tform is a 2-D geometric transformation, RA must be a 2-D spatial referencing object (imref2d).

• If tform is a 3-D geometric transformation, RA must be a 3-D spatial referencing object (imref3d).

Interp — Form of interpolation used 'linear' (default) | 'nearest' | 'cubic'

Form of interpolation used, specified as one of the following character strings:

Interpolation MethodDescription
'linear'Linear interpolation
'nearest'Nearest-neighbor interpolation—the output pixel is assigned the value of the pixel that the point falls within. No other pixels are considered.
'cubic'Cubic interpolation

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: J = imwarp(I,tform,'FillValues',[255]) uses white pixels as fill values.

'OutputView' — Size and location of output image in world coordinate systemimref2d or imref3d spatial referencing object

Size and location of output image in world coordinate system, specified as an imref2d or imref3d spatial referencing object. The ImageSize, XWorldLimits, and YWorldLimits properties of the specified spatial referencing object define the size of the output image and the location of the output image in the world coordinate system.

'FillValues' — Value used for output pixels outside image boundariesnumeric scalar or array

Value used for output pixels outside the input image boundaries, specified as a numeric array. Fill values are used for output pixels when the corresponding inverse transformed location in the input image is completely outside the input image boundaries.

• If the input image is 2-D, FillValues must be a scalar.

• If the input image is 3-D and the geometric transformation is 3-D, FillValues must be a scalar.

• If the input image is N-D and the geometric transformation is 2-D, FillValues may be either scalar or an array whose size matches dimensions 3 to N of the input image.

For example, if the input image is a uint8 RGB image that is 200-by-200-by-3, FillValues can be a scalar or a 3-by-1 array. In this RGB image example, possibilities for FillValues include:

FillValueEffect
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

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

Output Arguments

expand all

B — Transformed imagenonsparse, real-valued array of any numeric class or logical

Transformed image, returned as a nonsparse, real-valued array of any numeric class or logical.

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

Spatial referencing information associated with the transformed image, returned as a spatial referencing object.