How to perform image translation using OpenCV in Python?

Image translation is the process of shifting an image to a new position within the coordinate system. OpenCV provides the cv2.warpAffine() function along with translation matrices to perform this transformation efficiently.

Translation Matrix

To translate an image by (tx, ty) pixels, where tx is horizontal shift and ty is vertical shift, we define a 2x3 translation matrix:

import numpy as np

# Translation matrix for shifting by (tx, ty)
tx, ty = 100, 50  # 100px right, 50px down
M = np.float32([[1, 0, tx], [0, 1, ty]])
print("Translation matrix:")
print(M)
Translation matrix:
[[  1.   0. 100.]
 [  0.   1.  50.]]

Syntax

cv2.warpAffine(img, M, (width, height))

Parameters:

  • img ? Input image to be translated
  • M ? Translation matrix of type np.float32
  • (width, height) ? Output image dimensions

Note: Positive tx shifts right, negative shifts left. Positive ty shifts down, negative shifts up.

Basic Image Translation

Here's how to translate an image 100 pixels right and 50 pixels down:

import cv2
import numpy as np

# Read input image
img = cv2.imread('/path/to/image.jpg')
height, width = img.shape[:2]

# Define translation matrix (100px right, 50px down)
M = np.float32([[1, 0, 100], [0, 1, 50]])

# Apply translation
translated_img = cv2.warpAffine(img, M, (width, height))

# Display result
cv2.imshow('Original', img)
cv2.imshow('Translated', translated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Multiple Direction Translation

This example demonstrates translation in all four directions:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read image (replace with your image path)
img = cv2.imread('/path/to/image.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert for matplotlib
rows, cols = img.shape[:2]

# Define translation matrices for different directions
M_left = np.float32([[1, 0, -50], [0, 1, 0]])    # 50px left
M_right = np.float32([[1, 0, 50], [0, 1, 0]])    # 50px right
M_up = np.float32([[1, 0, 0], [0, 1, -50]])      # 50px up
M_down = np.float32([[1, 0, 0], [0, 1, 50]])     # 50px down

# Apply translations
img_left = cv2.warpAffine(img_rgb, M_left, (cols, rows))
img_right = cv2.warpAffine(img_rgb, M_right, (cols, rows))
img_up = cv2.warpAffine(img_rgb, M_up, (cols, rows))
img_down = cv2.warpAffine(img_rgb, M_down, (cols, rows))

# Display results in a 2x2 grid
plt.figure(figsize=(10, 8))
plt.subplot(221), plt.imshow(img_left), plt.title('Left (-50px)')
plt.subplot(222), plt.imshow(img_right), plt.title('Right (+50px)')
plt.subplot(223), plt.imshow(img_up), plt.title('Up (-50px)')
plt.subplot(224), plt.imshow(img_down), plt.title('Down (+50px)')

plt.tight_layout()
plt.show()

Key Points

  • Translation only changes position, not size or orientation
  • Black regions appear where the image moves away from original boundaries
  • Output image dimensions should match input to avoid clipping
  • Use negative values to translate in opposite directions

Conclusion

Image translation in OpenCV is straightforward using cv2.warpAffine() with a translation matrix. This technique is useful for image alignment, data augmentation, and creating sliding window effects in computer vision applications.

Updated on: 2026-03-26T21:58:57+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements