opencv旋转图像
时间: 2023-08-21 20:03:07 浏览: 37
要使用OpenCV旋转图像,可以按照以下步骤进行操作:
1. 使用`cv2.getRotationMatrix2D`函数获取旋转矩阵。该函数需要传入旋转中心点(可以是原图像的中心点),旋转角度(以度为单位)和缩放因子。例如,`matRotation = cv2.getRotationMatrix2D((width_ori/2, height_ori/2), degree, 1)`\[1\]。
2. 使用`cv2.warpAffine`函数将旋转矩阵应用于图像,得到旋转后的图像。为了避免图像被切割,可以设置变换后的图像大小为`(width_1, height_1)`。例如,`imgRotation = cv2.warpAffine(img, matRotation, (width_1, height_1), borderValue=(255, 255, 255))`\[2\]。
完整的代码如下所示:
```
height_ori, width_ori = img.shape\[:2\]
height_1 = int(width_ori * fabs(sin(radians(degree))) + height_ori * fabs(cos(radians(degree))))
width_1 = int(height_ori * fabs(sin(radians(degree))) + width_ori * fabs(cos(radians(degree))))
matRotation = cv2.getRotationMatrix2D((width_ori/2, height_ori/2), degree, 1)
matRotation\[0, 2\] += (width_1 - width_ori) / 2
matRotation\[1, 2\] += (height_1 - height_ori) / 2
imgRotation = cv2.warpAffine(img, matRotation, (width_1, height_1), borderValue=(255, 255, 255))
```
请注意,上述代码中的`img`是原始图像,`degree`是旋转角度。
#### 引用[.reference_title]
- *1* *2* *3* [opencv旋转图像](https://blog.csdn.net/u010122972/article/details/78345447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]