This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Apply identical random transformations to multiple images


augI = augment(augmenter,I)



augI = augment(augmenter,I) augments image I using a random transformation from the set of image preprocessing options defined by image data augmenter, augmenter. If I consists of multiple images, then augment applies an identical transformation to all images.


collapse all

Create an image augmenter that rotates images by a random angle. To use a custom range of valid rotation angles, you can specify a function handle when you create the augmenter. This example specifies a function called myrange (defined at the end of the example) that selects an angle from within two disjoint intervals.

imageAugmenter = imageDataAugmenter('RandRotation',@myrange);

Read multiple images into the workspace, and display the images.

img1 = imread('peppers.png');
img2 = imread('corn.tif',2);
inImg = imtile({img1,img2});

Augment the images with identical augmentations. The randomly selected rotation angle is returned in a temporary variable, angle.

outCellArray = augment(imageAugmenter,{img1,img2});
angle = 8.1158

View the augmented images.

outImg = imtile(outCellArray);

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;
        a = 170;
        b = 190;
    angle = a + (b-a).*rand(1)

Input Arguments

collapse all

Augmentation options, specified as an imageDataAugmenter object.

Images to augment, specified as one of the following.

  • Numeric array, representing a single grayscale or color image.

  • Cell array of numeric and categorical images. Images can be different sizes and types.

Output Arguments

collapse all

Augmented images, returned as a numeric array or cell array of numeric and categorical images, consistent with the format of the input images I.


  • You can use the augment function to preview the transformations applied to sample images.

  • To perform image augmentation during training, create an augmentedImageDatastore and specify preprocessing options by using the 'DataAugmentation' name-value pair with an imageDataAugmenter. The augmented image datastore automatically applies random transformations to the training data.

Introduced in R2018b