Photo by Umberto on Unsplash

Image Translation with OpenCV and Imutils

How to shift images along their axes using OpenCV and Imutils.

Samuel Ozechi
Analytics Vidhya
Published in
5 min readSep 22, 2021

--

Translation in computer vision refers to the process of shifting an image along its axes. Images can be shifted in upwards, downwards, sideways directions or with a combination of these directions through the image translation process. Image translation is often useful in image data augmentation and image reconstruction. I describe the steps required to shift images towards any direction using the OpenCV library and an alternative approach using a convenient function of the Imutils libraries in this post.

To shift images with OpenCV, a translation matrix is required. A translation matrix in image processing terms refers to a 2d array that specifies the direction and extent of shifting that should be applied to an image. A practical example will better describe the form and use of the matrix for image translation. I will be using the same coal miners statue image from my previous post to describe image translation..

Numpy, OpenCV ( imported as cv2) and Imutils are the libraries utilized for the image translation steps in this post. It is required to pre-install these libraries (conveniently by using the “!pip install” command for windows and its equivalent in other operating systems) before importing them for use in the script.

Shifting images with OpenCV:

First, I import the required libraries, load and then display the original image using OpenCV.

Script: Load and display the original image.

Output: Display of the loaded image. (Image by Author).

After loading the image, we will define a translation matrix which contains specifications such as direction and range of shifting the image(in pixels). We will also get the image’s spatial dimensions (width and height) which the OpenCV function requires for translating the image.

Script: Define a translation matrix to shift the image in a desired direction and get the image’s spatial dimensions.

Output: Display of the translation matrix, image’s width and image’s height. (Image by Author).

The translation matrix is a 2D Numpy array of float values. The first row of the matrix is [1., 0., 40.], the last value of the first row (40 in this example) defines the direction of shift along the x-axis and is used to shift the image sideways (to the left or right). In the example above we are to shift the image 40 pixels to the right. Negative values (e.g. -40) will set the shift to leftwards.

In the second row of the matrix ( [0., 1., 50.]), the last value (50 in this example) defines the direction of the shift along the y-axis (upwards or downwards) and is used to shift the image vertically. In the example above we are to shift the image 50 pixels downwards. Negative values (e.g. -50) will set the shift upwards.

Both rows of the translation matrix are set to shift the image rightwards and downwards according to the set ranges of pixels. We can then apply the desired shifting to the image using the defined translation matrix.

Script: Shift the image according to the defined translation matrix and display the result.

The Cv2.warpAffine function is used to shift the images according to the values defined in the translation matrix (M). It takes the read image to be translated, the translation matrix , the width and height of the image as parameter values to translate the image.

Output: Display of the image shifted 50 pixels downwards and 40 pixels rightwards.(Image by Author).

Similarly, I can shift the image leftwards and upwards by specifying negative values to the horizontal and vertical axes respectively

Script: Define a translation matrix to shift the image 70 pixels to the left and 120 pixels downwards and shift the image accordingly.

Output: Display of the image shifted 70 pixels leftwards and 120 pixels upwards. (Image by Author).

Shifting images with Imutils:

Alternative to using the OpenCV library, We could easily shift images using a convenient function of the Imutils package without needing to define a translation matrix every time we intend to shift images. This could be helpful when we are shifting multiple images and in multiple directions. The Imutils package is required to be pre-installed before use as earlier mentioned.

Images can be translated using the Imutils package by calling a translate function. The function takes parameter values of the image, shift pixels along the x-axis (0 in this example as we are not shifting the image sideways) and shift pixels along the y-axis (100 in this example) to shift the image downwards.

Output: Display of the image shifted 100 pixels downwards. (Image by Author).

Supplying positive values to the translate function shifts the image rightwards when supplied to the x-axis argument and downwards in the y-axis argument while negative values shift the image in the opposite directions (leftwards and upwards respectively).

Let’s apply leftward shift to the image by supplying negative values to the translate function.

Script: Shift the image 120 pixels to the left

Output: Display of the image shifted 120 pixels leftwards. (Image by Author)

Finally, to buttress how to specify parameter values to shift images in desired directions, we would shift the image in both directions using the Imutils package just as we did earlier with cv2.warpaffine.

Script: Shift the image 80 pixels rightwards and 60 pixels upwards

Output: Display of the image shifted 80 pixels rightwards and 60 pixels upwards. (Image by Author).

Summary: Image Translation refers to the process of shifting an image along its axes. images can be shifted in a variety of positions and directions using the OpenCV and Imutils packages. Positive and negative values can be used to create a translation a matrix to shift images using the cv2.warpaffine function or supplied in a single function call to Imutils’ translate function.

--

--