# Apply Geometric Transformation

Apply projective or affine transformation to an image

## Library

Geometric Transformations

`visiongeotforms`

## Description

Use the Apply Geometric Transformation block to apply projective or affine transform to an image. You can use this block to transform the entire image or portions of the image with either polygon or rectangle Regions of Interest (ROIs).

### Port Descriptions

Port Description
ImageM-by-N or M-by-N-by-P input matrix.

M: Number of rows in the image.

N: Number of columns in the image.

P: Number of color planes in the image.

TForm When you set the Transformation matrix source parameter to `Input port`, the TForm input port accepts:
• 3-by-2 matrix (affine transform) or a Q-by-6 matrix (multiple affine transforms).

• 3-by-3 matrix (projective transform) or a Q-by-9 matrix (multiple projective transforms).

Q: Number of transformations.

When you specify multiple transforms in a single matrix, each transform is applied separately to the original image. If the individual transforms produce an overlapping area, the result of the transform in the last row of the matrix is overlaid on top.
ROIWhen you set the ROI source parameter to Input port, the ROI input port accepts:
• 4-element vector rectangle ROI.

• 2L-element vector polygon ROI.

• R-by-4 matrix for multiple rectangle ROIs.

• R-by-2L matrix for multiple polygon ROIs.

R: Number of Region of Interests (ROIs).

L(L$\ge$3): Number of vertices in a polygon ROI.

### Transformations

The size of the transformation matrix will dictate the transformation type.

#### Affine Transformation

For affine transformation, the value of the pixel located at ${\left[\stackrel{^}{x},\stackrel{^}{y}\right]}^{}$in the output image, is determined by the value of the pixel located at ${\left[x,y\right]}^{}$ in the input image. The relationship between the input and the output point locations is defined by the following equations:

$\left\{\begin{array}{c}\stackrel{^}{x}=x{h}_{1}+y{h}_{2}+{h}_{3}\\ \stackrel{^}{y}=x{h}_{4}+y{h}_{5}+{h}_{6}\end{array}$

where h1, h2, ... h6 are transformation coefficients.

If you use one transformation, the transformation coefficients must be arranged as a 3-by-2 matrix as in:

$H=\left[\begin{array}{cc}{h}_{1}& {h}_{4}\\ {h}_{2}& {h}_{5}\\ {h}_{3}& {h}_{6}\end{array}\right]$

or in a 1-by-6 vector as in $H={\left[\begin{array}{cccccc}{h}_{1}& {h}_{2}& {h}_{3}& {h}_{4}& {h}_{5}& {h}_{6}\end{array}\right]}^{}$.

If you use more than one transformation, the transformation coefficients must be arranged as a Q-by-6 matrix, where each row has the format of $H={\left[\begin{array}{cccccc}{h}_{1}& {h}_{2}& {h}_{3}& {h}_{4}& {h}_{5}& {h}_{6}\end{array}\right]}^{}$, and Q is the number of transformations as in:

$H=\left[\begin{array}{cccc}{h}_{11}& {h}_{12}& ...& {h}_{16}\\ {h}_{21}& {h}_{22}& ...& {h}_{26}\\ ⋮& ⋮& ...& ⋮\\ {h}_{Q1}& {h}_{Q2}& ...& {h}_{Q6}\end{array}\right]$

When you specify multiple transforms in a single matrix, each transform is applied separately to the original image. If the individual transforms produce an overlapping area, the result of the transform in the last row of the matrix is overlaid on top.

#### Projective Transformation

For projective transformation, the relationship between the input and the output points is defined by the following equations:

$\left\{\begin{array}{c}\stackrel{^}{x}=\frac{x{h}_{1}+y{h}_{2}+{h}_{3}}{x{h}_{7}+y{h}_{8}+{h}_{9}}\\ \stackrel{^}{y}=\frac{x{h}_{4}+y{h}_{5}+{h}_{6}}{x{h}_{7}+y{h}_{8}+{h}_{9}}\end{array}$

where h1, h2, ... h9 are transformation coefficients.

If you use one transformation, the transformation coefficients must be arranged as a 3-by-3 matrix as in:

$H=\left[\begin{array}{ccc}{h}_{1}& {h}_{4}& {h}_{7}\\ {h}_{2}& {h}_{5}& {h}_{8}\\ {h}_{3}& {h}_{6}& {h}_{9}\end{array}\right]$

or in a 1-by-9 vector as in, $H={\left[\begin{array}{ccccccccc}{h}_{1}& {h}_{2}& {h}_{3}& {h}_{4}& {h}_{5}& {h}_{6}& {h}_{7}& {h}_{8}& {h}_{9}\end{array}\right]}^{}$.

If you use more than one transformation, the transformation coefficients must be arranged as a Q-by-9 matrix, where each row has the format of $H={\left[\begin{array}{ccccccccc}{h}_{1}& {h}_{2}& {h}_{3}& {h}_{4}& {h}_{5}& {h}_{6}& {h}_{7}& {h}_{8}& {h}_{9}\end{array}\right]}^{}$, and Q is the number of transformations. For example,

$H=\left[\begin{array}{cccc}{h}_{11}& {h}_{12}& ...& {h}_{19}\\ {h}_{21}& {h}_{22}& ...& {h}_{29}\\ ⋮& ⋮& ...& ⋮\\ {h}_{Q1}& {h}_{Q2}& ...& {h}_{Q9}\end{array}\right]$

### Dialog Box

The Main pane of the Apply Geometric Transformation dialog box appears as shown in the following figure.

#### Transformation matrix source

Specify input matrix source, either `Specified via dialog`, or `Input port`. If you select ```Specify via dialog```, you can enter the transformation matrix parameters in the parameter that appear with this selection.

#### Transformation matrix

Specify a 3-by-2, 3-by-3, Q-by-6,or aQ-by-9 matrix. This option appears when you set the Transformation matrix source parameter to `Specified via dialog`.

#### Interpolation method for calculating pixel value(s)

Specify interpolation method, either `Nearest neighbor`, `Bilinear`, or `Bicubic` interpolation to calculate output pixel values. See Geometric Transformation Interpolation Methods for an overview of these methods.

#### Background fill value

Specify the value of the pixels that are outside of the input image. Use either a scalar value of P-element vector.

#### Output image size and position

Specify the output image size to be either ```Same as input image```, or `Specify via dialog`. If you select to `Specify via dialog`, you can specify the bounding box in the size and location parameters that appear with this selection.

#### Size [height width]

Specify the height and width for the output image size as```[height width]```. You can specify this parameter, along with the Location of the upper left corner [x y] parameter when you set the Output image size and position parameter to `Specify via dialog`.

#### Location of the upper left corner [x y]

Specify the [x y] location for the upper left corner of the output image. You can specify this parameter, along with the Size [height width] parameter, when you set the Output image size and position parameter to ```Specify via dialog```.

#### Process pixels in

Specify the region in which to process pixels. Specify ```Whole input image```, `Rectangle ROI`, or ```Polygon ROI```. If you select `Rectangle ROI`, or ```Polygon ROI``` the ROI source parameter becomes available.

The transformations will be applied on the whole image, or on specified multiple ROIs. The table below outlines how transformation matrices are handled with an entire image and with single and multiple ROIs.

Number of Transformation MatricesRegion of Interest
One transformation matrixYou can apply the transformation on the entire image, single ROI or multiple ROIs.
Multiple transformation matrices
• You can apply multiple transformation matrices on one ROI or on the entire image. The transformations are done in the order they are entered in the TForm.

• You can apply multiple transformation matrices on multiple ROIs. Each transformation matrix is applied to one ROI. The first transformation matrix specified is applied to the first ROI specified. The second transformation matrix is applied to the second ROI specified, and so on. The number of transformation matrices must be equal to the number of ROIs.

#### ROI source

Specify the source for the region of interest (ROI), either ```Specify via dialog``` or `Input port`. This appears when you set the Process pixels in parameter to either `Rectangle ROI`, or `Polygon ROI`.

#### Location and size of rectangle ROI [x y width height]

Specify a 4-element vector or an R-by-4 matrix , (where R represents the number of ROIs). This parameter appears when you set the Process pixels in parameter to `Rectangle ROI`.

Specify the rectangle by its top-left corner and size in width and height. If you specify one ROI, it must be a 4-element vector of format [x y width height]. If you specify more than one ROI, it must be an R-by-4 matrix, such that each row's format is [x y width height].

#### Vertices of polygon ROI [x1 y1 x2 y2 ... xL yL]

Specify a 2L-element vector or an R-by-2L matrix, (where R represents the number of ROIs and L is the number of vertices in a polygon). This parameter appears when you set the Process pixels in parameter to `Polygon ROI`.

Specify the polygon by its vertices in clockwise or counter-clockwise order, with at least three or more vertices. If you specify one ROI of L vertices, it must be a 2L-element vector of format $\left[\begin{array}{ccccccc}{x}_{1}& {y}_{1}& {x}_{2}& {y}_{2}& ...& {x}_{L}& {y}_{L}\end{array}\right]$. If you specify more than one ROI, it must be an R-by-2L matrix, where L is the maximum number of vertices in the ROIs. For ROI with vertices fewer than L, its last vertex can be repeated to form a vector.

#### Output flag indicating if any part of ROI is outside input image

Select the Output flag indicating if any part of ROI is outside input image check box to enable this output port on the Apply Geometric Transformation block.

#### For projective transformation, use quadratic approximation to calculate pixel locations

Specify whether to use an exact computation or an approximation for the projective transformation. If you select this option, you can enter an error tolerance in the Error tolerance (in pixels) parameter.

#### Error tolerance (in pixels)

Specify the maximum error tolerance in pixels. This appears when you select the For projective transformation, use quadratic approximation to calculate pixel locations check box.

#### Output flag indicating if any transformed pixels were clipped

Enable output port for flag, which indicates clipping. Clipping occurs when any of the transformed pixels fall outside of the output image.

## Examples

### Apply a Projective Transformation to an Image

The Simple projective transformation model `ex_visionApplyGeo_proj``ex_visionApplyGeo_proj`, uses the Apply Geometric Transformation block, two Constant blocks and twoVideo Viewer blocks to illustrate a basic model. The transformation matrix determines a projective transformation and is applied to the entire input image. The input image is a checker board. The steps taken to run this model were:

1. Add two Constant blocks for the input image and the transformation matrix. Set the Constant value parameters for the constant blocks as follows:

• for the input image, `"checker_board"`, and

• for the transformation matrix,```[1 0 0; .4 1 0; 0 0 1]```

2. Add two Video Viewer blocks, connecting one directly to the input image output port, and the other one to the Apply Geometric Transformation output port.

### Create an Image of a Cube using Three Regions of Interest

This example shows how to apply affine transformation on multiple ROIs of an image. It also sets the background color of the output image to a solid color purple. The input image, transformation matrix, and ROI vertices are provided to the Apply Geometric Transformation block via constant blocks. Video viewers are used to view the original image and the output image created. Open this model by typing `ex_visionApplyGeo_roi` `ex_visionApplyGeo_roi` at the MATLAB command prompt. The steps taken to run this model was:

1. Change the Process pixels in parameter to `Polygon ROI`.

2. Change the Background fill value to ```[0.5 0.5 0.75]```

3. Add three Constant blocks for the input image, transformation matrix, and ROI vertices. Set the Constant value parameters for the three blocks as follows:

• For the input image, `checker_board(20,10)`.

• For the transformation matrix,```[1 0 30 0 1 -30; 1.0204 -0.4082 70 0 0.4082 30; 0.4082 0 89.1836 -0.4082 1 10.8164]```.

• For the polygon ROI, ```[1 101 99 101 99 199 1 199; 1 1 99 1 99 99 1 99; 101 101 199 101 199 199 101 199]```.

4. Add two Video Viewer blocks, connecting one directly to the Constant block containing the input image. The other, to the Apply Geometric Transformation output port.

## References

[1] George Wolberg, "Digital Image Warping", IEEE Computer Society Press, 3rd edition, 1994.

Richard Hartley and Andrew Zisserman, "Multiple View Geometry in Computer Vision", Cambridge University Press, 2nd edition, 2003.

## Supported Data Types

PortSupported Data Types
Image
• Double-precision floating point

• Single-precision floating point

TForm
• Double-precision floating point

• Single-precision floating point

ROI
• Double-precision floating point

• Single-precision floating point

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

OutputSame as input
Err_roiBoolean
Err_clipBoolean

 `imtransform` Image Processing Toolbox™ Estimate Geometric Transformation Computer Vision System Toolbox™ Trace Boundary Computer Vision System Toolbox Blob Analysis Computer Vision System Toolbox ```Video and Image Processing Demos``` ```Video and Image Processing Demos``` Computer Vision System Toolbox