Affine Align Transformations: A Practical Guide to Image Alignment and Transformation

Babykrishna Rayaguru
5 min readOct 18, 2023

--

In the realm of computer vision, precision is paramount. Accurate pose detection, especially in challenging scenarios involving occluded human figures, is a crucial objective for researchers and developers. In this segment, we’ll delve deeper into real-world applications of affine transformations and explore a practical implementation of the technique using an image of a snowball.

Why Use Affine Align Transformations in Real-World Applications?

  1. Accurate Pose Recognition: Affine align transformations enable the precise alignment of human poses, even in situations where parts of the body are obscured. This accuracy is crucial in applications like gesture recognition, sports analysis, and virtual reality, where subtle movements matter.
  2. Normalization of Poses: Affine transformation can normalize different body poses, ensuring consistency in the orientation and scale of body parts across different images. This normalization is crucial for accurate pose estimation.
  3. Robustness to Variations: Affine alignment makes the pose estimation model more robust to variations in lighting conditions, backgrounds, and body poses. It helps in generalizing the model’s learning across diverse scenarios.
  4. Improved Feature Extraction: Applying affine transformations aligns body parts in a standardized manner. This alignment enhances the accuracy of feature extraction algorithms, capturing relevant information for pose estimation.

Implementing Affine Align Transformations: A Step-by-Step Guide

Affine alignment transformations might sound complex, but breaking down the process into manageable steps reveals the underlying simplicity. Let’s explore the key steps involved in the provided code snippets to implement affine transformations using different libraries.

How to Implement in OpenCV:

Step 1: Read the Image The process starts by reading the input image using OpenCV and converting it to RGB format, ensuring consistent handling of color channels.

Step 2: Define Transformation Parameters Specify the transformation parameters, such as rotation angle, scaling factors, translation distances, and shearing factors. These parameters define how the image will be transformed.

Step 3: Create Transformation Matrices Construct individual transformation matrices for scaling, rotation, shearing, and translation. Combine these matrices into a single transformation matrix to represent the overall transformation.

Step 4: Apply the Affine Transformation Use the cv2.warpAffine() function, providing the input image and the transformation matrix. This function applies the transformation to the image, generating the transformed output.

Step 5: Display the Results Visualize the original and transformed images using matplotlib. This step allows you to compare the effects of the affine transformation on the input image.

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read the input image using OpenCV
input_image = cv2.imread('snowball.jpeg')
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB) # Convert image to RGB format

# Define the affine transformation parameters
rotation_angle = np.pi / 4 # Rotation angle (45 degrees)
scaling_factor_x = 0.5 # Scaling factor along the x-axis
scaling_factor_y = 0.7 # Scaling factor along the y-axis
translation_x = 50 # Translation along the x-axis
translation_y = -30 # Translation along the y-axis
shear_factor_x = 0.2 # Shearing factor along the x-axis
shear_factor_y = 0.3 # Shearing factor along the y-axis

# Create the transformation matrices for different transformations
scaling_matrix = np.array([[scaling_factor_x, 0],
[0, scaling_factor_y]])

rotation_matrix = np.array([[np.cos(rotation_angle), -np.sin(rotation_angle)],
[np.sin(rotation_angle), np.cos(rotation_angle)]])

shearing_matrix = np.array([[1, shear_factor_x],
[shear_factor_y, 1]])

translation_matrix = np.array([[translation_x], [translation_y]])

# Combine all transformations into a single transformation matrix
transformation_matrix = np.hstack((scaling_matrix.dot(rotation_matrix).dot(shearing_matrix), translation_matrix))

# Get input image dimensions
height, width, _ = input_image.shape

# Apply the affine transformation to the input image
transformed_image = cv2.warpAffine(input_image, transformation_matrix, (width, height))

# Display the original and transformed images
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(input_image)
plt.title('Original Image')

plt.subplot(1, 2, 2)
plt.imshow(transformed_image)
plt.title('Transformed Image')

plt.show()
Output of the above code | Image by Author

How to Implement in SciPy:

Step 1: Read the Image Similar to the OpenCV approach, begin by reading the input image using a library like PIL and convert it to a NumPy array in RGB format.

Step 2: Define Transformation Parameters Define rotation angle, scaling factors, translation distances, and shearing factors, just like in the OpenCV method. These parameters guide the transformation.

Step 3: Create the Affine Transformation Matrix Construct a 2x3 transformation matrix representing the combination of scaling, rotation, shearing, and translation. This matrix defines the complete transformation.

Step 4: Apply the Affine Transformation Use the scipy.ndimage.affine_transform() function, passing the input image and the transformation matrix. This function applies the specified transformations to the image.

Step 5: Visualize the Results Display both the original and transformed images side by side using matplotlib. This step allows a direct comparison between the input and the output after the affine alignment transformation.

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from scipy.ndimage import affine_transform

# Read the input image in RGB format
image_path = 'snowball.jpeg'
input_image = np.array(Image.open(image_path).convert('RGB'))

# Define the transformation parameters
rotation_degrees = 45
scaling_factor_x, scaling_factor_y = 1.2, 1.5
shearing_factor_x, shearing_factor_y = 0.2, 0.3
translation_x, translation_y = 50, -30

# Define the affine transformation matrix
matrix = np.array([[scaling_factor_x, shearing_factor_x, translation_x],
[shearing_factor_y, scaling_factor_y, translation_y]])

# Apply the affine transformation to each color channel separately
transformed_image = np.empty_like(input_image)
for channel in range(3): # Iterate over RGB channels
transformed_image[..., channel] = affine_transform(input_image[..., channel], matrix)

# Plot the original and transformed images
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
ax[0].imshow(input_image)
ax[0].set_title('Original Image')

ax[1].imshow(transformed_image.astype(np.uint8))
ax[1].set_title('Transformed Image')

plt.show()
Output of the above code | Image by Author

By understanding these steps, you can effortlessly implement affine transformations in your projects, choosing the method that aligns with your specific requirements and preferences. Whether you opt for OpenCV’s versatility or SciPy’s simplicity, mastering affine transformations opens the door to a world of creative possibilities in image manipulation and computer vision applications.

Conclusion:

In the realm of computer vision, where precision and accuracy are paramount, affine alignment transformations emerge as a powerful tool.

Both the OpenCV and SciPy methods, though distinct in their implementation, share a common purpose: to unlock the transformative capabilities within digital images. OpenCV’s flexibility and comprehensive functionality make it ideal for intricate, multifaceted transformations. On the other hand, SciPy’s simplicity and ease of use cater to those seeking straightforward yet effective transformations.

--

--