mardi 14 juin 2016

My stereo rectified (OpenCV) images are wrong. PYTHON

I am using OpenCV to compute a disparity map of a scene. I have already calibrate the stereo camera by finding the intrinsic parameters individually with cv2.calibrateCamera and then with cv2.stereoCalibrate to find the rotation matrix and the translation vector. I copy my calibration code but I think my problem is not here:

import numpy as np
import cv2


# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.000001)

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


# Arrays to store object points and image points from all the images.
objpointsL = [] 
imgpointsL = [] 
objpointsR = []
imgpointsR = []

imgR = cv2.imread('right2.jpg',0)

# Find the chess board corners
ret, cornersR = cv2.findChessboardCorners(imgR, (7,6),None)
# If found, add object points, image points (after refining them)
if ret == True:
    objpointsR.append(objp)

    cv2.cornerSubPix(imgR,cornersR,(11,11),(-1,-1),criteria)
    imgpointsR.append(cornersR)


imgL = cv2.imread('left3.jpg',0)
#grayL = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Find the chess board corners
ret, cornersL = cv2.findChessboardCorners(imgL, (7,6),None)

# If found, add object points, image points (after refining them)
if ret == True:
    objpointsL.append(objp)

    cv2.cornerSubPix(imgL,cornersL,(11,11),(-1,-1),criteria)
    imgpointsL.append(cornersL)   




#Intrinsic parameters
distCoeffsR = np.array([1.191755160158372399e-02, -8.585146447098485067e-03, 8.429413399383550720e-04, -6.222973871460263460e-05, -7.966310474599684957e-03])
distCoeffsL = np.array([-1.627558337813042599e-02, 2.409982163230293128e-01, 4.443126374210568282e-03, 1.288079049351137243e-03, -3.177831292965794807e-01])
cameraMatrixR = np.matrix('3.252248978261580987e+02 0 3.269955537627058106e+02;0 3.228400384496266042e+02 2.341068611530280350e+02;0 0 1')
cameraMatrixL = np.matrix('4.570360097428241488e+02 0 3.465188967298854550e+02;0 4.573286269805292363e+02 2.691439570063795372e+02;0 0 1')


retval,cameraMatrixL, distCoeffsL, cameraMatrixR, distCoeffsR, R, T, E, F = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, cameraMatrixL, distCoeffsL, cameraMatrixR, distCoeffsR, (640,480))

Now I cv2.stereoRectify:

lFrame = cv2.imread('izquierda.jpg')
rFrame = cv2.imread('derecha.jpg')
w, h = lFrame.shape[:2] # both frames should be of same shape

#Perform stereorectification 
R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify(cameraMatrixL, distCoeffsL, cameraMatrixR, distCoeffsR, (w,h), R, T, cv2.CALIB_ZERO_DISPARITY,0, (0,0))

#computes undistort and rectify maps
mapxL, mapyL = cv2.initUndistortRectifyMap(cameraMatrixL, distCoeffsL, R1, P1, (w,h), cv2.CV_32FC1)
mapxR, mapyR = cv2.initUndistortRectifyMap(cameraMatrixR, distCoeffsR, R2, P2, (w,h), cv2.CV_32FC1)


dstL = cv2.remap(lFrame, mapxL, mapyL,cv2.INTER_LINEAR)
dstR = cv2.remap(rFrame, mapxR, mapyR,cv2.INTER_LINEAR)

while (True):

    cv2.imshow('Left normal',lFrame)
    cv2.imshow('Right normal',rFrame)
    cv2.imshow('Left rectify',dstL)
    cv2.imshow('Right rectify',dstR)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

And these are the rectified images:

Left rectify Right rectify

Anyone can help me please? I am getting stuck with this...

Aucun commentaire:

Enregistrer un commentaire