How to Perform Image Translation Using OpenCV in Python?
Image translation is the process of shifting an image left, right, up, or down. OpenCV (Open Source Computer Vision) is a open source computer libraries for vision research and applications, which provides a great deal of functionality for image processing tasks in Python. In this article, we’ll show you how to perform image translation with OpenCV in Python.
Prerequisites
Before we get started, make sure that you have OpenCV and Python installed on your system. You can verify this by typing the following commands in your terminal:
import cv2
print(cv2.__version__)
If you get an error, you might need to install OpenCV using pip:
pip install opencv-python
Loading an Image
Let’s start by loading an image. We’ll use the imread
function from OpenCV to read an image file. Suppose we have an image called example.png
in the same directory as our Python file. We can load the image like this:
import cv2
# read the image
image = cv2.imread("example.png")
# display the image
cv2.imshow("Original Image", image)
# wait for a key press
cv2.waitKey(0)
# close the window
cv2.destroyAllWindows()
This code will display the original image in a window. We can close the window by pressing any key.
Translation
The warpAffine
function from OpenCV can be used to apply an affine transformation to an image, such as translation. To perform translation on an image, we need to create a translation matrix using np.float32
. Suppose we want to shift the image by (100, 50) pixels to the right and down, respectively. We can do the following:
import cv2
import numpy as np
# read the image
image = cv2.imread("example.png")
# get the height and width of the image
height, width = image.shape[:2]
# define the translation matrix
translation_matrix = np.float32([[1, 0, 100], [0, 1, 50]])
# apply the translation matrix
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
# display the translated image
cv2.imshow("Translated Image", translated_image)
# wait for a key press
cv2.waitKey(0)
# close the window
cv2.destroyAllWindows()
This code will display the translated image in a window. We can close the window by pressing any key.
Conclusion
In this article, we covered how to perform image translation using OpenCV in Python. We loaded an image, created a translation matrix, and applied the translation matrix to the image using warpAffine
. By understanding these concepts, you can start to incorporate image translation into your own OpenCV projects.