What is Image Augmentation?

Image Source: https://www.calipsa.io/blog/what-is-data-augmentation-in-deep-learning

Image augmentation is a computer vision approach for producing variations of existing images to artificially expand a dataset. Transformative techniques including rotation, translation, flipping, cropping, adding noise, and adjusting brightness and contrast can be used to achieve this. Preventing overfitting, boosting robustness, and improving the model’s resiliency to picture changes are the objectives.

Image augmentation has various applications in daily life, some of them include:

  1. Computer Vision: Used in training deep learning models for image classification, object detection, and segmentation tasks.
  2. Medical imaging: Helps in increasing the size of medical image datasets for training deep learning models, which can improve the accuracy of diagnoses.
  3. Photography and Video production: Image augmentation techniques can be used to create different variations of the same scene for visual effects in movies, advertising, and other forms of media.
  4. E-commerce: Augmented images can be used in e-commerce websites to display products from different angles, improving the customer’s experience and helping with decision-making.
  5. Virtual and Augmented Reality: Image augmentation techniques are used to generate a large number of virtual and augmented reality environments, objects, and images.

In this blog, we will discuss the steps on how to make an Image Augmentation using Python.

Image augmentation can be done in Python using the following libraries:

  1. OpenCV: A computer vision library that includes functions for image processing and computer vision tasks, including image augmentation.
  2. Pillow: A Python Imaging Library that supports opening, manipulating, and saving many different image file formats.
  3. imgaug: A library for image augmentation in Python that provides a simple and flexible way to apply a variety of transformations to images.
  4. Keras: A high-level deep learning library for Python that includes functions for image augmentation, allowing the creation of augmented images with just a few lines of code.

Here are the steps to create an image augmentation pipeline using Python:

  1. Install the necessary libraries: You can use libraries such as OpenCV, Pillow, imgaug, or Keras to perform image augmentation.
  2. Load the data: Load the images you want to augment into memory using functions from the selected library.
  3. Define the augmentation parameters: Decide on the types of transformations you want to apply to the images and set the corresponding parameters. For example, rotation angle, zoom level, or flipping direction.
  4. Apply the transformations: Use functions from the selected library to apply the transformations to the images.
  5. Save the augmented images: Save the transformed images to disk or memory.
import numpy as np
import keras
from keras.preprocessing.image import ImageDataGenerator

# load data
x_train = np.load('x_train.npy')
y_train = np.load('y_train.npy')

# create an instance of ImageDataGenerator
datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')

# fit the generator to the original data
datagen.fit(x_train)

# use the generator to generate augmented data
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
  # train your model on the augmented data
  model.fit(x_batch, y_batch)
  if model.stop_training:
    break

Note: In this example, the ImageDataGenerator class is used to generate new images by applying different transformations, such as rotation, shifting, shearing, zooming, and flipping. These augmented images can then be used to train a deep learning model to improve its performance.

For Open CV:

  1. Install OpenCV: You can install OpenCV by running pip install opencv-python or conda install -c conda-forge opencv in your terminal or command prompt.
  2. Load the data: Load the images you want to augment into memory using the OpenCV library.
  3. Define the augmentation parameters: Decide on the types of transformations you want to apply to the images and set the corresponding parameters. For example, rotation angle, scale, or translation.
  4. Apply the transformations: Use functions from the OpenCV library to apply the transformations to the images. For example, cv2.warpAffine() can be used to perform rotations, scaling, and translations.
  5. Save the augmented images: Save the transformed images to disk or memory.
import cv2
import numpy as np
import random

# Load the image
img = cv2.imread("image.jpg")

# Define the augmentation parameters
angle = random.uniform(-30, 30)
scale = random.uniform(0.7, 1.3)

# Get the image dimensions
(h, w) = img.shape[:2]

# Get the rotation matrix
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, angle, scale)

# Apply the rotation
result = cv2.warpAffine(img, M, (w, h))

# Save the augmented image
cv2.imwrite("augmented_image.jpg", result)

Note: In this example, the cv2.getRotationMatrix2D() function is used to generate a matrix that describes the desired rotation, scaling, and translation. The cv2.warpAffine() function is then used to apply this transformation to the image.

For Pillow:

  1. Install Pillow: You can install Pillow by running pip install Pillow in your terminal or command prompt.
  2. Load the data: Load the images you want to augment into memory using the Pillow library.
  3. Define the augmentation parameters: Decide on the types of transformations you want to apply to the images and set the corresponding parameters. For example, rotation angle, zoom level, or flipping direction.
  4. Apply the transformations: Use functions from the Pillow library to apply the transformations to the images. For example, Image.rotate() can be used to perform rotations and ImageOps.flip() can be used to perform flipping.
  5. Save the augmented images: Save the transformed images to disk or memory.
from PIL import Image
import random

# Load the image
img = Image.open("image.jpg")

# Define the augmentation parameters
angle = random.uniform(-30, 30)

# Apply the rotation
result = img.rotate(angle)

# Save the augmented image
result.save("augmented_image.jpg")

Note: In this example, the Image.rotate() function is used to apply a random rotation to the image. The rotated image is then saved to disk using the save() method. Similarly, you can use other functions from the Pillow library to perform other types of transformations, such as flipping, resizing, or cropping.

For Imgaug:

  1. Install imgaug: You can install imgaug by running pip install imgaug in your terminal or command prompt.
  2. Load the data: Load the images you want to augment into memory using the imgaug library.
  3. Define the augmentation parameters: Decide on the types of transformations you want to apply to the images and set the corresponding parameters. For example, rotation angle, zoom level, or flipping direction.
  4. Apply the transformations: Use functions from the imgaug library to apply the transformations to the images. For example, iaa.Affine() can be used to perform rotations, scaling, and translations, and iaa.Fliplr() can be used to perform horizontal flipping.
  5. Save the augmented images: Save the transformed images to disk or memory.
import imgaug as ia
import imgaug.augmenters as iaa
import numpy as np
import random

# Load the image
img = ia.imresize_single_image(ia.imread("image.jpg"), (256, 256))

# Define the augmentation parameters
angle = random.uniform(-30, 30)

# Apply the rotation
seq = iaa.Affine(rotate=angle)
result = seq.augment_images([img])[0]

# Save the augmented image
ia.imwrite("augmented_image.jpg", result)

Note: In this example, the iaa.Affine() function is used to define a random rotation transformation, which is then applied to the image using the augment_images() method. The rotated image is then saved to disk using the imwrite() function from the imgaug library. The imgaug library provides many other functions and classes for performing various types of transformations, such as flipping, resizing, or color jittering.

CONCLUSION

In summary, image augmentation is a method used to fictitiously expand a training dataset for computer vision applications. In order to boost the diversity of the data, it entails making random adjustments to the photos, such as rotation, flipping, scaling, or cropping. Image augmentation aids in reducing overfitting, enhancing a model’s ability to generalize, and preventing the model from memorizing the training set of data. A variety of libraries and frameworks, including OpenCV, Pillow, and imgaug, can be used to conduct image augmentation. You may use image augmentation to train more reliable computer vision models for your applications by being aware of the fundamentals of the method and the tools at your disposal.

Leave a Comment

Your email address will not be published. Required fields are marked *