我正在做一个障碍物探测器项目,并且正在使用立体声相机。我已经完成了两个相机的校准。但是对于计算视差,我感到视差图不理想。视差图的图像在这里:
我认为在将参数传递给cv.StereoSGBM_create()函数时犯了一些错误。谁能解释我如何获得正确的视差图?我在python中的代码在下面
from __future__ import print_function
import numpy as np
import cv2 as cv
import glob
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
'''
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 ')
if __name__ == '__main__':
print('loading images...')
imgL = cv.pyrDown( cv.imread('D:/Images/left_img.jpg') ) # downscale images for faster processing
imgR = cv.pyrDown( cv.imread('D:/Images/right_img.jpg') )
hl, wl = imgL.shape[:2]
hr, wr = imgR.shape[:2]
#reading camera parameters after calibration
Rc = np.load('D:/Images/Rcalib.npz')
Lc = np.load('D:/Images/Lcalib.npz')
# undistorting images
newCameraMtxR, roiR = cv.getOptimalNewCameraMatrix(Rc['mtx'], Rc['dist'], (wr, hr), 1, (wr, hr))
udImgR = cv.undistort(imgR, Rc['mtx'], Rc['dist'], None, newCameraMtxR) #undistorted Right Image
newCameraMtxL, roiL = cv.getOptimalNewCameraMatrix(Lc['mtx'], Lc['dist'], (wl, hl), 1, (wl, hl))
udImgL = cv.undistort(imgL, Lc['mtx'], Lc['dist'], None, newCameraMtxL)
window_size = 3
min_disp = 16
num_disp = 112-min_disp
stereo = cv.StereoSGBM_create(minDisparity = min_disp,
numDisparities = num_disp,
blockSize = 16,
P1 = 8*3*window_size**2,
P2 = 32*3*window_size**2,
disp12MaxDiff = 1,
uniquenessRatio = 10,
speckleWindowSize = 100,
speckleRange = 32
)
print('computing disparity...')
disp = stereo.compute(udImgL, udImgR).astype(np.float32) / 16.0
print('generating 3d point cloud...',disp[0][0])
h, w = udImgL.shape[:2]
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, -948], # 948 is focal length of cameras from camera matrix,after calibration
[0, 0, 1, 0]])
points = cv.reprojectImageTo3D(disp, Q)
colors = cv.cvtColor(udImgL, cv.COLOR_BGR2RGB)
mask = disp > disp.min() #boolean mask of 2D after condition,where disparity is greater then minimum disparity
out_points = points[mask] # contain only those points where this above condition match
out_colors = colors[mask]
out_fn = 'out.ply'
write_ply('out.ply', out_points, out_colors)
print('%s saved' % 'out.ply')
cv.imshow('left', udImgL)
cv.imshow('disparity', (disp-disp.min())/(disp.max()-disp.min()))
b=0.0635 #baseline
D=b*948/disp #depth for each point
print('Max dasparity: ',disp.max())
mat=np.matrix(D)
print('distance: ',mat.min())
with open('outfile.txt','wb') as f:
for line in mat:
np.savetxt(f, line, fmt='%.2f')
cv.waitKey()
cv.destroyAllWindows()