# Documentation

## 2-D Geometric Transformation Process Overview

To perform a general 2-D geometric transformation, follow this process:

1. Define Parameters of the Geometric Transformation — To do this, you must create a geometric transformation object. The toolbox provides several ways to create a geometric transformation object.

2. Perform the Geometric Transformation— To do this, you pass the image to be transformed and the geometric transformation object to the `imwarp` function.

The following figure illustrates this process.

### Define Parameters of the Geometric Transformation

Before you can perform a geometric transformation, you must first define the parameters of the transformation in a geometric transformation object. The following sections describe two ways you can do this:

#### Using a Transformation Matrix

If you know the transformation matrix for the geometric transformation you want to perform, you can create one of the geometric transformation objects directly, passing the transformation matrix as a parameter.

For example, you can use a 3-by-3 matrix to specify any of the affine transformations. For affine transformations, the last column must contain 0 0 1 (`[zeros(N,1); 1]`). The following table lists 2-D affine transformations with the transformation matrix used to define them. You can combine multiple affine transformations into a single matrix.

Affine Transform

Example

Transformation Matrix

Translation

`t`x specifies the displacement along the x axis

`t`y specifies the displacement along the y axis.

Scale

`s`x specifies the scale factor along the x axis

`s`y specifies the scale factor along the y axis.

Shear

`sh`x specifies the shear factor along the x axis

`sh`y specifies the shear factor along the y axis.

Rotation

`q` specifies the angle of rotation.

The following table lists the 3-D affine transformations with the transformation matrix used to define them. Note that in the 3-D case, there are multiple matrices, depending on how you want to rotate or shear the image.

3-D Affine TransformTransformation Matrix
Translation

$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& 1& 0\\ {t}_{x}& {t}_{y}& {t}_{z}& 1\end{array}\right]$

Scale

$\left[\begin{array}{cccc}{s}_{x}& 0& 0& 0\\ 0& {s}_{y}& 0& 0\\ 0& 0& {s}_{z}& 0\\ 0& 0& 0& 1\end{array}\right]$

Shearx,y shear:

$\begin{array}{l}x\text{'}=x+az\\ y\text{'}=y+bz\\ z\text{'}=z\end{array}$

$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ a& b& 1& 0\\ 0& 0& 0& 1\end{array}\right]$

x,z shear:

$\begin{array}{l}x\text{'}=x+ay\\ y\text{'}=y\\ z\text{'}=z+cy\end{array}$

$\left[\begin{array}{cccc}1& 0& 0& 0\\ a& 1& c& 0\\ 0& 0& 1& 0\\ 0& 0& 0& 1\end{array}\right]$

y, z shear:

$\begin{array}{l}x\text{'}=x\\ y\text{'}=y+bx\\ z\text{'}=z+cx\end{array}$

$\left[\begin{array}{cccc}1& b& c& 0\\ 0& 1& 0& 0\\ 0& 0& 1& 0\\ 0& 0& 0& 1\end{array}\right]$

$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& \mathrm{cos}\left(a\right)& \mathrm{sin}\left(a\right)& 0\\ 0& -\mathrm{sin}\left(a\right)& \mathrm{cos}\left(a\right)& 0\\ 0& 0& 0& 1\end{array}\right]$

$\left[\begin{array}{cccc}\mathrm{cos}\left(a\right)& 0& -\mathrm{sin}\left(a\right)& 0\\ 0& 1& 0& 0\\ \mathrm{sin}\left(a\right)& 0& \mathrm{cos}\left(a\right)& 0\\ 0& 0& 0& 1\end{array}\right]$

$\left[\begin{array}{cccc}\mathrm{cos}\left(a\right)& \mathrm{sin}\left(a\right)& 0& 0\\ -sin\left(a\right)& \mathrm{cos}\left(a\right)& 0& 0\\ 0& 0& 0& 0\\ 0& 0& 0& 1\end{array}\right]$

The following example defines the transformation matrix for a translation and creates an `affine2d` geometric transformation object.

```xform = [ 1 0 0 0 1 0 40 40 1 ]; tform_translate = affine2d(xform); ```
```tform_translate = affine2d with properties: T: [3x3 double] Dimensionality: 2```

#### Using Sets of Points

You can create a geometric transformation object by passing two sets of control point pairs to the `fitgeotrans` function. The `fitgeotrans` function estimates the transformation from these points and returns one of the geometric transformation objects.

Different transformations require a varying number of points. For example, affine transformations require three non-collinear points in each image (a triangle) and projective transformations require four points (a quadrilateral).

This example passes two sets of control points to `fitgeotrans`, which returns an affine geometric transformation object.

```movingPoints = [11 11;21 11; 21 21]; fixedPoints = [51 51;61 51;61 61]; tform = fitgeotrans(movingPoints,fixedPoints,'affine') ```
```tform = affine2d with properties: T: [3x3 double] Dimensionality: 2```

### Perform the Geometric Transformation

Once you define the transformation in a geometric transformation object, you can perform the transformation by calling the `imwarp` function, passing it the image to be transformed and a geometric transformation object. The `imwarp` function performs the specified transformation on the coordinates of the input image and creates an output image. To see an illustration of this process, see Perform Simple 2-D Translation Transformation.