O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Translation

For geometric transformation, OpenCV provides the wrapAffine() function, which takes the image, transformation matrix, and dimension of the image as input. The transformation matrix for the translation is as follows:

Here, (tx, ty) tells us the amount of shift in the image. For example, the following code shifts the image by (100,100):

>>> import cv2>>> import numpy as np>>> img = cv2.imread("image.jpg")>>> r,c = img.shape[:2]>>> M = np.float32([[1,0,100],[0,1,100]])>>> new_img = cv2.warpAffine(img,M,(c,r))>>> cv2.imwrite("translation.jpg", new_img)>>> cv2.imshow("translation", new_img)

The following figure is the output of the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required