# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

# imwarp

Apply geometric transformation to image

## Syntax

``B = imwarp(A,tform)``
``B = imwarp(A,D)``
``````[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 transformed image.```
````B = imwarp(A,D)` transforms the input image `A` according to the displacement field defined by `D`. ```
``````[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.```

## Examples

collapse all

Read grayscale image into workspace and display it.

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

Create a 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(J)```

Read 3-D MRI data into the workspace and visualize 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```

Create a 3-D geometric transformation object. First create a transformation matrix that rotates the image around the y-axis. Then pass the matrix to the `affine3d` object constructor.

``` theta = pi/8; t = [cos(theta) 0 -sin(theta) 0 0 1 0 0 sin(theta) 0 cos(theta) 0 0 0 0 1]```
```t = 0.9239 0 -0.3827 0 0 1.0000 0 0 0.3827 0 0.9239 0 0 0 0 1.0000 ```
`tform = affine3d(t)`
```tform = affine3d with properties: T: [4x4 double] Dimensionality: 3 ```

Apply the transformation to the image.

`mriVolumeRotated = imwarp(mriVolume,tform);`

Visualize three slice planes through the center of the transformed volumes.

```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 effect of rotation.

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

## Input Arguments

collapse all

Image to be transformed, specified as a nonsparse, real-valued array.

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

2-D or 3-D geometric transformation to perform, specified as a geometric transformation object. There are three types of geometric transformation objects: `affine2d`, `projective2d` or `affine3d`.

• If `tform` is 2-D and ```ndims(A) > 2```, such as for a truecolor 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.

Displacement field, specified as nonsparse numeric array. The displacement field defines the grid size and location of the output image. Displacement values are in units of pixels. `imwarp` assumes that `D` is referenced to the default intrinsic coordinate system. To estimate the displacement field, use `imregdemons`.

• If `A` is a 2-D grayscale image of size m-by-n, then `D` is m-by-n-by-2. The first plane of the displacement field, `D(:,:,1)`, describes the x-component of additive displacement. `imwarp` adds these values to column and row locations in `D` to produce remapped locations in `A`. Similarly, the second plane of the displacement field, `D(:,:,2)`, describes the y-component of additive displacement values.

• If `A` is a 2-D truecolor or 3-D grayscale image of size m-by-n-by-p, then `D` is:

• m-by-n-by-p-by-3. `D(:,:,:,1)` contains displacements along the x-axis, `D(:,:,:,2)` contains displacements along the y-axis, and `D(:,:,:,3)` contains displacements along the z-axis

• m-by-n-by-2, then `imwarp` applies the displacement field to one plane at a time.

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

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

Form of interpolation used, specified as one of the following values:

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.

collapse all

Size and location of output image in world coordinate system, specified as the comma-separated pair consisting of `'OutputView'` and an `imref2d` or `imref3d` spatial referencing object. The `ImageSize`, `XWorldLimits`, and `YWorldLimits` properties of the spatial referencing object define the size of the output image and the location of the output image in the world coordinate system. The use of `'OutputView'` is not available when applying displacement fields.

Value used for output pixels outside the input image boundaries, specified as the comma-separated pair consisting of `'FillValues'` and 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` can be a scalar or an array. If you specify an array, the array size must match the higher dimensions 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 another example, 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.

In this RGB image example, possibilities for `FillValues` include:

FillValueEffect
`0`Fill with black
`[0;0;0]`Fill with black
`255`Fill with white
`[255;255;255]`Fill with white
`[0;0;255]`Fill with blue
`[255;255;0]`Fill with yellow

Pad image to create smooth edges, specified as the logical value `true` or `false`. When set to `true`, `imwarp` pads the input image (with values specified by `FillValues`) to create a smoother edge in the output image. When set to `false`, `imwarp` does not pad the image. Choosing `false` (not padding) the input image can result in a sharper edge in the output image. This sharper edge can be useful to minimize seam distortions when registering two images side by side.

## Output Arguments

collapse all

Transformed image, returned as a nonsparse real-valued array. `B` is the same class as `A`.

Spatial referencing information associated with the transformed image, returned as an `imref2d` or `imref3d` spatial referencing object.

## See Also

### Using Objects

#### Introduced in R2013a

Was this topic helpful?

Download now