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

标签 python opencv stereo-3d perspectivecamera

我正在使用 OpenCV 制作深度图。我可以获得它,但它是从左相机原点重建的,后者有一点倾斜,正如你在图中看到的那样,深度“偏移”了(深度应该接近并且没有水平梯度):

enter image description here

我想用零角度来表达它,我尝试使用扭曲透视功能,如下所示,但我得到了一个空场...

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

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

与:

#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))

稍后编辑:

您可以在此处下载 32MB 的现场数据集:https://filex.ec-lille.fr/get?k=cCBoyoV4tbmkzSV5bi6 .然后,加载并查看图像:

from matplotlib import pyplot as plt
import numpy as np

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

最佳答案

我已经有了一个粗略的解决方案。您可以稍后修改它。

我使用 OpenCV 中可用的鼠标处理操作来裁剪给定热图中的感兴趣区域。

(我刚才是不是说我用鼠标裁剪了该区域?)是的,我说了。要了解有关 OpenCV 中鼠标功能的更多信息 SEE THIS .此外,还有许多其他 SO 问题可以在这方面为您提供帮助。:)

使用这些函数我能够获得以下内容:

enter image description here

现在回答您消除倾斜的问题。我通过获取上面图像的角点并将其用于确定大小的“白色”图像来使用单应性原理。为此,我使用了 cv2.findHomography() 函数。

现在使用 OpenCV 中的 cv2.warpPerspective() 函数,我能够获得以下内容:

enter image description here

现在您可以根据需要对该图像进行所需的缩放。

代码:

我还附上了一些代码片段供您阅读:

#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

接下来我在下面的倾斜图像上获得角点 pts_src :

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

我希望将上面的点映射到下面给出的点“pts_dst”:

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

现在我用到了单应性原理:

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

最后,我使用透视变换将原始图像映射到白色图像。

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

希望对您有所帮助!我也从这个问题中学到了很多东西。

注意:提供给“cv2.findHomography()”的点必须是float。 有关 Homography 的更多信息,请访问 THIS PAGE

关于Python - 从旋转角度对 OpenCV 进行透视变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41428162/

相关文章:

opencv - 旋转矩阵 R1 w.r.t 到另一个矩阵,比如 R2

image-processing - 立体相机的基线是什么?

python - 过滤器对象没有属性 pop

python - 如何为使用 matplotlib 创建的 PDF 文档创建标题页

c++ - OpenCV C++ 视频捕获似乎不起作用

python - 使用 OpenCV 和 Python 从图像中识别和裁剪文本的问题

python - PyInstaller 生成的可执行文件在 MacOS X 上的代码签名不被接受

python - 使用 OpenCV GStreamer 读取 HLS 流时 CPU 使用率过高

linux - 在树莓派上运行 OpenCV

c++ - stereoCalibrate() 改变焦距,即使它不应该改变