📜  Python OpenCV – cv2.rotate() 方法

📅  最后修改于: 2022-05-13 01:55:47.098000             🧑  作者: Mango

Python OpenCV – cv2.rotate() 方法


OpenCV-Python是一个Python绑定库,旨在解决计算机视觉问题。 cv2.rotate()方法用于将二维数组旋转 90 度的倍数。函数cv::rotate 以三种不同的方式旋转数组。

用于以下所有示例的图像:

示例 #1:顺时针旋转 90 度

# Python program to explain cv2.rotate() method
  
# importing cv2
import cv2
  
# path
path = r'C:\Users\user\Desktop\geeks14.png'
  
# Reading an image in default mode
src = cv2.imread(path)
  
# Window name in which image is displayed
window_name = 'Image'
  
# Using cv2.rotate() method
# Using cv2.ROTATE_90_CLOCKWISE rotate
# by 90 degrees clockwise
image = cv2.rotate(src, cv2.cv2.ROTATE_90_CLOCKWISE)
  
# Displaying the image
cv2.imshow(window_name, image)
cv2.waitKey(0)

输出:

示例 #2:顺时针旋转 180 度

# Python program to explain cv2.rotate() method
  
# importing cv2
import cv2
  
# path
path = r'C:\Users\user\Desktop\geeks14.png'
  
# Reading an image in default mode
src = cv2.imread(path)
  
# Window name in which image is displayed
window_name = 'Image'
  
# Using cv2.rotate() method
# Using cv2.ROTATE_180 rotate by 
# 180 degrees clockwise
image = cv2.rotate(src, cv2.ROTATE_180)
  
# Displaying the image
cv2.imshow(window_name, image)
cv2.waitKey(0)

输出:

示例 #3:顺时针旋转 270 度

# Python program to explain cv2.rotate() method
  
# importing cv2
import cv2
  
# path
path = r'C:\Users\user\Desktop\geeks14.png'
  
# Reading an image in default mode
src = cv2.imread(path)
  
# Window name in which image is displayed
window_name = 'Image'
  
# Using cv2.rotate() method
# Using cv2.ROTATE_90_COUNTERCLOCKWISE 
# rotate by 270 degrees clockwise
image = cv2.rotate(src, cv2.ROTATE_90_COUNTERCLOCKWISE)
  
# Displaying the image
cv2.imshow(window_name, image)
cv2.waitKey(0)

输出: