如何使用纹理改善StereoSGBM结果

时间:2019-05-19 21:39:19

标签: python opencv computer-vision

因此,我正在为高中高级项目制作6Dof立体声360。我的视差结果不错,但我想知道是否有办法使它们更好,尤其是如何处理纹理。视差贴图应该随着点越来越远而逐渐消失,但是,因为StereoSGBM处理纹理的效果不好,所以远点之间的距离不合理。另外,天空应显示为黑色,但光线非常明亮。 视差图: https://imgur.com/MqK4gMU

我正在使用StereoSGBM来获取2个理光Theta SC相机的视差图。我尝试调整视差设置,并与输入的图像进行亮度和对比度的播放。我还尝试过更改StereoSGBM模式(HH,SGBM,SGBM_3WAY),翻转输入图像并使用StereoBM而不是SGBM。我没有尝试过校准相机(除了移动图像,以便相机指向完全相同的方向),因为我发现如果相机校准成为问题,我会得到更差的结果。除了StereoSGBM之外,还有其他我可以使用的视差函数吗?我是否应该尝试将机器学习与Google Cloud结合使用以创建更好的视差模型?我可以使用NYU深度数据集(https://cs.nyu.edu/~silberman/datasets/nyu_depth_v2.html)来训练模型。有人对提高我的成绩有任何想法吗?

import numpy as np
import cv2 as cv
from sklearn.preprocessing import normalize
from PIL import Image, ImageEnhance, ImageOps


def func_disparity(window_size, minDisparity2, numDisparities2, blockSize2, 
    disp12MaxDiff2, uniquenessRatio2, speckleWindowSize2, speckleRange2, 
    preFilterCap2, brightness,
    contrast, event=None):

    imgR = Image.open(FILE_NAME)
    imgL = Image.open(FILEN_NAME)
    print(imgL.size)

    imgL = ImageOps.expand(imgL, border=50)
    imgR = ImageOps.expand(imgR, border=50)    

    contrastL = ImageEnhance.Contrast(imgL)
    contrastR = ImageEnhance.Contrast(imgR)
    imgL = contrastL.enhance(contrast)
    imgR = contrastR.enhance(contrast)

    brightnessL = ImageEnhance.Brightness(imgL)
    brightnessR = ImageEnhance.Brightness(imgR)
    imgL = brightnessL.enhance(brightness)
    imgR = brightnessR.enhance(brightness)

    imgL = imgL.convert('L')
    imgL = np.array(imgL)
    imgR = imgR.convert('L')
    imgR = np.array(imgR)


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

    left_matcher = cv.StereoSGBM_create(
        minDisparity=minDisparity2,
        numDisparities=numDisparities2,             # max_disp has to be 
        dividable by 16 f. E. HH 192, 256
        blockSize= blockSize2,
        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=disp12MaxDiff2,
        uniquenessRatio=uniquenessRatio2,
        speckleWindowSize=speckleWindowSize2,
        speckleRange=speckleRange2,
        preFilterCap= preFilterCap2,
        mode=cv.STEREO_SGBM_MODE_SGBM_3WAY
        )

    right_matcher = cv.ximgproc.createRightMatcher(left_matcher)

    # FILTER Parameters
    lmbda = 80000
    sigma = 1.2
    visual_multiplier = 1.0

    wls_filter = 
    cv.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 = cv.normalize(src=filteredImg, dst=filteredImg, beta=0, 
    alpha=255, norm_type=cv.NORM_MINMAX);
    filteredImg = np.uint8(filteredImg)

    height, width = filteredImg.shape
    filteredImg = np.delete(filteredImg, np.s_[0:50], axis=0)
    filteredImg = np.delete(filteredImg, np.s_[height-100:height-50], axis=0)
    filteredImg = np.delete(filteredImg, np.s_[0:50], axis=1)
    filteredImg = np.delete(filteredImg, np.s_[width-100:width-50], axis=1)
    print(filteredImg.shape)

    return(filteredImg)
    #print(filteredImg)
    #file = Image.fromarray(filteredImg)
    #file.save("disparitymap.jpg")
    print("Done")

window_size = 3
minDisparity2 = 15
numDisparities2=16      #160max_disp has to be dividable by 16 f. E. HH 192, 
blockSize2=20   #Maybe 20 is optimal
disp12MaxDiff2=1
uniquenessRatio2=15
speckleWindowSize2=0
speckleRange2=2
preFilterCap2=63
brightness=1
contrast=1

disparity = func_disparity(window_size, minDisparity2, numDisparities2, blockSize2, disp12MaxDiff2, uniquenessRatio2, speckleWindowSize2, speckleRange2, preFilterCap2, brightness, contrast, event=None)

file = Image.fromarray(disparity)
file.show()
'''

0 个答案:

没有答案