We’re almost done with the basics, this will be the last article and we’ll talk about Image Transformation.
If you haven’t read through our previous articles in the image processing series, you can click the links below:
- Part 1 – Loading Images in OpenCV
- Part 2 – Setting Pixels with Python OpenCV
- Part 3 – Drawing with Python OpenCV
In this article, we’ll be discussing various methods to modify images using concepts such as Translation, Rotation, Resizing, Flipping, and Cropping.
Let’s not waste any time and get right into it!
Table of Contents
Getting started with Image Transformation
All of the following programs consist of modules to be imported and will also be taking in image arguments.
This will, as such, be discussed here before we proceed to the actual concept.
Firstly, we shall import the modules used in transforming the image,
# importing the numpy module to work with pixels in images import numpy as np # importing argument parsers import argparse # importing the OpenCV module import cv2
Next, we’ll set up the argument parsers so that we can take input from the user regarding the image file location.
# initializing an argument parser object ap = argparse.ArgumentParser() # adding the argument, providing the user an option # to input the path of the image ap.add_argument("-i", "--image", required=True, help="Path to the image") # parsing the argument args = vars(ap.parse_args())
Now, let’s get into the core of the transformations!
Translation in OpenCV
Working with translation in OpenCV, we’ll first define a function that takes in the input image, and the shift in both the X and Y axes.
warpAffine method takes in both the input image, as well as the translation matrix and warps the image in the process of translation.
We then, finally, return the altered image back to the program.
# defining a function for translation def translate(image, x, y): # defining the translation matrix M = np.float32([[1, 0, x], [0, 1, y]]) # the cv2.warpAffine method does the actual translation # containing the input image and the translation matrix shifted = cv2.warpAffine(image, M, (image.shape, image.shape)) # we then return the image return shifted
Now, we have the code responsible for taking in input, and providing output. The translate function provides us with a simple direct call to the process.
# reads the image from image location image = cv2.imread(args["image"]) cv2.imshow("Original", image) # call the translation function to translate the image shifted = translate(image, 0, 100) cv2.imshow("Shifted Down", shifted) cv2.waitKey(0)
You should be receiving a image shifted down by 100 pixels alongside the original one.
Image Rotation with Python OpenCV
Let’s first define a function for rotation, this allows us to use a single line of code to rotate our image later on.
The rotate function takes in the image, the angle we have to rotate the image, and we’ll also declare a few defaults for the center and scaling.
cv2.getRotationMatrix2D allows us to create a matrix which upon warping, provides us with a rotated image.
Then, we return the rotated image.
# defining a function for rotation def rotate(image, angle, center=None, scale=1.0): (h, w) = image.shape[:2] if center is None: center = (w / 2, h / 2) # the cv2.getRotationMatrix2D allows us to create a # Rotation matrix M = cv2.getRotationMatrix2D(center, angle, scale) # the warpAffine function allows us to rotate the image # using the rotation matrix rotated = cv2.warpAffine(image, M, (w, h)) return rotated
Now, let’s test out the function by providing different angles for our images, ranging from 45 to 90 to 180!
# rotating the image by 45 degrees rotated = rotate(image, 45) cv2.imshow("Rotated by 45 Degrees", rotated) # rotating the image by 90 degrees rotated = rotate(image, 90) cv2.imshow("Rotated by -90 Degrees", rotated) # rotating the image by 180 degrees rotated = rotate(image, 180) cv2.imshow("Rotated by 180 degrees", rotated)
While we haven’t written a function which includes offsets into it, it’s much the same.
A more procedural approach to working with offsets, is given as below,
# working with offsets in images # then, rotating 45 degrees (h, w) = image.shape[:2] (cX, cY) = (w / 2, h / 2) M = cv2.getRotationMatrix2D((cX - 50, cY - 50), 45, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) cv2.imshow("Rotated by Offset & 45 Degrees", rotated) # used to wait for user input before closing the images cv2.waitKey(0)
The Upside Down, OpenCV and Flipping
Flipping in OpenCV is pretty simple, with a simple
cv2.flip function takes in two arguments, one being the image itself, and the other signifying how to flip the image.
|-1||Vertical and Horizontal Flip|
Here’s the code to flip the image,
# flipping the image horizontally flipped = cv2.flip(image, 1) cv2.imshow("Flipped Horizontally", flipped) # flipping the image vertically flipped = cv2.flip(image, 0) cv2.imshow("Flipped Vertically", flipped) # flipping the image vertically and horizontally flipped = cv2.flip(image, -1) cv2.imshow("Flipped Vertically and Horizontally", flipped) # wait for the user's key to proceed cv2.waitKey(0)
A part of me you’ll never see: Cropping
Cropping the image in
cv2 is as simple as accessing lists in Python.
There’s no function for it, as there doesn’t need to be one.
Code to crop,
# displaying the width and height of the image print("Width", image.shape) print("Height", image.shape) # cropping the image manually face = image[:400, :600] cv2.imshow("Face", face) cv2.waitKey(0) # cropping the image manually body = image[400:600, 120:600] cv2.imshow("Body", body) cv2.waitKey(0)
A good idea to resolve using a lot of code in your programs is to work with the
imutils module, which inherently provides methods to transform images.
We can also include our own custom made transformations in the module and can use it anywhere with a simple import statement!
Image transformation marks the end of the basic operations that don’t alter the properties of the images by large.
We’ll be proceeding to work on the OpenCV code that delves into the fundamentals of Image Processing now.
The basics are over, but, that doesn’t mean we’re going into the advanced topics just yet.
Here’s a look at some future works if you wish to see what you might be dealing with 😉
Here’s OpenCV and Facial Recognition for further reading!
- Official OpenCV Website
- Introduction to starting out with OpenCV
- My GitHub Repository for Image Processing
- Getting started with Translation
- Working with Rotation
- Resizing the images
- Upside down with Flipping
- Hidden from everyone, Cropping