0
0
Computer Visionml~5 mins

Albumentations library in Computer Vision

Choose your learning style9 modes available
Introduction
Albumentations helps you easily change images to make your AI models better at understanding different pictures.
When you want to teach a model to recognize objects in photos taken in different lighting.
When you have few images and want to create more by changing them slightly.
When you want to make your model strong against small changes like rotation or color shifts.
When you need to prepare images for training by resizing or cropping.
When you want to add random effects like blur or noise to make your model more robust.
Syntax
Computer Vision
import albumentations as A

transform = A.Compose([
    A.HorizontalFlip(p=0.5),
    A.RandomBrightnessContrast(p=0.2),
    A.Rotate(limit=40, p=0.5),
])

augmented = transform(image=image)
augmented_image = augmented['image']
Use A.Compose to combine many image changes together.
Each change has a probability p to decide if it happens or not.
Examples
This will flip the image upside down sometimes and always crop a 256x256 part.
Computer Vision
transform = A.Compose([
    A.VerticalFlip(p=0.3),
    A.RandomCrop(width=256, height=256, p=1.0),
])
This adds blur or changes colors randomly to make images look different.
Computer Vision
transform = A.Compose([
    A.Blur(blur_limit=3, p=0.4),
    A.HueSaturationValue(p=0.5),
])
Sample Model
This code loads a sample image, applies flipping, brightness/contrast change, and rotation, then shows both images. It prints a message when done.
Computer Vision
import albumentations as A
import cv2

# Load an example image
image = cv2.imread(cv2.samples.findFile('lena.jpg'))

# Define transformations
transform = A.Compose([
    A.HorizontalFlip(p=1.0),
    A.RandomBrightnessContrast(p=1.0),
    A.Rotate(limit=30, p=1.0),
])

# Apply transformations
augmented = transform(image=image)
augmented_image = augmented['image']

# Show original and augmented images
cv2.imshow('Original', image)
cv2.imshow('Augmented', augmented_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Print confirmation
print('Transformation applied successfully')
OutputSuccess
Important Notes
Albumentations works with images as numpy arrays, usually loaded by OpenCV.
Always check the probability p to control how often a change happens.
You can combine many transformations to make your dataset more varied.
Summary
Albumentations makes image changes easy and fast for training AI models.
Use Compose to group multiple changes with set chances.
Transforms help models learn better by seeing many versions of images.