有没有Python代码来获取立体图像的完整深度图?

2024-06-17 08:21:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个运行stereo SBGM的Python代码,我从以下站点复制了该代码:

http://timosam.com/python_opencv_depthimage

但我的输出不是一个完整的深度图,而立体图像是完美的

import numpy as np      
from matplotlib import pyplot as plt
import cv2    
import time as t    
print('loading images...')    
imgL = cv2.imread('image_left.png')  # downscale images for faster processing

imgR = cv2.imread('image_right.png')

cv2.imshow('image_left.png',imgL)

cv2.imshow('image_right.png',imgR)

dawn = t.time()

SGBM参数-----------------

窗口大小=3#wsize默认值3;5; 7用于SGBM缩小图像;15用于SGBM全尺寸图像(1300px及以上);5个很好用

left_matcher = cv2.StereoSGBM_create(
    minDisparity=0,

    numDisparities=160,             # max_disp has to be dividable by 16 f. E. HH 192, 256

    blockSize=7,

    P1=8 * 3 * window_size ** 2,    # wsize default 3; 5; 7 for SGBM reduced size image; 15 for SGBM full size image (1300px and above); 5 Works nicely

    P2=32 * 3 * window_size ** 2,

    disp12MaxDiff=1,

    uniquenessRatio=15,

    speckleWindowSize=0,

    speckleRange=2,

    preFilterCap=63,

    mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY

)

right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)


# FILTER Parameters(weighted least square)
lmbda = 80000

sigma = 1.2

visual_multiplier = 1.0


wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=left_matcher)

wls_filter.setLambda(lmbda)

wls_filter.setSigmaColor(sigma)


print('computing disparity...')

displ = left_matcher.compute(imgL, imgR)  # .astype(np.float32)/16

dispr = right_matcher.compute(imgR, imgL)  # .astype(np.float32)/16

displ = np.int16(displ)

dispr = np.int16(dispr)

filteredImg = wls_filter.filter(displ, imgL, None, dispr)  # important to put "imgL" here!!!


filteredImg = cv2.normalize(src=filteredImg, dst=filteredImg, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX);

filteredImg = np.uint8(filteredImg)

dusk = t.time()

print('\nTotal execution time = {:.2f} s'.format(dusk - dawn))

cv2.imshow('Disparity Map', filteredImg)

我希望这段代码的输出是完整的深度图像,但不完整

stereo images and depth map result


Tags: 图像imageimportrighttimepngnpmatcher