【问题标题】:Stereo rectification with OpenCV 4.2.0使用 OpenCV 4.2.0 进行立体声整流
【发布时间】:2020-03-27 15:23:18
【问题描述】:

我已经用自己的代码进行了整改。现在我正在尝试让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 documentation我正在尝试:

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 computer-vision stereo-3d


【解决方案1】:

深入研究 OpenCV 文档,我发现了 stereoRectify() 似乎不起作用的原因。

大多数研究论文经常提到单应变换以应用于图像。 相反,OpenCV 正在计算对象空间中的旋转,正如cv2.initUndistortrectifyMap() 文档中(害羞地)解释的那样(参见here)。

所以调用后:

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

R1R2 是 OpenCV 的转换输出,A1A2 是 3x3 相机矩阵(内部)。

它似乎工作得很好。如果您有更好的想法,请发表评论。

希望这对任何人都有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 2011-08-19
    • 2015-11-11
    • 1970-01-01
    • 2015-09-09
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多