Welcome to Raspberry Pi 4B repository
6.Picture Rotation
Picture Rotation
Image rotation is the process that an image rotates to a certain angle according to a certain position. During the rotation, the image still maintains its original size.
After the rotated image, its horizontal axis of symmetry, vertical axis of symmetry and the origin of the center coordinate may be transformed.
Therefore, we need to transform the coordinates in the image rotation accordingly.
The coordinate transformation diagram is shown below.
If the image rotates θ with counterclockwise. According to the coordinate conversion diagram, the position of the x and y axes after rotation conversion is,
(1)
and
(2)
Take (2) into Formula (1),we can get,
(3)
After calculation,

Because the gray value of the rotated picture is equal to the gray value of the corresponding position in the original picture, we can get following mathematical expression,
f(x′,y′)=f(x,y)
The above is the principle of rotation.
OpenCV provides an function to obtain the conversion matrix directly through the function. The function is as follows,
matRotate = cv2.getRotationMatrix2D(center, angle, scale)
l center: the center point of rotation
l angle: the angle of rotation. Positive numbers indicate counterclockwise; negative numbers indicate clockwise
l scale: transform scale (zoom size). 1 indicate no change, < 1 indicate zoom out, and > 1 indicate zoom in.
Code:
|
After running the following program, two images will be displayed in the jupyterLab control interface, that is original image and the rotated image.
|
|
After running the following program, two images will be displayed in the jupyterLab control interface, as shown below.

[Original picture] [Shift picture]