How to flip an image in OpenCV Python?
Image processing is an essential skill for computer vision, and OpenCV Python is a widely popular library for performing image processing. Among many image processing techniques, flipping an image is a fundamental operation that involves flipping the image on its vertical or horizontal axis.
Flipping an image is useful in many applications, including image classification, object detection, and image segmentation. OpenCV Python offers several functions to flip an image along its axis. In this article, we will explore the most common ways to flip an image in OpenCV Python.
Requirements
To follow this tutorial, you should have the following dependencies installed on your system:
– Python 3.x
– OpenCV Python library
– Numpy library
– Matplotlib library
If you don’t have these dependencies installed, you can use the following commands to install them using pip.
pip install opencv-python
pip install numpy
pip install matplotlib
Or, if you’re using a virtual environment, you can run the following commands:
pip install -r requirements.txt
Load the image
Before we flip an image, we need to load it into our program. OpenCV Python provides a useful function to load an image from a file. Suppose we have an image named sample_image.jpg
in the same directory as our program.
Let’s create a Python script called flip_image.py
that loads the image from the file and displays it:
import cv2
import matplotlib.pyplot as plt
# Load the image
img = cv2.imread('sample_image.jpg')
# Display the image
plt.imshow(img)
plt.show()
This code reads the sample_image.jpg
file using the cv2.imread()
function and then displays the image using the matplotlib.pyplot.imshow()
function.
Run the program using the following command:
python flip_image.py
The program should display the sample_image.jpg
file in a separate window.
Flip the image horizontally
To flip an image horizontally, we can use the cv2.flip()
function. This function takes two arguments, the first being the image we want to flip, and the second being a flag that indicates the axis we want to flip over. For horizontal flipping, we set the flag value to 1
.
Let’s modify our Python script to flip the image horizontally and display the result:
import cv2
import matplotlib.pyplot as plt
# Load the image
img = cv2.imread('sample_image.jpg')
# Flip the image horizontally
horizontal_img = cv2.flip(img, 1)
# Display the original and flipped image
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.subplot(122),plt.imshow(horizontal_img),plt.title('Horizontally flipped')
plt.show()
In this code, we use the cv2.flip()
function to flip the img
horizontally and store the result in a new variable horizontal_img
. Then we use the plt.subplot()
and plt.imshow()
functions to display the original and flipped images side by side.
Run the program again using the following command:
python flip_image.py
The program should display the sample_image.jpg
file and its horizontally flipped version side by side.
Flip the image vertically
To flip an image vertically, we use the cv2.flip()
function again with the flag value set to 0
. Let’s modify our Python script to flip the image vertically:
import cv2
import matplotlib.pyplot as plt
# Load the image
img = cv2.imread('sample_image.jpg')
# Flip the image vertically
vertical_img = cv2.flip(img, 0)
# Display the original and flipped image
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.subplot(122),plt.imshow(vertical_img),plt.title('Vertically flipped')
plt.show()
In this code, we use the cv2.flip()
function to flip the img
vertically and save the result in a new variable vertical_img
. Similarly, we use the plt.subplot()
and plt.imshow()
functions to display both images side by side.
Run the program again using the following command:
python flip_image.py
This time, the program should display the sample_image.jpg
file and its vertically flipped version side by side.
Flip the image along both axes
We can also flip an image along both axes by using the cv2.flip()
function with the flag value set to -1
. Let’s modify our Python script to flip the image along both axes:
import cv2
import matplotlib.pyplot as plt
# Load the image
img = cv2.imread('sample_image.jpg')
# Flip the image along both axes
both_img = cv2.flip(img, -1)
# Display the original and flipped image
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.subplot(122),plt.imshow(both_img),plt.title('Flipped along both axes')
plt.show()
In this code, we use the cv2.flip()
function to flip the img
along both axes and save the result in a new variable both_img
. Then, we use the plt.subplot()
and plt.imshow()
functions to display both images side by side.
Run the program again using the following command:
python flip_image.py
The program should display the sample_image.jpg
file and its flipped versions along both axes side by side.
Conclusion
Flipping an image is a simple yet essential operation in image processing. In this tutorial, we learned how to flip an image in OpenCV Python using the cv2.flip()
function. We explored the three types of image flipping, namely horizontal, vertical, and along both axes. We also used the matplotlib.pyplot
library to display the original and flipped images.
Image flipping is just one of many image processing operations that can be performed using OpenCV Python. By mastering image processing with OpenCV Python, you can advance your computer vision skills and develop complex computer vision applications.