我正试图纠正python中的图像。我有一个单应H(来自绕x、y和z轴旋转的旋转矩阵),如下所示: [[9.95671447e-01 7.83610423e-02 7.47993630e+02] [-7.69292630e-02 9.96586377e-01-4.48354859e+02] [-3.48494755e-06 1.73615469e-06 9.98300856e-01]]
我以为我可以用cv2.perspectiveTransform()来完成这个任务,但是我无法让它工作。这是我使用的代码:
# warp image
img_cv2 = cv2.imread('surf.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
# strange output but it does something:
dst = cv2.perspectiveTransform(img_cv2,H)
但我得到了以下错误:
Traceback (most recent call last):
File "C:\directory structure\python_files\Rectification\rectify.py", line 82, in <module>
dst = cv2.perspectiveTransform(img_cv2,H)
error: C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\matmul.cpp:1916: error: (-215) scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)`</pre>
有人知道怎么回事吗?
我认为你想要的是
cv2.warpPerspective
(参见文档(link)),而不是cv2.perspectiveTransform
。它说图像应该是
np.float32
或np.float64
。所以首先通过
img_cv2 = np.float32(img_cv2)
转换图像。然后应用
cv2.perspectiveTransform()
和cv2.warpPerspective()
查看演示
源图像和目标图像必须是浮点数据。
参考:http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#cv2.perspectiveTransform
因此,将8U映像转换为适当的数据类型。
相关问题 更多 >
编程相关推荐