This tutorial will teach you to flip an image around the x-axis and y-axis using OpenCV’s
cv2.flip function. Let’s get started.
Why is image flipping Important?
That’s is a good question and let’s answer it using a real-world scenario.
Let’s say you’ve been hired to develop a computer vision application, and your goal is to train a classifier capable of recognizing each employee that is part of that company. And you have to mark their attendance to indicate if they are late or on time, without a card reader.
Pretty cool, right? Ahh… well, not so soon.
And for this task, you’ve been handed a few images — about ten samples of each employee, which isn’t enough to train a classifier.
Without a consumable amount of training samples, you’ll be left stranded, not knowing what step to take in getting more face samples of each employee.
However, to help improve the accuracy of our classifier, a simple operation we can apply is a flipping operation during the training process. Not just any flipping operation. I’m referring to more like a horizontal flip. That’s flipping across the y-axis, not a vertical flip (across the x-axis).
As it will help generate more data samples for our classifier been built during the training time (this technique is commonly referred to as data augmentation).
Without further ado, let’s understand how to harness the power of the
cv2.flip function OpenCV provides for us.
Before we get started implementing our Python script for this tutorial, let’s first review our project directory structure:
$ tree . --dirsfirst
0 directories, 2 files
The directory contains :
- Our Python script file
- Our experimental image,
Let’s now implement our
Python script using OpenCV!
Implementing the Image Flipping Script
First, make a new script, naming it
opencv_flip.py, and insert the following code:
# import the necessary packages
# initialize the argument parser and establish the arguments required
parser = argparse.ArgumentParser()
parser.add_argument('--image', required=True, help='Path to image')
args = vars(parser.parse_args())
Let’s import our required Python packages — all we need is cv2 for our OpenCV bindings and argparse for command-line arguments (lines 2 – 3). Here we only need a single argument,
--image, to specify where the input image is located (lines 6 – 8).
# load the image and show it on screen
image = cv2.imread(args['image'])
First, we will instruct OpenCV to go and find the image
"lucid.jpg", read it, and then store it in this variable
"image". Then later, display the output towards our screen.
To flip the image, we will use the
cv2.flip method, which requires two arguments: the image we want to flip and the specific code/flag to determine how we would flip the image.
Here are the following code/flag values which are available:
- Code value, 1: Indicates we flipped the image horizontally, around the y-axis.
- Code value, 0: Indicates that we want to flip the image vertically around the x-axis.
- Code value, -1: This value will flip the image around both the x-axis and y-axis.
Let’s flip some images now.
# flip the image horizontally
horizontal_flip_img = cv2.flip(image, flipCode=1)
cv2.imshow("Horizontal flip", horizontal_flip_img)
print(f"[INFO] flipping image horizontally...")
Notice how the image has been mirrored horizontally.
We can do the same vertically:
# flip the image vertically
vertical_flip_img = cv2.flip(image, flipCode=0)
cv2.imshow("Vertical flip", vertical_flip_img)
print(f"[INFO] flipping image vertically...")
And we can combine both the horizontal and vertical flip as well:
# flip the image both horizontally and vertically
flip_image = cv2.flip(image, flipCode=-1)
cv2.imshow("Horizontal and vertical flip", flip_image)
print(f"[INFO] flipping image horizontally and then vertically...")
# waits for any key to be pressed then remove any
# created gui window from the screen & memory
Display OpenCV Flip Result
Now that’s implemented, it’s time to run our script. So, fire up your terminal, and execute the following command:
$ python3 opencv_flip.py --image lucid.jpg
$ [INFO] flipping image horizontally...
$ [INFO] flipping image vertically...
$ [INFO] flipping image horizontally and then vertically...
The output we’ll get should match the results in the previous sections shown above.
This article has explained why the
cv2.flip method is powerful in computer vision and image processing. You also learned about the different flags which flip the image with respect to the x-axis, y-axis or both.
Now, what’s next? in the following tutorial, we will explore the library OpenCV’s functionalities. Until then, share, like the video above, comment, and subscribe.
We have listed some useful resources below if you thirst for more reading.
- A Simple Walk-through with NumPy for Data Science
- Drawing (Rectangles, Circles & Text) using OpenCV
- How to Split an Image into 4 Pieces OpenCV
- How to Read and Display an Image using OpenCV
- 3 Rookie Mistakes People Make Installing OpenCV | Avoid It!
- Why Google and Microsoft uses OpenCV
- Why is Python the most popular language for Data Science