imgaug - Image Augmentation Library

0.4.0 · active · verified Sun Apr 12

imgaug is a Python library for image augmentation in machine learning experiments, particularly for deep neural networks. It supports a wide range of augmentation techniques for images, keypoints/landmarks, bounding boxes, heatmaps, and segmentation maps. The library allows for easy combination of augmenters into sequences, execution in random order or on multiple CPU cores, and provides a powerful stochastic interface. The current stable version is 0.4.0, with releases historically every few months, though the last release was in early 2020.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple augmentation sequence using `iaa.Sequential` and apply it to an image. It includes common transformations like flipping, blurring, and affine transformations. Images are expected as NumPy arrays (H, W, C) for a single image or (N, H, W, C) for a batch, typically with `uint8` dtype and pixel values in `0-255` range.

import numpy as np
import imageio.v3 as iio
import imgaug as ia
import imgaug.augmenters as iaa

# Fix seed to make examples reproducible
ia.seed(1)

# Example image (replace with your actual image loading)
image = iio.imread('https://upload.wikimedia.org/wikipedia/commons/8/8c/Default_pfp.jpg')
image = ia.quokka(size=(64, 64)) # Fallback if direct imageio fails or for testing

# Define an augmentation sequence
seq = iaa.Sequential([
    iaa.Fliplr(0.5), # horizontally flip 50% of all images
    iaa.Sometimes(0.5, # apply to 50% of images
        iaa.GaussianBlur(sigma=(0, 1.0)) # blur images with a sigma of 0 to 1.0
    ),
    iaa.Affine( # apply affine transformations to images
        scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
        translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
        rotate=(-25, 25),
        shear=(-8, 8)
    )
], random_order=True) # apply augmenters in random order

# Augment a batch of images (or a single image wrapped in a list)
# `image` should be (H, W, C) for single, or (N, H, W, C) for batch
images_aug = seq(images=[image]) # Pass a list for a single image, imgaug expects batches

# Display the original and augmented image (requires matplotlib)
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2)
axes[0].imshow(image)
axes[0].set_title('Original')
axes[1].imshow(images_aug[0])
axes[1].set_title('Augmented')
plt.show()

view raw JSON →