contexts
In image processing, there are times when there will be an angle rotation of the picture processing, especially in computer vision for image expansion, rotating the angle expansion of the picture is a common processing. This rotation of the image is also more application scenarios, such as when the user uploads a picture that is vertical, it is not good for processing, and it also needs to be rotated for the subsequent algorithm processing. There are two common ways to rotate the processing, one is converted to numpy matrix, numpy matrix processing, the other is to use opencv comes with the function of various transformations to achieve the results of the rotation angle.
Original image:
opencv functions
The following functions are commonly used in rotation
: Transpose the image matrix
img = (origin_img_path) img_transpose = (img) ('transpose', img_transpose) (0)
: The image matrix is flipped, and the parameters can be set to 1, 0, and -1, corresponding to horizontal flip, vertical flip, and horizontal-vertical flip, respectively.
img = (origin_img_path) img_flip = (img, 1) ('flip', img_flip) (0)
cv2.getRotationMatrix2D
: Construct the rotation matrix M. Subsequent rotations can be accomplished by simply multiplying with the rotation matrix.
The rotation matrix M
img = (origin_img_path) rows, cols = # Here the first parameter is the center of rotation, the second is the angle of rotation, and the third is the scaling factor after rotation # You can prevent rotations from going out of bounds by setting the center of rotation, the scaling factor, and the size of the window. M = cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)
: perform affine transformations on images, usually translations or rotations
img = (origin_img_path) (img, M,(lengh,lengh),borderValue=(255,255,255)) # M is the rotation matrix from above
numpy function
The numpy implementation of rotation is generally done usingnumpy.rot90
Rotate images by a factor of 90 degrees
Official Description.
numpy.rot90
(m, k=1, axes=(0, 1))[source]Rotate an array by 90 degrees in the plane specified by axes.
Rotation direction is from the first towards the second axis.
k: Number of times the array is rotated by 90 degrees.
Key parametersk
Indicates a multiple of 90 degrees of rotation, k is generally 1, 2, 3, respectively, 90 degrees of rotation, 180 degrees, 270 degrees; k can also take a negative number, -1, -2, -3. k takes a positive number of counterclockwise rotation, and take a negative number of clockwise rotation.
Rotate 90 degrees
counterclockwise
- Rotate using the transpose operation + flip operation of the opencv function
- Implemented using numpy.rot90
def rotateAntiClockWise90(img_file): # Rotate 90 degrees counterclockwise img = (img_file) trans_img = (img) img90 = (trans_img, 0) ("rotate", img90) (0) return img90 def totateAntiClockWise90ByNumpy(img_file): # np.rot90(img, -1) rotate 90 degrees counterclockwise img = (img_file) img90 = np.rot90(img, -1) ("rotate", img90) (0) return img90
counterclockwise
def rotateClockWise90(self, img_file): img = (img_file) trans_img = ( img ) img90 = (trans_img, 1) ("rotate", img90) (0) return img90 def totateClockWise90ByNumpy(img_file): # np.rot90(img, 1) rotate 90 degrees clockwise img = (img_file) img90 = np.rot90(img, 1) ("rotate", img90) (0) return img90
Rotate 180 degrees, 270 degrees
utilizationnumpy.rot90
Realize rotation of 180 degrees, 270 degrees
180 degrees
img180 = np.rot90(img, 2) ("rotate", img180) (0)
270 degrees
img270 = np.rot90(img, 3) ("rotate", img270) (0)
Rotate at any angle to fill the background with any color value
import cv2 from math import * import numpy as np # rotate angle, missing background white (255, 255, 255) fill def rotate_bound_white_bg(image, angle): # grab the dimensions of the image and then determine the # center (h, w) = [:2] (cX, cY) = (w // 2, h // 2) # grab the rotation matrix (applying the negative of the # angle to rotate clockwise), then grab the sine and cosine # (., the rotation components of the matrix) # -angle position parameter for the angle parameter negative value means clockwise rotation; 1.0 position parameter scale is to adjust the size of the proportion (image scaling parameters), it is recommended that the 0.75 M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) cos = (M[0, 0]) sin = (M[0, 1]) # compute the new bounding dimensions of the image nW = int((h * sin) + (w * cos)) nH = int((h * cos) + (w * sin)) # adjust the rotation matrix to take into account translation M[0, 2] += (nW / 2) - cX M[1, 2] += (nH / 2) - cY # perform the actual rotation and return the image # borderValue missing background fill color, white here, customizable return (image, M, (nW, nH),borderValue=(255,255,255)) # borderValue default, default is black (0, 0 , 0) # return (image, M, (nW, nH)) img = ("") imgRotation = rotate_bound_white_bg(img, 45) ("img",img) ("imgRotation",imgRotation) (0)
45 degrees
60 degrees
consultation
Introducing the cv2.getRotationMatrix2D blog!
Blog Introduction
numpy.rot90
Rotate any angle
to this article on the use of python opencv rotate images to this article, more related python opencv rotate images content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!