[英]Stereo rectification with OpenCV 4.2.0
我已经用我自己的代码进行了纠正。 现在我试图让cv2.stereoRectify
工作。
假设我有以下代码:
import numpy as np
import cv2
img1 = cv2.imread(IMG_LEFT) # Load image left
img2 = cv2.imread(IMG_RIGHT) # Load image right
A1 = np.array(A1) # Left camera matrix intrinsic
A2 = np.array(A2) # Right camera matrix intrinsic
RT1 = np.array(RT1) # Left camera extrinsic (3x4)
RT2 = np.array(RT2) # Right camera extrinsic (3x4)
# Original projection matrices
Po1 = A1.dot( RT1 )
Po2 = A2.dot( RT2 )
# Camera centers (world coord.)
C1 = -np.linalg.inv(Po1[:,:3]).dot(Po1[:,3])
C2 = -np.linalg.inv(Po2[:,:3]).dot(Po2[:,3])
# Transformations
T1to2 = C2 - C1 # Translation from first to second camera
R1to2 = RT2[:,:3].dot(np.linalg.inv(RT1[:,:3])) # Rotation from first to second camera (3x3)
然后,我想找到整流变换(3x3)。 按照我正在尝试的OpenCV 文档:
Rectify1, Rectify2, Pn1, Pn2, _, _, _ = cv2.stereoRectify(A1, np.zeros((1,5)), A2, np.zeros((1,5)), (img1.shape[1], img1.shape[0]), R1to2, T1to2, alpha=-1 )
mapL1, mapL2 = cv2.initUndistortRectifyMap(A1, np.zeros((1,5)), Rectify1, Pn1, (img1.shape[1], img1.shape[0]), cv2.CV_32FC1)
mapR1, mapR2 = cv2.initUndistortRectifyMap(A2, np.zeros((1,5)), Rectify2, Pn2, (img2.shape[1], img2.shape[0]), cv2.CV_32FC1)
img1_rect = cv2.remap(img1, mapL1, mapL2, cv2.INTER_LINEAR)
img2_rect = cv2.remap(img2, mapR1, mapR2, cv2.INTER_LINEAR)
无论如何,我得到完全搞砸的图像,肯定没有纠正。 我究竟做错了什么? 我想这与旋转/平移有关,但我无法弄清楚。
另外,OpenCV 对此是否有点过于复杂? 无论如何,这应该是一个简单的操作。
非常感谢。
编辑:您可能会注意到我将失真参数设置为零。 那是因为我使用的是没有镜头失真的计算机生成的立体图像。
深入研究 OpenCV 文档,我发现了stereoRectify()
似乎不起作用的原因。
大多数研究论文经常提到单应变换应用于图像。 相反,OpenCV 正在计算对象空间中的旋转,正如cv2.initUndistortrectifyMap()
文档中(害羞地)解释的cv2.initUndistortrectifyMap()
(请参阅此处)。
所以在调用之后:
R1, R2, Pn1, Pn2, _, _, _ = cv2.stereoRectify(A1, np.zeros((1,5)), A2, np.zeros((1,5)), (img1.shape[1], img1.shape[0]), R1to2, T1to2, alpha=-1 )
要获得我使用的整流变换:
Rectify1 = R1.dot(np.linalg.inv(A1))
Rectify2 = R2.dot(np.linalg.inv(A2))
其中R1
和R2
是 OpenCV 的转换输出, A1
和A2
是 3x3 相机矩阵(内在)。
它似乎工作得很好。 如果您有更好的想法,请发表评论。
希望这对任何人都有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.