【OpenCV】06.旋转图片

Posted on Sep 12, 2022

可以将图片旋转多个角度(90,180,270 以及 360)。语法如下:

cv2.getRotationMatrix2D(center, angle, scale rotated = cv2.warpAfifne(img,M,(w,h))  

参数

  • center: 图像的中心。
  • angle: 图片逆时针旋转角度。
  • rotated: ndarray 保存旋转后图片信息。
  • scale: 1.0 表明图像大小不变。根据给定数值缩放图片大小。

示例一:

import cv2  
# read image as greyscale  
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg')  
# get image height, width  
(h, w) = img.shape[:2]  
# calculate the center of the image  
center = (w / 2, h / 2)  
  
angle90 = 90  
angle180 = 180  
angle270 = 270  
  
scale = 1.0  
  
# Perform the counterclockwise rotation holding at the center  
# 90 degrees  
M = cv2.getRotationMatrix2D(center, angle90, scale)  
rotated90 = cv2.warpAffine(img, M, (h, w))  
  
# 180 degrees  
M = cv2.getRotationMatrix2D(center, angle180, scale)  
rotated180 = cv2.warpAffine(img, M, (w, h))  
  
# 270 degrees  
M = cv2.getRotationMatrix2D(center, angle270, scale)  
rotated270 = cv2.warpAffine(img, M, (h, w))  
  
cv2.imshow('Original Image', img)  
cv2.waitKey(0)  # waits until a key is pressed  
cv2.destroyAllWindows()  # destroys the window showing image  
  
cv2.imshow('Image rotated by 90 degrees', rotated90)  
cv2.waitKey(0)  # waits until a key is pressed  
cv2.destroyAllWindows()  # destroys the window showing imag  
  
cv2.imshow('Image rotated by 180 degrees', rotated180)  
cv2.waitKey(0)  # waits until a key is pressed  
cv2.destroyAllWindows()  # destroys the window showing image  
  
cv2.imshow('Image rotated by 270 degrees', rotated270)  
cv2.waitKey(0)  # waits until a key is pressed  
cv2.destroyAllWindows()  # destroys the window showing image  

输出:

opencv-image-rotation