The rotation of the image is very similar to the translation of the image, and is also implemented using cv2.warpAffine
. The transformation matrix needs to be obtained through the cv2.getRotationMatrix2D
function first, and its function prototype is
cv2.getRotationMatrix2D(center, angle, scale)
in
-
center
rotation center point coordinates -
angle
The angle of rotation, the unit is angle, the positive number represents the counterclockwise direction, and the negative number represents the clockwise direction -
scale
zoom factor
This function returns the affine transformation matrix M, and then we pass this affine transformation matrix to the next function:
Example
import cv2 import numpy as np image = cv2.imread("lenna.png") cv2.imshow("original image", image) column, row, channel = image.shape M = cv2.getRotationMatrix2D((column/2,row/2), 45, 1) dst = cv2.warpAffine(image, M, (column, row)) cv2.imshow('dst', dst) cv2.waitKey(0) cv2.destroyAllWindows()
This article is reproduced from https://xugaoxiang.com/2022/07/09/opencv-rotation/
This site is for inclusion only, and the copyright belongs to the original author.