Welcome to JetCobot repository
11. Picture rotation
Picture rotation
Image rotation refers to the process of rotating an image at a certain angle according to a certain position, and the image still maintains its original size during rotation.
After the image is rotated, the horizontal symmetry axis, vertical symmetry axis and center coordinate origin of the image may change, so the coordinates of the image rotation need to be converted accordingly.
As shown below:

Assuming that the image is rotated counterclockwise by θ, the rotation transformation can be obtained according to the coordinate transformation:

and

Then,

As shown below.

The grayscale value of the rotated image is equal to the grayscale value of the corresponding position in the original image as follows.
f(x′,y′)=f(x,y)
The above is the principle of rotation, but the API provided by OpenCV can directly obtain the transformation matrix through the function.
The syntax format of this function is as follows.
matRotate = cv2.getRotationMatrix2D(center, angle, scale)
center: Center point of rotation
angle: The angle of rotation. Positive number means counterclockwise; negative number means clockwise.
scale: Change the scale (scale). 1 means no change, less than 1 means reduction, greater than 1 means enlargement.
Code path:
~/jetcobot_ws/src/jetcobot_opencv/opencv_basic02_OpenCV Transform/06 pictures rotating.ipynb
xxxxxxxxxximport cv2import numpy as npimg = cv2.imread(yahboom.jpg',1)#cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 1)# mat rotate 1 center 2 angle 3 scale#100*100 25 dst = cv2.warpAffine(img, matRotate, (height,width))Display the original image and the rotated image in jupyterLab.
xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])ximport ipywidgets.widgets as widgetsimage_widget1 = widgets.Image(format='jpg', )image_widget2 = widgets.Image(format='jpg', )# create a horizontal box container to place the image widget next to eachotherimage_container = widgets.HBox([image_widget1, image_widget2])
# display the container in this cell's outputdisplay(image_container)#display(image_widget2)img1 = cv2.imread('image0.jpg',1)image_widget1.value = bgr8_to_jpeg(img1)
image_widget2.value = bgr8_to_jpeg(dst)