简体   繁体   English

Python - 从旋转角度对OpenCV进行透视变换

[英]Python - Perspective transform for OpenCV from a rotation angle

I'm working on depth map with OpenCV . 我正在depth map with OpenCV开发depth map with OpenCV I can obtain it but it is reconstructed from the left camera origin and there is a little tilt of this latter and as you can see on the figure, the depth is "shifted" (the depth should be close and no horizontal gradient): 我可以获得它,但它是从左相机原点重建的,后者有一点倾斜,如图所示,深度“移位”(深度应该接近,没有水平渐变):

在此输入图像描述

I would like to express it as with a zero angle, i try with the warp perspective function as you can see below but i obtain a null field... 我想以零角度表达它,我尝试使用warp透视函数,如下所示,但我获得了一个空字段...

P = np.dot(cam,np.dot(Transl,np.dot(Rot,A1)))

dst = cv2.warpPerspective(depth, P, (2048, 2048))

with : 用:

#Projection 2D -> 3D matrix
A1 = np.zeros((4,3))
A1[0,0] = 1 
A1[0,2] = -1024
A1[1,1] = 1
A1[1,2] = -1024
A1[3,2] = 1

#Rotation matrice around the Y axis
theta = np.deg2rad(5) 
Rot = np.zeros((4,4))
Rot[0,0] = np.cos(theta)
Rot[0,2] = -np.sin(theta)
Rot[1,1] = 1
Rot[2,0] = np.sin(theta)
Rot[2,2] = np.cos(theta)
Rot[3,3] = 1

#Translation matrix on the X axis 
dist = 0
Transl = np.zeros((4,4))
Transl[0,0] = 1 
Transl[0,2] = dist
Transl[1,1] = 1
Transl[2,2] = 1
Transl[3,3] = 1

#Camera Intrisecs matrix 3D -> 2D
cam = np.concatenate((C1,np.zeros((3,1))),axis=1)
cam[2,2] = 1
P = np.dot(cam,np.dot(Transl,np.dot(Rot,A1)))

dst = cv2.warpPerspective(Z0_0, P, (2048*3, 2048*3))

EDIT LATER : 稍后编辑:

You can download the 32MB field dataset here: https://filex.ec-lille.fr/get?k=cCBoyoV4tbmkzSV5bi6 . 您可以在此处下载32MB字段数据集: https//filex.ec-lille.fr/get? k = cCBoyoV4tbmkzSV5bi6 Then, load and view the image with: 然后,加载并查看图像:

from matplotlib import pyplot as plt
import numpy as np

img = np.load('testZ0.npy')
plt.imshow(img)
plt.show()

I have got a rough solution in place. 我有一个粗略的解决方案。 You can modify it later. 您可以稍后修改它。

I used the mouse handling operations available in OpenCV to crop the region of interest in the given heatmap. 我使用OpenCV中可用的鼠标处理操作来裁剪给定热图中的感兴趣区域。

(Did I just say I used a mouse to crop the region?) Yes, I did. (我只是说我用鼠标来裁剪区域吗?)是的,我做到了。 To learn more about mouse functions in OpenCV SEE THIS . 要了解有关OpenCV中鼠标功能的更多信息,请参阅此内容 Besides, there are many other SO questions that can help you in this regard.:) 此外,还有许多其他SO问题可以帮助你在这方面。:)

Using those functions I was able to obtain the following: 使用这些功能,我能够获得以下内容:

在此输入图像描述

Now to your question of removing the tilt. 现在回答您关于移除倾斜的问题。 I used the homography principal by taking the corner points of the image above and using it on a 'white' image of a definite size. 我使用了单应性原则,通过拍摄上面图像的角点并在一个确定大小的“白色”图像上使用它。 I used the cv2.findHomography() function for this. cv2.findHomography()使用了cv2.findHomography()函数。

Now using the cv2.warpPerspective() function in OpenCV, I was able to obtain the following: 现在在OpenCV中使用cv2.warpPerspective()函数,我能够获得以下内容:

在此输入图像描述

Now you can the required scale to this image as you wanted. 现在,您可以根据需要缩放到此图像。

CODE: 码:

I have also attached some snippets of code for your perusal: 我还附上了一些代码片段供您阅读:

#First I created an image of white color of a definite size
back = np.ones((435, 379, 3)) # size
back[:] = (255, 255, 255)     # white color

Next I obtained the corner points pts_src on the tilted image below : 接下来,我在下面的倾斜图像上获得了角点pts_src

pts_src = np.array([[25.0, 2.0],[403.0,22.0],[375.0,436.0],[6.0,433.0]])

I wanted the points above to be mapped to the points 'pts_dst' given below : 我希望将上面的点映射到下面给出的点'pts_dst':

pts_dst = np.array([[2.0, 2.0], [379.0, 2.0], [379.0, 435.0],[2.0, 435.0]])

Now I used the principal of homography: 现在我用了单应性的原理:

h, status = cv2.findHomography(pts_src, pts_dst)

Finally I mapped the original image to the white image using perspective transform. 最后,我使用透视变换将原始图像映射到白色图像。

fin = cv2.warpPerspective(img, h, (back.shape[1],back.shape[0]))
# img -> original tilted image.
# back -> image of white color.

Hope this helps! 希望这可以帮助! I also got to learn a great deal from this question. 我也从这个问题中学到了很多东西。

Note: The points fed to the 'cv2.findHomography()' must be in float . 注意:输入'cv2.findHomography()'的点必须是float For more info on Homography , visit THIS PAGE 有关Homography的更多信息,请访问此页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM