我正在尝试使用cv2在python中旋转图像。到目前为止,我只是从网上复制了示例,但无法使它们按预期工作。我的代码如下:
import cv2
image = cv2.imread('test.jpg')
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, 270, 1.0)
rotated = cv2.warpAffine(image, M, (h, w))
cv2.imshow("rotated", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
编辑和答案:
感谢@ jeru-luke向我指出了正确的方向。下面的代码由Adrian Rosebrock从this post修改而来。
import cv2
import imutils
image = cv2.imread('test.jpg')
rotated = imutils.rotate_bound(image, 90)
cv2.imshow("Rotated (Correct)", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()