current position:Home>Python+OpenCV calculates the depth map of the image scene (principle and code implementation)

Python+OpenCV calculates the depth map of the image scene (principle and code implementation)

2022-09-09 04:09:47Qiao Qing

问题描述

目录hw3Below are two images corresponding to stereo visionview1.png和view5.png,图像来源于http://vision.middlebury.edu/stereo/data/.Please calculate the depth of the scene corresponding to these two images.

原理介绍

由于时间原因,通过调用OpenCVImplementation of functions related to computing depth maps for stereo images in ,Don't make your own wheels.

双目立体匹配一直是双目视觉的研究热点,双目相机拍摄同一场景的左、右两幅视点图像,运用立体匹配匹配算法Get a disparity map,进而获取深度图,This is exactly what this experiment is about.从原理上,If we have two images of the same scene,We can get depth information from it in an intuitive way,如下图所示.
在这里插入图片描述上图包含等效三角形,and has the following equation:

在这里插入图片描述
其中disparity是视差,x与x’is the distance between a point in the image plane that corresponds to a point in the 3D scene and its camera center.B是两个摄像机之间的距离(已知),f是摄像机的焦距(已知).This equation shows that,The depth of a point in the scene is inversely proportional to the distance difference between the corresponding image plane point and its camera center.This equation is what derives the pixel depth.

在OpenCV中,The specific implementation involves the following methods:

StereoBM_create()

作用

创建StereoBM(block match)对象.

原型

cv.StereoBM_create( [, numDisparities[, blockSize]] ) -> retval

参数

• numDisparities:disparity的搜索范围. 对于每个像素,The algorithm will find from 0(Default minimum parallax)到numDisparitiesbest parallax.之后,The search range can be changed by changing the minimum parallax,见StereoSGBM_create().
• blockSize:Linear size of the block.Size should be odd(Because the block is at the center of the current pixel).Larger block size means smoother,But less accurate disparity maps.A smaller block size will give a more detailed disparity map,But the algorithm has a higher chance of finding the wrong correspondence.

返回值

• retval:StereoBM(block match)对象.可以通过该对象的compute()方法.

StereoBM.compute()

作用

计算一对stereoParallax of the image.

原型

cv.StereoMatcher.compute(left, right[, disparity]) -> disparity

参数

• left:8bit单通道图像,左图.
• right:8bit单通道图像,右图.

输出

• disparity:视差图,与输入图像的大小相同.StereoBM或StereoSGBM计算16Fixed-point disparity map(where each disparity value has 4个小数位),while other algorithms output32Bit float disparity map.

StereoSGBM_create()

见该文章:
python cv.StereoSGBM_createDetailed explanation of method parameters and usage

OpenCV代码

import cv2

def estimate_depth(left_path, right_path, show=True):
    # Read the left and right images
    img_left = cv2.imread(left_path, 0)
    img_right = cv2.imread(right_path, 0)
    height, width = img_left.shape[:2]

    # 初始化stereo block match对象
    stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)

    # Get a disparity map
    disparity = stereo.compute(img_left, img_right)

    if show:
        # Normalize the disparity map
        # min_val = disparity.min()
        # max_val = disparity.max()
        # disparity = np.uint8(6400 * (disparity - min_val) / (max_val - min_val))

        # 显示视差图
        cv2.imshow('disparity image', disparity)
        cv2.imwrite('disparity.jpg', disparity)
        cv2.waitKey(0)


estimate_depth('images/view1.png', 'images/view5.png')

结果

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

copyright notice
author[Qiao Qing],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/252/202209090355583588.html

Random recommended