Create randomized 2-D affine transformation

creates an
`tform`

= randomAffine2d`affine2d`

object with default property values consistent with the
identity transformation.

specifies the type of affine transformations using name-value pair arguments.`tform`

= randomAffine2d(`Name,Value`

)

Read and display an image.

```
I = imread('kobi.png');
imshow(I)
```

Create an `affine2d`

transformation object that rotates images. The `randomAffine2d`

function picks a rotation angle randomly from a continuous uniform distribution within the interval [35, 55] degrees.

`tform1 = randomAffine2d('Rotation',[35 55]);`

Rotate the image and display the result.

J = imwarp(I,tform1); imshow(J)

The transformation object, `tform1`

, rotates all images by the same amount. To rotate an image by a different randomly selected amount, create a new `affine2d`

transformation object.

```
tform2 = randomAffine2d('Rotation',[-10 10]);
J2 = imwarp(I,tform2);
imshow(J2)
```

Read and display an image.

```
I = imread('sherlock.jpg');
imshow(I)
```

Create an `affine2d`

transformation object that rotates images. To select a rotation angle from a custom range, specify the `'Rotation'`

name-value pair argument as a function handle. This example specifies a function called `myrange`

(defined at the end of the example) that selects an angle from within two disjoint intervals.

`tform = randomAffine2d('Rotation',@myrange);`

Rotate the image and display the result.

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

**Supporting Function**

This example defines the `myrange`

function that first randomly selects one of two intervals (-10, 10) and (170, 190) with equal probability. Within the selected interval, the function returns a single random number from a uniform distribution.

function angle = myrange() if randi([0 1],1) a = -10; b = 10; else a = 170; b = 190; end angle = a + (b-a).*rand(1); end

Specify optional
comma-separated pairs of `Name,Value`

arguments. `Name`

is
the argument name and `Value`

is the corresponding value.
`Name`

must appear inside quotes. You can specify several name and value
pair arguments in any order as
`Name1,Value1,...,NameN,ValueN`

.

`tform = randomAffine2d('XReflection',true)`

`'XReflection'`

— Random horizontal reflection`false`

(default) | `true`

Random horizontal reflection, specified as the comma-separated pair consisting of
`'XReflection'`

and `false`

or
`true`

. When `XReflection`

is
`true`

(`1`

), the transformation
`tform`

reflects images vertically with 50% probability. By
default, the transformation does not reflect images in the horizontal
direction.

`'YReflection'`

— Random vertical reflection`false`

(default) | `true`

Random vertical reflection, specified as the comma-separated pair consisting of
`'YReflection'`

and `false`

or
`true`

. When `YReflection`

is
`true`

(`1`

), the transformation
`tform`

reflects images vertically with 50% probability. By
default, the transformation does not reflect images in the vertical direction.

`'Rotation'`

— Range of rotation`[0 0]`

(default) | 2-element numeric vector | function handleRange of rotation, in degrees, applied to the input image, specified as the
comma-separated pair consisting of `'Rotation'`

and one of the following.

2-element numeric vector. The second element must be larger than or equal to the first element. The rotation angle is picked randomly from a continuous uniform distribution within the specified interval.

function handle. The function must accept no input arguments and return the rotation angle as a numeric scalar. Use a function handle to pick rotation angles from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, see Create Function Handle (MATLAB).

By default, the transformation `tform`

does not rotate
images.

**Example: **`[-45 45]`

`'Scale'`

— Range of uniform scaling`[1 1]`

(default) | 2-element numeric vector | function handleRange of uniform (isotropic) scaling applied to the input image, specified as the
comma-separated pair consisting of `'Scale'`

and one of the following.

2-element numeric vector. The second element must be larger than or equal to the first element. The scale factor is picked randomly from a continuous uniform distribution within the specified interval.

function handle. The function must accept no input arguments and return the scale factor as a numeric scalar. Use a function handle to pick scale factors from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, see Create Function Handle (MATLAB).

By default, the transformation `tform`

does not scale
images.

**Example: **`[0.5 4]`

`'XShear'`

— Range of horizontal shear`[0 0]`

(default) | 2-element numeric vector | function handleRange of horizontal shear applied to the input image, specified as the
comma-separated pair consisting of `'XShear'`

and one of the
following. Shear is measured as an angle in degrees, and is in the range (–90, 90).

2-element numeric vector. The second element must be larger than or equal to the first element. The horizontal shear angle is picked randomly from a continuous uniform distribution within the specified interval.

function handle. The function must accept no input arguments and return the horizontal shear angle as a numeric scalar. Use a function handle to pick horizontal shear angles from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, see Create Function Handle (MATLAB).

By default, the transformation `tform`

does not shear images in
the horizontal direction.

**Example: **`[0 45]`

`'YShear'`

— Range of vertical shear`[0 0]`

(default) | 2-element numeric vector | function handleRange of vertical shear applied to the input image, specified as the
comma-separated pair consisting of `'YShear'`

and one of the
following. Shear is measured as an angle in degrees, and is in the range (–90, 90).

2-element numeric vector. The second element must be larger than or equal to the first element. The vertical shear angle is picked randomly from a continuous uniform distribution within the specified interval.

function handle. The function must accept no input arguments and return the vertical shear angle as a numeric scalar. Use a function handle to pick vertical shear angles from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, see Create Function Handle (MATLAB).

By default, the transformation `tform`

does not shear images in
the vertical direction.

**Example: **`[0 45]`

`'XTranslation'`

— Range of horizontal translation`[0 0]`

(default) | 2-element numeric vector | function handleRange of horizontal translation applied to the input image, specified as the
comma-separated pair consisting of `'XTranslation'`

and one of the
following. Translation distance is measured in pixels.

2-element numeric vector. The second element must be larger than or equal to the first element. The horizontal translation distance is picked randomly from a continuous uniform distribution within the specified interval.

function handle. The function must accept no input arguments and return the horizontal translation distance as a numeric scalar. Use a function handle to pick horizontal translation distances from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, see Create Function Handle (MATLAB).

By default, the transformation `tform`

does not translate
images in the horizontal direction.

**Example: **`[-5 5]`

`'YTranslation'`

— Range of vertical translation`[0 0]`

(default) | 2-element numeric vector | function handleRange of vertical translation applied to the input image, specified as the
comma-separated pair consisting of `'YTranslation'`

and one of the
following. Translation distance is measured in pixels.

2-element numeric vector. The second element must be larger than or equal to the first element. The vertical translation distance is picked randomly from a continuous uniform distribution within the specified interval.

function handle. The function must accept no input arguments and return the vertical translation distance as a numeric scalar. Use a function handle to pick vertical translation distances from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, see Create Function Handle (MATLAB).

By default, the transformation `tform`

does not translate
images in the vertical direction.

**Example: **`[-5 5]`

`tform`

— Affine transformation`affine2d`

objectAffine transformation, specified as an `affine2d`

object.

`centerCropWindow2d`

| `imwarp`

| `randomAffine3d`

| `randomCropWindow2d`

- Augment Images for Deep Learning Workflows Using Image Processing Toolbox (Deep Learning Toolbox)

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)