Data Science OpenCV Python

How to Flip an Image using Python and OpenCV

Pinterest LinkedIn Tumblr

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:

The directory contains :

  • Our Python script file opencv_flip.py.
  • Our experimental image, lucid.jpg.

Let’s now implement our opencv_flip.py Python script using OpenCV!

Implementing the Image Flipping Script

First, make a new script, naming it opencv_flip.py, and insert the following code:

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).

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.

Figure 1: Original Image

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.

Figure 2: Flipping an image horizontally with OpenCV.

Notice how the image has been mirrored horizontally.

We can do the same vertically:

Figure 3: Flipping an image vertically with OpenCV.

And we can combine both the horizontal and vertical flip as well:

Figure 4: Flipping an image horizontally and then vertically with OpenCV.

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:

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.

Write A Comment