python-3.x - 为什么使用 cv2.getRotationMatrix2D 和 cv2.warpAffine 旋转图像会返回零矩阵
问题描述
我是 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))
此代码返回一个零矩阵。谁能帮我调试一下并告诉我为什么会这样?
解决方案
该代码似乎还可以并且对我有用,但是您没有显示您的图像...
旋转矩阵的输出为:
[[ 0.98480775 0.17364818 -0.25563765]
[-0.17364818 0.98480775 99.97181918]]
在opencv中,您必须在最后处理这些waitKey尝试,它会暂停cv2直到按下escape..
cv2.imshow("image", image)
while True:
keyboard = cv2.waitKey(2320)
if keyboard == 27:
break
cv2.destroyAllWindows()
推荐阅读
- automated-tests - 如何修复 CircleCI 中长时间运行的 testcafe 测试
- vimeo - 如何在单个 Vimeo API 调用中获取多个视频的视频详细信息?
- c# - C# .NET 打印到默认打印机
- django - Django 模型多对多和桥接表
- r - 在列的子集上使用 mutate rowwise
- powershell - 如何显示 nuget 包的源存储库?
- xml - 使用 Access 导入 XML 文件,更新然后输出相同的 XML 文件
- youtube-api - YouTube 数据 API 错误
- javascript - 在使用 watermarkjs 上传之前向图像添加水印
- zend-framework3 - 如何使用 Composer 正确安装 Zend Framework 3?