视差图代码错误:cv :: StereoSGBMImpl :: compute'(-215 OpenCV)

时间:2018-11-09 17:02:47

标签: opencv

我正在尝试创建高质量的视差图。但是,我当前的视差图输出非常糟糕。这是我当前迭代的代码:

from __future__ import print_function
import os
import numpy as np
import cv2 as cv
import time

ply_header = '''ply
format ascii 1.0
element vertex %(vert_num)d
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
'''

cap = cv.VideoCapture(0)
cap2 = cv.VideoCapture(2)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
cap2.set(3,640) # set Width
cap2.set(4,480) # set Height

def write_ply(fn, verts, colors):
    verts = verts.reshape(-1, 3)
    colors = colors.reshape(-1, 3)
    verts = np.hstack([verts, colors])
    with open(fn, 'wb') as f:
        f.write((ply_header % dict(vert_num=len(verts))).encode('utf-8'))
        np.savetxt(f, verts, fmt='%f %f %f %d %d %d ')

while(True):
    if __name__ == '__main__':
        print('loading images...')
        bool1, image1 = cap.read()
        bool2, image2 = cap2.read()
        cv.imwrite('opencvL'+'.jpg', image1)
        cv.imwrite('opencvR'+'.jpg', image2)
        imgL = cv.imread('opencvL.jpg')  # downscale images for faster processing
        imgR = cv.imread('opencvR.jpg')

        # disparity range is tuned for 'aloe' image pair
        window_size = 7
        min_disp = 0
        max_disp = 160
        num_disp = 112-min_disp
        stereo = cv.StereoSGBM_create(minDisparity = min_disp,
            numDisparities = num_disp,
            blockSize = 5,
            P1 = 8*3*window_size**2,
            P2 = 32*3*window_size**2,
            disp12MaxDiff = 1,
            uniquenessRatio = 15,
            speckleWindowSize = 50,
            speckleRange = 2
        )

        print('computing disparity...')
        disp = stereo.compute(imgL, imgR).astype(np.float32) / 16.0

        print('generating 3d point cloud...',)
        h, w = imgL.shape[:2]
        f = 0.8*w                          # guess for focal length
        Q = np.float32([[1, 0, 0, -0.5*w],
                        [0,-1, 0,  0.5*h], # turn points 180 deg around x-axis,
                        [0, 0, 0,     -f], # so that y-axis looks up
                        [0, 0, 1,      0]])
        points = cv.reprojectImageTo3D(disp, Q)
        colors = cv.cvtColor(imgL, cv.COLOR_BGR2RGB)
        mask = disp > disp.min()
        out_points = points[mask]
        out_colors = colors[mask]
        #out_fn = 'out.ply'
        #write_ply('out.ply', out_points, out_colors)
        #print('%s saved' % 'out.ply')
        cv.imshow('left', imgL)
        cv.imshow('right', imgR)
        cv.imshow('disparity', (disp-min_disp)/num_disp)
        os.remove("opencvL.jpg")
        #os.remove("out.ply")
        os.remove("opencvR.jpg")

        if cv.waitKey(1) & 0xFF == ord('q'):
            break
        time.sleep(.1)
    else:
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

此代码有效,但是输出太可怕了。它甚至不像视差图。这就是我得到的:

Image of output

因此,我尝试了一些在网上进行编辑后发现的代码。在这里:

import numpy as np
from sklearn.preprocessing import normalize
import cv2

print('loading images...')
imgL = cv2.imread('left.jpg')  # downscale images for faster processing
imgR = cv2.imread('right.jpg')

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

left_matcher = cv2.StereoSGBM_create(
    minDisparity=0,
    numDisparities=160,             # max_disp has to be dividable by 16 f. E. HH 192, 256
    blockSize=5,
    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
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)
cv2.imshow('Disparity Map', filteredImg)
cv2.waitKey()
cv2.destroyAllWindows()

但是,当我尝试运行此命令时,这是命令终端的输出:

    C:\Users\Adithya Kumar\Desktop\sciencefair18\github\Sciencefair2018-19>python onlinefiltereddisparitymap.py
C:\Users\Adithya Kumar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
loading images...
computing disparity...
Traceback (most recent call last):
  File "onlinefiltereddisparitymap.py", line 38, in <module>
    displ = left_matcher.compute(imgL, imgR)  # .astype(np.float32)/16
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\calib3d\src\stereosgbm.cpp:2156: error: (-215:Assertion failed) left.size() == right.size() && left.type() == right.type() && left.depth() == CV_8U in function 'cv::StereoSGBMImpl::compute'


C:\Users\Adithya Kumar\Desktop\sciencefair18\github\Sciencefair2018-19>

任何帮助将不胜感激。

0 个答案:

没有答案