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, ...