Translation refers to shifting of object from one point to another.
Transformation matrix for translation is given by, T =
1 | 0 | tx |
0 | 1 | ty |
Where,
tx and ty are the amount of translation in x and y axis respectively.
cv2.warpAffine():
To perform linear mapping, we can use cv2.warpAffine(image, transformation_matrix, shape) function which takes a 2×3 matrix and convert the image based on the passed transformation matrix. It has three arguments:
- image: the image which we want to transform.
- transformation_matrix: it is a 2×3 matrix which is used to transform the input image. We can perform rotation, translation and other transformations by changing the transformation matrix.
- shape: it defines the shape(width, height) of the resultant image.
Implemented code in Python:
import cv2 import numpy as np img = cv2.imread('birds.jpg',1); rows = img.shape[0]; cols = img.shape[1]; T = np.float32([[1,0,50],[0,1,25]]); #translation matrix output_img = cv2.warpAffine(img, T, (cols,rows)); cv2.imshow('Original', img); cv2.imshow('Output Image',output_img); #wait for 10 seconds cv2.waitKey(10000); cv2.destroyAllWindows();
Output:

Implemented code in Python:
import cv2 import numpy as np img = cv2.imread('birds.jpg',1); rows = 100; cols = 350; T = np.float32([[1,0,50],[0,1,25]]); #translation matrix output_img = cv2.warpAffine(img, T, (cols,rows)); cv2.imshow('Original', img); cv2.imshow('Output Image',output_img); #wait for 10 seconds cv2.waitKey(10000); cv2.destroyAllWindows();
Output:

Thank you Rishika Gupta for this article.
0 Comments