opencv旋转图片45度,30度
时间: 2023-06-30 16:26:18 浏览: 230
您可以使用 OpenCV 中的 `warpAffine` 函数来旋转图像。这个函数使用一个旋转矩阵来对图像进行变换。您可以使用 `getRotationMatrix2D` 函数来获取旋转矩阵。
以下是旋转图像 45 度和 30 度的示例代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 获取图像的高度和宽度
(h, w) = img.shape[:2]
# 定义旋转中心
center = (w // 2, h // 2)
# 定义旋转角度
angle_45 = 45
angle_30 = 30
# 定义缩放比例
scale = 1.0
# 获取旋转矩阵
M_45 = cv2.getRotationMatrix2D(center, angle_45, scale)
M_30 = cv2.getRotationMatrix2D(center, angle_30, scale)
# 对图像进行旋转
img_45 = cv2.warpAffine(img, M_45, (w, h))
img_30 = cv2.warpAffine(img, M_30, (w, h))
# 显示旋转后的图像
cv2.imshow('45 Degree Rotation', img_45)
cv2.imshow('30 Degree Rotation', img_30)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,我们首先读取了一张图像,然后使用 `getRotationMatrix2D` 函数分别获取旋转 45 度和 30 度的旋转矩阵。接下来,我们使用 `warpAffine` 函数对图像进行旋转,并使用 `imshow` 函数显示旋转后的图像。
阅读全文
相关推荐

















