[英]OpenCV perspective transform in python
I'm trying to rectify an image in python. 我正在尝试纠正python中的图像。 I have a Homography H (from a rotation matrix that rotates around the x, y and z axis) that looks like this for example: [[ 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]] 我有一个Homography H(来自围绕x,y和z轴旋转的旋转矩阵),例如:[[9.95671447e-01 7.83610423e-02 7.47993630e + 02] [-7.69292630e-02 9.96586377 e-01 -4.48354859e + 02] [-3.48494755e-06 1.73615469e-06 9.98300856e-01]]
I thought I could do this woth cv2.perspectiveTransform() but I cant't get it to work. 我以为我可以做到这一点cv2.perspectiveTransform(),但我不能让它工作。 This is the code I use: 这是我使用的代码:
# 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)
But I get the following error: 但是我收到以下错误:
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>
Can anybody see what is going wrong? 有人能看出出了什么问题吗?
The source and destination image must be floating point data. 源映像和目标映像必须是浮点数据。
cv2.perspectiveTransform(src, m[, dst]) → dst cv2.perspectiveTransform(src,m [,dst])→dst
Parameters : 参数 :
- src – input two-channel or three-channel floating-point array; src - 输入双通道或三通道浮点数组; each element is a 2D/3D vector to be transformed. 每个元素都是要转换的2D / 3D矢量。
- dst – output array of the same size and type as src. dst - 输出与src大小和类型相同的数组。
- m – 3x3 or 4x4 floating-point transformation matrix. m - 3x3或4x4浮点变换矩阵。
Refer: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#cv2.perspectiveTransform 参考: http : //docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#cv2.perspectiveTransform
So convert the 8U image to the appropriate datatype. 因此,将8U映像转换为适当的数据类型。
我想你想要的是cv2.warpPerspective
(参见文档(链接) ),而不是cv2.perspectiveTransform
。
It says the image should be np.float32
or np.float64
. 它说图像应该是np.float32
或np.float64
。
So convert the image first by img_cv2 = np.float32(img_cv2)
. 因此首先通过img_cv2 = np.float32(img_cv2)
转换图像。
Then apply the cv2.perspectiveTransform()
and cv2.warpPerspective()
然后应用cv2.perspectiveTransform()
和cv2.warpPerspective()
Check this tutorial for demo 查看本教程的演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.