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 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.
Project Structure
Before we get started implementing our Python script for this tutorial, let’s first review our project directory structure:
1 2 3 4 5 6 | $ tree . --dirsfirst . ├── lucid.jpg └── opencv_flip.py 0 directories, 2 files |
The directory contains :
- Our Python script file
opencv_flip.py
. - Our experimental image,
lucid.jpg
.
Let’s now implement our
Python script using OpenCV!opencv_flip.py
Implementing the Image Flipping Script
First, make a new script, naming it opencv_flip.py
, and insert the following code:
1 2 3 4 5 6 7 8 | # import the necessary packages import argparse import cv2 # 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).
13 14 15 | # load the image and show it on screen image = cv2.imread(args['image']) cv2.imshow("Image", 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.
17 18 19 20 21 | # 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...") cv2.waitKey(0) |
Notice how the image has been mirrored horizontally.
We can do the same vertically:
23 24 25 26 27 | # 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...") cv2.waitKey(0) |
And we can combine both the horizontal and vertical flip as well:
29 30 31 32 33 34 35 36 37 | # 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 cv2.waitKey(0) cv2.destroyAllWindows() |
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:
1 2 3 4 5 | $ 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.
Summary
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.
What’s Next?
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.
Further Reading
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