Open In App

Image Translation using OpenCV | Python

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Image translation is the process of shifting an image from one position to another. We simply move the entire image by a fixed number of pixels, either horizontally (along the x-axis) or vertically (along the y-axis). This technique is important in various computer vision tasks such as object tracking, image alignment and creating animations. We achieve this by using a transformation matrix which helps shift the image without distorting its content. In this article, we'll see image translation, how to perform it and other core concepts.

Key Concepts in Image Translation

Let's see key concepts in Image Translation which are as follows:

1. Translation Matrix

The translation matrix is used to define how much an image should be shifted. It is a 2x3 matrix that specifies the amount of horizontal and vertical shifts. The matrix looks like this:

\begin{bmatrix} 1 & 0 & T_x \\ 0 & 1 & T_y \end{bmatrix}

where:

  • T_x is the horizontal shift (in pixels).
  • T_y is the vertical shift (in pixels).

This matrix is used to move every pixel in the image by the specified amount without distorting its content.

2. OpenCV Function

OpenCV provides the cv2.wrapAffine() function to apply affine transformations like translation. This function uses the translation matrix to shift the image by the specified values of T_x and T_y .

Syntax:

cv2.warpAffine(img, M, (w, h))

Parameters:

  • img: Image to be shifted.
  • M: The translation matrix that defines how the image will be moved.
  • (w, h): Width and height of the image after translation.

Note: A positive value for tx will shift the image to the right, while a negative value for tx will shift the image to the left and a positive value for ty will shift the image down, while a negative value for ty will shift the image up.

Example 1: Translating the Image Right and Down

Here we will be using OpenCV and Numpy libraries for this implementation. Also we used a random sample image you can download it from here.

  • height, width = image.shape[:2]: image.shape[:2] gives the height and width of the image. These values are stored in height and width variables.
  • quarter_height, quarter_width = height / 4, width / 4: This calculates one-quarter of the image's height and width which will be used as the translation distance (in pixels).
  • img_translation = cv2.warpAffine(image, T, (width, height)): The image is shifted by the defined values using the matrix T. The resulting translated image is stored in img_translation.
Python
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

image = cv2.imread('/content/retriver.webp')

height, width = image.shape[:2]
quarter_height, quarter_width = height / 4, width / 4

T = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]])

img_translation = cv2.warpAffine(image, T, (width, height))

cv2_imshow(image) 
cv2_imshow(img_translation) 

Output:

t12
Translating the Image Right and Down

Example 2: Performing Multiple Translations

In this example, we perform four different translations on the same image: left, right, top and bottom.

  • rows, cols, _ = img.shape: This extracts the number of rows (height) and columns (width) from the image's shape. The third value (_) represents the number of color channels.
Python
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('/content/retriver.webp')
rows, cols, _ = img.shape

M_left = np.float32([[1, 0, -50], [0, 1, 0]])
M_right = np.float32([[1, 0, 50], [0, 1, 0]])
M_top = np.float32([[1, 0, 0], [0, 1, 50]])
M_bottom = np.float32([[1, 0, 0], [0, 1, -50]])

img_left = cv2.warpAffine(img, M_left, (cols, rows))
img_right = cv2.warpAffine(img, M_right, (cols, rows))
img_top = cv2.warpAffine(img, M_top, (cols, rows))
img_bottom = cv2.warpAffine(img, M_bottom, (cols, rows))

plt.subplot(221), plt.imshow(img_left), plt.title('Left')
plt.subplot(222), plt.imshow(img_right), plt.title('Right')
plt.subplot(223), plt.imshow(img_top), plt.title('Top')
plt.subplot(224), plt.imshow(img_bottom), plt.title('Bottom')
plt.show()

Output:

translation-of-image
Multiple Translations
  • First image shows the translation to the left by 50px.
  • Second image shows the translation to the right by 50px.
  • Third image shows the translation to the top by 50px.
  • Fourth image shows the translation to the bottom by 50px.

Real-World Applications of Image Translation

  1. Object Tracking: It helps track moving objects in video frames by shifting the image based on the object's position, important for surveillance and motion analysis.
  2. Image Stitching: It helps align parts of images when creating panoramic views, making sure that they blend seamlessly.
  3. Augmented Reality (AR): It is used to move virtual objects within real-world environments in AR applications, helping them interact naturally with the surroundings.
  4. Image Animation: It is used to create smooth animations by shifting parts of the image step by step, simulating motion or transitions.

Advantages of Image Translation

Let's see some common advantages of Image Translation:

  1. Simple Implementation: Image translation is easy to implement with basic transformation matrices, making it an accessible tool for many applications.
  2. Preserves Image Integrity: Unlike other transformations, it shifts the image without distorting or altering its content, keeping the original data intact.
  3. Improves Image Alignment: It helps align images or objects in tasks like stitching, ensuring smoother integration between different visual elements.
  4. Efficient for Image Cropping: It allows precise cropping by shifting the image to focus on the desired portion, making it ideal for creating zoomed-in or focused views.

Limitations of Image Translation

  1. Loss of Image Quality: Large translations can cause pixelation or blurriness, reducing image clarity, especially when shifting significant portions.
  2. Handling Large Translations: Shifting an image too far may crop or distort parts, losing important content or causing visual issues.
  3. Difficulty in Alignment: In tasks like stitching or tracking, translation may not perfectly align images or objects with different perspectives, leading to mismatches.
  4. Limited Real-Time Application: In real-time systems like AR or object tracking, translation can be computationally demanding, causing delays or misalignment.

Practice Tags :

Similar Reads