1

我使用我在网上找到的其他代码编写了此代码。我没有收到任何错误,但是我无法使用 imshow 显示视差图。我保存了图像,它只是黑色的,所以我没有正确地做某事。有人可以看看我的代码,看看有什么问题吗?我可能没有正确地进行立体校准,但我不确定。谢谢!

import numpy as np
import cv2
import glob
import os
from matplotlib import pyplot as plt

print 'program starts'

from matplotlib import pyplot as plt


#termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.001)

#-------------------------------------------------------------------------
#INITIALIZATION VARIABLES

#prepare object points 
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)

#Arrays to store object points and image points from all the images. 
objpoints = []  #3D points in real world space
imgpointsL = []  #2D points in image plane.
imgpointsR = []
#-------------------------------------------------------------------------

os.chdir('/home/pi/Desktop/LeftImg')
images = sorted(glob.glob('*.png'))  
 #print images

for fname in images:

    img = cv2.imread(fname)
    grayL = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # find chess board corners
    ret, cornersL = cv2.findChessboardCorners(grayL, (9,6))

    # if found, add object points, image points (after refining them)
    if ret == True: 
        objpoints.append(objp)
    cv2.cornerSubPix(grayL, cornersL, (11,11),(-1,-1), criteria)
        imgpointsL.append(cornersL)

print len(objpoints)
os.chdir('/home/pi/Desktop/RightImg')
images = sorted(glob.glob('*.png'))  
 #print images
for fname in images:

    img = cv2.imread(fname)
    grayR = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # find chess board corners
    ret, cornersR = cv2.findChessboardCorners(grayR, (9,6))

    # if found, add object points, image points (after refining them)
    if ret == True: 
        prevobjp = objp
    objpoints.append(objp)
    cv2.cornerSubPix(grayR, cornersR,(11,11),(-1,-1), criteria)
        imgpointsR.append(cornersR)

    c[prevobjp & objp]
    if c.all():
        print 'True'
    else: 
        print 'False'


#print len(objpointsR)

    #Draw and display corners
         #img = cv2.drawChessboardCorners(img, (9,6), cornersR,ret)
        # cv2.drawChessboardCorners(img, (9,6), cornersR,ret)
       # plt.imshow(img)
       #  plt.show()
        # print type(img)
    # cv2.imshow('img', img)
    # cv2.waitKey(500)      


print 'start'   

cameraMatrix1 = cv2.cv.CreateMat(3,3, cv2.CV_64FC1)
cameraMatrix2 = cv2.cv.CreateMat(3,3, cv2.CV_64FC1)

retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpoints, imgpointsL, imgpointsR, (384,288))

print cameraMatrix1, cameraMatrix2

print 'over'
R1= np.zeros(shape=(3,3))
R2= np.zeros(shape=(3,3))
P1= np.zeros(shape=(3,4))
P2= np.zeros(shape=(3,4))
Q= np.zeros(shape=(4,4))
map1x=[]
map1y=[]
map2x=[]
map2y=[]
#imgU1=[]
#imgU2=[]
#print cameraMatrix1
#print cameraMatrix1
os.chdir('/home/pi/Desktop')
imgL=cv2.imread('calLeft1.png')
imgR=cv2.imread('calRight1.png')

imgL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
imgR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)

cv2.stereoRectify(cameraMatrix1,distCoeffs1,cameraMatrix2,distCoeffs2,(384,288),R,T,R1,R2,P1,P2,Q,flags=cv2.cv.CV_CALIB_ZERO_DISPARITY,alpha=-1,newImageSize=(0,0))
#cv2.reprojectImageTo3D(disp,points,Q)

print Q

map1x,map1y=cv2.initUndistortRectifyMap(cameraMatrix1,distCoeffs1,R1,P1,(384,288),cv2.CV_32FC1)
map2x,map2y=cv2.initUndistortRectifyMap(cameraMatrix2,distCoeffs2,R2,P2,(384,288),cv2.CV_32FC1)

#cv2.remap(imgL,map1x,map1y,cv2.INTER_LINEAR, imgU1, cv2.BORDER_CONSTANT,0)
#cv2.remap(imgR,map2x,map2y,cv2.INTER_LINEAR, imgU2, cv2.BORDER_CONSTANT,0)

imgU1 = cv2.remap(imgL,map1x,map1y,cv2.INTER_LINEAR)
imgU2 = cv2.remap(imgR,map2x,map2y,cv2.INTER_LINEAR)

#imgU1 = imgU1.astype(np.uint8)
#imgU2 = imgU2.astype(np.uint8)

stereo = cv2.StereoBM(1,16,15)
disp=stereo.compute(imgU1,imgU2,disptype=cv2.CV_32FC1)  
norm_coeff = 255/disp.max()
cv2.imshow('disp', disp*norm_coeff/255)

cv2.imwrite('dispimage.png', disp*norm_coeff/255)


# When everything done, release the capture
 #cap.release()
#cv2.destroyAllWindows()
4

0 回答 0