opencv图片缩放和旋转
时间: 2024-10-04 11:00:51 浏览: 41
OpenCV是一个强大的计算机视觉库,提供了一系列操作图像的功能,包括缩放和旋转。以下是基本步骤:
1. 图片缩放:你可以通过`cv2.resize()`函数来缩放图像。这个函数需要两个参数,一个是输入图像,另一个是新尺寸。例如:
```python
import cv2
resized_image = cv2.resize(input_image, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
```
`interpolation`参数可以选择不同的插值方法,如`INTER_NEAREST`、`INTER_LINEAR`等。
2. 图片旋转:可以使用`getRotationMatrix2D()`和`warpAffine()`函数实现。首先计算旋转矩阵,然后应用该矩阵到原始图像上:
```python
center = (img.shape[1] // 2, img.shape[0] // 2) # 获取中心点
angle = degrees_of_rotation # 角度值
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0) # 保持原图大小不变
rotated_img = cv2.warpAffine(img, rotation_matrix, img.shape)
```
这里的`degrees_of_rotation`是你想要旋转的角度数,单位是度。
阅读全文
相关推荐



















