MENU

[AI from Scratch] Episode 274: Image Preprocessing

TOC

Recap and Today’s Theme

Hello! In the previous episode, we introduced OpenCV and learned how to perform basic operations like loading, displaying, resizing, and color space conversion.

Today, we will discuss the important topic of image preprocessing. Preprocessing is a crucial step that enhances image quality and prepares the data for analysis, leading to improved model accuracy. Specifically, we will cover resizing, normalization, filtering, and data augmentation techniques.

What is Image Preprocessing?

Image preprocessing involves preparing image data before feeding it into a machine learning model. Preprocessing improves image quality by reducing noise, adjusting contrast, and unifying image sizes, which makes image analysis more effective. Common preprocessing techniques include:

  • Resizing: Adjusting the size of images to standardize them.
  • Normalization: Scaling pixel values to a specific range.
  • Filtering: Removing noise and enhancing image features.
  • Data Augmentation: Applying transformations to increase the size of the dataset.

1. Resizing

Purpose of Resizing

Resizing is used to unify the dimensions of images, ensuring consistent input size for machine learning models. This reduces computational load and ensures the model can process the data efficiently.

Resizing with OpenCV

To resize an image using OpenCV, use the cv2.resize() function. Here’s an example:

import cv2

# Load the image
image = cv2.imread('example.jpg')

# Resize the image to 200x200 pixels
resized_image = cv2.resize(image, (200, 200))

# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this code, the image is resized to 200×200 pixels using cv2.resize().

2. Normalization

Purpose of Normalization

Normalization adjusts pixel values to a specific scale. Typically, pixel values range from 0 to 255, but machine learning models often benefit from values between 0 and 1. This improves the stability of gradient calculations during training.

How to Normalize

Normalization can be achieved by dividing each pixel value by 255:

import numpy as np

# Load the image
image = cv2.imread('example.jpg')

# Normalize the pixel values to the range of 0 to 1
normalized_image = image / 255.0

# Display the normalized pixel data
print(normalized_image)

In this example, pixel values are scaled to the 0–1 range, which helps the model process the data more efficiently.

3. Filtering

Purpose of Filtering

Filtering helps remove noise and enhance image features, improving the quality of input data for machine learning models. Common filtering techniques include blurring and edge detection.

Blurring (Smoothing)

Blurring reduces noise in an image by averaging neighboring pixel values. A common method is Gaussian blur, which can be applied using cv2.GaussianBlur():

# Apply Gaussian blur (kernel size: 15x15)
blurred_image = cv2.GaussianBlur(image, (15, 15), 0)

# Display the blurred image
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this code, a Gaussian blur is applied using a 15×15 kernel size, which reduces noise in the image.

Edge Detection

Edge detection highlights the boundaries of objects within an image. OpenCV provides the cv2.Canny() function for edge detection:

# Perform edge detection
edges = cv2.Canny(image, 100, 200)

# Display the edges
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

The cv2.Canny() function detects edges based on the specified thresholds.

Smoothing (Averaging Filter)

Smoothing, or averaging, can be done using an averaging filter to reduce noise:

# Apply an averaging filter (kernel size: 5x5)
smoothed_image = cv2.blur(image, (5, 5))

# Display the smoothed image
cv2.imshow('Smoothed Image', smoothed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here, a 5×5 averaging filter is applied to smooth the image.

4. Data Augmentation

Purpose of Data Augmentation

Data augmentation involves creating new data from existing images by applying transformations like rotation, flipping, and scaling. This increases the diversity of the dataset and improves model performance, especially when the available data is limited.

Rotating Images

To rotate an image using OpenCV, use cv2.getRotationMatrix2D() and cv2.warpAffine():

# Create a rotation matrix (rotate by 45 degrees)
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 45, 1)

# Rotate the image
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))

# Display the rotated image
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, the image is rotated 45 degrees around its center.

Flipping Images

To flip an image horizontally or vertically, use cv2.flip():

# Flip the image horizontally
flipped_image = cv2.flip(image, 1)

# Display the flipped image
cv2.imshow('Flipped Image', flipped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Setting the second argument of cv2.flip() to 1 flips the image horizontally, while setting it to 0 flips it vertically.

Summary

In this episode, we discussed the importance of image preprocessing and learned various techniques to improve image quality before analysis. We covered resizing, normalization, filtering, and data augmentation, all of which are essential for preparing images for machine learning tasks.

Next Episode Preview

In the next episode, we will learn about convolution and its role in image processing, focusing on how it is applied in computer vision.


Notes

  1. Normalization: Scaling data values to a common range, such as 0 to 1 for pixel values.
  2. Edge Detection: A technique for identifying boundaries and contours in an image.
  3. Data Augmentation: Increasing the size of a dataset by applying transformations to existing data.
Let's share this post !

Author of this article

株式会社PROMPTは生成AIに関する様々な情報を発信しています。
記事にしてほしいテーマや調べてほしいテーマがあればお問合せフォームからご連絡ください。
---
PROMPT Inc. provides a variety of information related to generative AI.
If there is a topic you would like us to write an article about or research, please contact us using the inquiry form.

Comments

To comment

TOC