Geometric transformations are used to modify the position, size, or orientation of an image. In OpenCV, the most common transformations include translation, scaling, and rotation. These operations are essential in tasks like data augmentation, preprocessing, and object alignment.
Translation means shifting an image along the X or Y axis. We use cv2.warpAffine()
with a translation matrix.
import cv2
import numpy as np
img = cv2.imread("sample.jpg")
rows, cols = img.shape[:2]
# Translation matrix (shift by 100px right, 50px down)
M = np.float32([[1, 0, 100], [0, 1, 50]])
translated = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("Translated Image", translated)
cv2.waitKey(0)
cv2.destroyAllWindows()
Scaling means enlarging or shrinking the image using cv2.resize().
It can use different interpolation methods: INTER_LINEAR, INTER_CUBIC, INTER_NEAREST.
scaled_up = cv2.resize(img, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_CUBIC)
scaled_down = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
cv2.imshow("Scaled Up", scaled_up)
cv2.imshow("Scaled Down", scaled_down)
cv2.waitKey(0)
cv2.destroyAllWindows()
Rotation involves turning an image around its center using
cv2.getRotationMatrix2D() and cv2.warpAffine().
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
# Rotate image by 45 degrees
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(img, M, (w, h))
cv2.imshow("Rotated Image", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
✅ In this lesson, you learned about translation, scaling, and rotation in OpenCV.
These geometric transformations are vital for data preprocessing, augmentation, and object detection pipelines.
👉 Next, we will explore image thresholding techniques.
