Applying Basic Image Transformations (Scaling, Rotation, etc.) using OpenCV in Python

Image transformations play a vital role in various computer vision tasks and applications. OpenCV, a popular computer vision library, provides powerful functions to apply basic image transformations such as scaling, rotation, and more. In this article, we will explore how to use OpenCV with Python to perform these transformations effectively.

Scaling an Image

Scaling an image refers to resizing it, either by enlarging or shrinking its dimensions. OpenCV provides the resize() function to accomplish this task. Let's see an example:

import cv2

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

# Define scale factors for both width and height
scale_x = 0.5
scale_y = 0.5

# Create a resized image using the defined scale factors
resized_image = cv2.resize(image, None, fx=scale_x, fy=scale_y)

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

In the above code, we first load an image using the imread() function. Then, we define the scale factors scale_x and scale_y to shrink the image to half of its original size. Finally, we use the resize() function to create a resized image based on the scale factors.

Rotating an Image

Rotating an image involves changing its orientation by a specific angle. OpenCV's getRotationMatrix2D() and warpAffine() functions are used to rotate an image. Here's an example:

import cv2
import numpy as np

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

# Get image dimensions
height, width = image.shape[:2]

# Define the rotation angle in degrees
angle = 45

# Calculate rotation matrix
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)

# Apply the rotation to the image
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))

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

In the above code, we load an image and then determine its dimensions using the shape attribute. Next, we define the rotation angle angle (in degrees) and calculate the rotation matrix using getRotationMatrix2D(). Finally, we apply the rotation to the image using warpAffine().

Flipping an Image

Flipping an image involves mirroring it either horizontally or vertically. For horizontal flipping, we use the flip() function with flipCode=1. For vertical flipping, we use flipCode=0. Here's an example:

import cv2

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

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

# Flip the image vertically
#flipped_image = cv2.flip(image, 0)

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

In the above code, we first load an image. Then, we use the flip() function to flip the image horizontally by setting flipCode=1. If we want to flip the image vertically, we need to set flipCode=0.

Conclusion

Performing basic image transformations such as scaling, rotation, and flipping is essential in various computer vision tasks. OpenCV, along with Python, provides an efficient way to accomplish these transformations. In this article, we explored how to scale, rotate, and flip images using OpenCV. Feel free to experiment with different images and parameters to explore further possibilities with image transformations using OpenCV!


noob to master © copyleft