Skip to content
Run in Google Colab View notebook on GitHub

Applying the same augmentation with the same parameters to multiple images, masks, bounding boxes, or keypoints

Sometimes you want to apply the same set of augmentations to multiple input objects of the same type. For example, you might have a set of frames from the video, and you want to augment them in the same way. Or you may have multiple masks for the same image, and you want to apply the same augmentation for all of them.

In Albumentations, you can declare additional targets and their types using the additional_targets argument to Compose.

For the name of an additional target, you can use any string value that is also a valid argument name in Python. Later, you will use those names to pass additional targets to a transformation pipeline. So you can't use a string that starts with a digit, such as '0image' because it is not a valid Python argument name.

The type could be either image, mask, bboxes, or keypoints.

An example definition of Compose that supports multiple inputs of the same type may be the following:

import albumentations as A

transform = A.Compose(
    [HorizontalFlip(p=0.5), ...],
        'image1': 'image',
        'image2': 'image',
        'imageN': 'image',

        'bboxes1': 'bboxes',
        'bboxes1': 'bboxes',
        'bboxesM': 'bboxes',

        'keypoints1': 'keypoints',
        'keypoints2': 'keypoints',
        'keypointsK': 'keypoints',

        'mask1': 'mask',
        'mask2': 'mask',
        'maskL': 'mask'

Import the required libraries

import random

import cv2
from matplotlib import pyplot as plt

import albumentations as A

Define a function to visualize an image

def visualize(image):
    plt.figure(figsize=(10, 10))

Load images from the disk

image = cv2.imread('images/multi_target_1.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image0 = cv2.imread('images/multi_target_2.jpg')
image0 = cv2.cvtColor(image0, cv2.COLOR_BGR2RGB)
image1 = cv2.imread('images/multi_target_3.jpg')
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

Show original images