cv2 图像逆时针旋转180度
时间: 2023-02-08 09:34:00 浏览: 133
使用 OpenCV 的 Python 库时,可以使用 cv2.rotate() 函数来旋转图像。具体地,可以使用以下代码将图像逆时针旋转 180 度:
```python
import cv2
# 读入图像
img = cv2.imread("image.jpg")
# 计算旋转矩阵
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 180, 1)
# 旋转图像
img = cv2.warpAffine(img, M, (cols, rows))
# 显示旋转后的图像
cv2.imshow("rotated image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
注意:
- cv2.imread(path) is to read the image from path.
- cv2.imshow(winname,mat) is to create window, show image in window
- cv2.waitKey(time) is to wait for key press for time.
- cv2.destroyAllWindows() is to close all windows when done.
还有一种简单的方法就是直接用np.flip来翻转坐标轴,像这样:
```python
import cv2
import numpy as np
img = cv2.imread("image.jpg")
img_rotate = np.flip(img, -1)
cv2.imshow("rotated image", img_rotate)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文