How does opencv keep the rotation of image content
This article mainly introduces "how to keep the rotation of image content in opencv". In daily operation, I believe many people have doubts about how to keep the rotation of image content in opencv. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to keep the rotation of image content in opencv". Next, please follow the editor to study!
In opencv, to rotate an image, simply call the following code:
M = cv2.getRotationMatrix2D ((width//2, height//2), 45,1) image_new = cv2.warpAffine (image, M, (width, height))
But this call will lose the image content, as follows: cdn.nlark.com/yuque/0/2020/png/1445121/1599750201721-e1633576-dcb6-45a7-8550-da35510ff367.png ">
But many times we want the image to remain intact after rotation:
Here is a solution:
Def rotate_keep (degree, width, height): "" rotation of lossless images @ param degree: @ param width: @ param height: @ return: rotation matrix New width and height "" height_new = int (width * fabs (sin (radians (degree) + height * fabs (cos (radians (degree) width_new = int (height * fabs (sin (radians (degree) + width * fabs (cos (radians (degree) mat_rotation = cv2.getRotationMatrix2D ((width / 2, height / 2), degree, 1) mat_rotation [0,2] + = (width_new-width) / 2 mat_rotation [1 2] + = (height_new-height) / 2 return mat_rotation, width_new, height_newangle = 45height, width = image.shape [0:2] M, width_new, height_new = rotate_keep (45, width, height) image_new = cv2.warpAffine (image, M, (width_new, height_new))
The code divides the whole process into three steps
Rotate the picture first
Calculate the theoretical image size after rotation
Translate the image so that the coordinates are negative and the coordinates are positive
At this point, the study on "how to keep the rotation of image content in opencv" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!