Last Updated : 07 Aug, 2025
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 TranslationLet's see key concepts in Image Translation which are as follows:
1. Translation MatrixThe 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:
This matrix is used to move every pixel in the image by the specified amount without distorting its content.
2. OpenCV FunctionOpenCV 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:
Example 1: Translating the Image Right and DownNote: 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.
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.
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:
Translating the Image Right and Down Example 2: Performing Multiple TranslationsIn this example, we perform four different translations on the same image: left, right, top and bottom.
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:
Multiple TranslationsLet's see some common advantages of Image Translation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4