Basic OpenCV Data Science

OpenCV Image Translation with Python

Pinterest LinkedIn Tumblr

In this tutorial, you will learn how to translate an image using OpenCV. We’ll first start by explaining the concept of translation and then see a python implementation of it. Trust me. It’s just two lines of code. Let’s get started.

What is Image Translation?

You’ve probably seen the movement of an aircraft as it moves across the sky. Well, image translation isn’t any different from what you can see in the above gif image.

It’s simply the process of shifting the image along both the x and y-axis. All the pixel values are moved in the same direction and distance to form the original image.

For this process to be possible using OpenCV, we first need to define a 2 x 3 matrix called an affine transformation matrix:

Figure 1: An affine translation matrix used to translate an image with OpenCV

Within the translation matrix, we only care about the $t_{x}$ and $t_{y}$ values, as they provide the amount of shift in horizontal and the vertical direction:

  • A negative value of $t_{x}$ will shift the image to the left.
  • A positive value of $t_{x}$ will shift the image to the right.
  • A negative value of $t_{y}$ will shift the image upwards.
  • A positive value of $t_{y}$ will shift the image downwards.

As we mentioned earlier, translating an image requires 2 steps.

  • Load the image directly from the disk
  • Define the affine transformation matrix. As seen in figure 1.
  • Apply the cv2.warpAffine function to perform the translation.

Let’s code!

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_translate.py.
  • Our experimental image, emirates_plane.jpg.

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

Implementing the Image Translation Script

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

Let’s import our required Python packages — all we need is cv2 for our OpenCV bindings, numpy and argparse for command line arguments (lines 2 – 4). 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 "emirate_plane.jpg", read it, and then store it in this variable "image". Then display the output towards our screen.

Figure 1: Our example image the translation matrix will be applied to with OpenCV.

Next, let’s define our affine translation matrix and store it in a variable named “M”. You’ll also need to be aware that OpenCV expects this matrix to be of type float, which is why we had to convert the datatype from integers.

Since we want to shift our image 150 pixels towards the right and 50 pixels downwards, we must specify that in-place of \(t_{x}\) and \(t_{y}\) within the translation matrix shown in figure 1.

Now that we have defined our translation matrix, the main translation takes place on line 24, where we call the cv2.warpAffine function.

Within this function, we need to provide 3 parameters.

  • The first argument is the image we want to perform the shift
  • The second is our translation matrix ( already stored in the variable M)
  • The third is the dimension of the image ( the width and height)

Let’s display the results the see what we’ve accomplished up until now:

Figure 2: Translating an image 50 pixels downwards and 150 pixels to the right.

Can you notice how the image has been “shifted” down and to the right?

Yep, that’s right!

Now let’s examine another example where the image is being “shifted” upwards and to the left.

The same procedure as what we’ve done with the previous example still applies. The only difference is within lines 31 – 32. Here we will have to supply different values for both \(t_{x}\) and \(t_{y}\), which are negative.

Let’s have a look at the output:

Figure 3: Translating an image 50 pixels upwards and 150 pixels to the left.

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 what image translation is about and how to translate your images using OpenCV. We also talked about the affine translation matrix and other necessary parameters crucial for translating any image using OpenCV.

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