使用opencv计算深度视差图

2024-05-09

我无法使用 opencv 从视差图计算深度。我知道两个立体图像中的距离是用以下公式计算的z = (baseline * focal) / (disparity * p)但我不知道如何使用地图计算视差。我使用的代码如下,为我提供了两个图像的视差图。

import numpy as np
import cv2

# Load the left and right images in gray scale
imgLeft = cv2.imread('logga.png', 0)
imgRight = cv2.imread('logga1.png', 0)

# Initialize the stereo block matching object 
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=5)

# Compute the disparity image
disparity = stereo.compute(imgLeft, imgRight)

# Normalize the image for representation
min = disparity.min()
max = disparity.max()
disparity = np.uint8(6400 * (disparity - min) / (max - min))



# Display the result
cv2.imshow('disparittet', np.hstack((imgLeft, imgRight, disparity)))
cv2.waitKey(0)
cv2.destroyAllWindows()

为了根据视差计算深度,OpenCV 有以下函数重新投影图像至 3d https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#reprojectimageto3d.

您需要来自立体校正的视差到深度矩阵 (Q)(或者您可以按照链接中给出的方式创建它)。您可以了解有关 Q 矩阵的更多信息here https://answers.opencv.org/question/187734/derivation-for-perspective-transformation-matrix-q/?answer=187997#post-id-187997.

得到Q矩阵后,只需将视差图重新投影为3D即可

depth = cv2.reprojectImageTo3D(disparity, Q)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用opencv计算深度视差图 的相关文章

随机推荐