【发布时间】:2020-06-14 08:54:28
【问题描述】:
我是 cv2 库的初学者。我在 python 3.7 中使用 cv2 库来旋转图像。这是我的代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("messi.jpg")
(h, w) = img.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), 10, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
image = cv2.warpAffine(img, M, (nW, nH))
此代码返回一个零矩阵。谁能帮我调试一下并告诉我为什么会这样?
【问题讨论】:
标签: python-3.x opencv image-rotation