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

Rotation

Rotation can also be done using wrapAffine()--only the transformation matrix changes. The transformation matrix for the rotation is as follows:

Here, theta is the angle by which you want to rotate the image. The following code is an example of rotating the image by 90 degrees:

>>> import cv2>>> img = cv2.imread("image.jpg")>>> r,c = img.shape[:2]>>> M = cv2.getRotationMatrix2D((c/2,r/2),90,1)>>> new_img = cv2.warpAffine(img,M,(c,r))>>> cv2.imwrite("rotate_img.jpg", new_img)>>> cv2.imshow("rotate", new_img)

In this code, we use the getRotationMatrix2D() function to generate the transformation matrix. It takes the center for rotation, ...

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