Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Error with stereocalibrate function in python

Hi all !

I have a problem with the stereocalibrate function in python although calibrateCamera works well with the same structure of data (e.g. list of numpy arrays). The error is a system error : " new style getargs format but argument is not a tuple". My version of python is 3.5 and OpenCV is 3.2.0.dev.

I put my code below :

import os
import numpy as np
import cv2

pathDir='/home/amelie/Documents/these/donnees_ucon/calibration_170410/'
prefixCamera='cam'
imageExtension='png'
cameraNumber=3
referenceCamera=1
imageNumber=200
maxImageCalibration=100
xCornerNumber=11
yCornerNumber=7

criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

objP=np.zeros((xCornerNumber*yCornerNumber,3), np.float32)
objP[:,0:2] = np.mgrid[0:xCornerNumber,0:yCornerNumber].T.reshape(xCornerNumber*yCornerNumber,2)
objPoints = np.nan*np.ones((cameraNumber,imageNumber,xCornerNumber*yCornerNumber,3)) #3d point in real world space

imgPoints = np.nan*np.ones((cameraNumber,imageNumber,xCornerNumber*yCornerNumber,2)) #2d points in image plane.

validImages=np.zeros((imageNumber, cameraNumber),np.uint)

for camera in range(1,cameraNumber+1):
    for image in range(1,imageNumber+1):
        fname=pathDir+prefixCamera+str(camera)+'_'+str(image)+'.'+imageExtension

        if os.path.isfile(fname):
            print(prefixCamera+str(camera)+'_'+str(image)+'.'+imageExtension)

            img = cv2.imread(fname)
            gray=img[:,:,1] #Study on the green channel of the image

            # Find the chess board corners
            found, corners = cv2.findChessboardCorners(gray, (xCornerNumber,yCornerNumber),None)
            if found == True:

                corners=corners.reshape(xCornerNumber*yCornerNumber,2)
                corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
                # Draw and display the corners
                img = cv2.drawChessboardCorners(img, xCornerNumber,yCornerNumber),corners2,found)
                imgPoints[camera-1,image-1,:,:]=corners2
                objPoints[camera-1,image-1,:,:]=objP
                validImages[image-1,camera-1]=1
                hist=np.histogram(gray,255,(0,255))                 
            else:
                hist=np.histogram(gray,255,(0,255))

objPointsCameras=[]
imgPointsCameras=[]
projErrorCameras=[]
matrixCameras=[]
distortCoeffCameras=[]
rotVectorsCameras=[]
trVectorsCameras=[]

validImagesIndex=np.where(np.prod(validImages,axis=1)==1)
for camera in range(1,cameraNumber+1):

    validImgPointsCam=imgPoints[camera-1,validImagesIndex[0],:,:]
    validObjPointsCam=objPoints[camera-1,validImagesIndex[0],:,:]

    print(len(validImgPointsCam))

    index=0
    imgPointsCamList=[]
    objPointsCamList=[]
    while index<validImgPointsCam.shape[0] and index<maxImageCalibration:
        imgPointsCamList.append(validImgPointsCam[index,:,:].astype('float32'))
        objPointsCamList.append(validObjPointsCam[index,:,:].astype('float32'))
        index=index+1

    squaredError, matrix, distortions, rotVectors, trVectors = cv2.calibrateCamera(objPointsCamList, imgPointsCamList, gray.shape, None,None)

    #Calculates the re-projection error
    meanError = 0
    for i in range(0,len(rotVectors)):
        imgPoints2, _ = cv2.projectPoints(validObjPointsCam[i], rotVectors[i], trVectors[i], matrix, distortions)
        imgPoints2=imgPoints2.reshape(imgPoints2.shape[0],imgPoints2.shape[2])
        error = cv2.norm(validImgPointsCam[i],imgPoints2, cv2.NORM_L2)/len(imgPoints2)
        meanError += error
    print('Au final '+str(meanError/i)+ '\n')

    objPointsCameras.append(objPointsCamList)
    imgPointsCameras.append(imgPointsCamList)
    projErrorCameras.append(meanError)
    matrixCameras.append(matrix)
    distortCoeffCameras.append(distortions)
    rotVectorsCameras.append(rotVectors)
    trVectorsCameras.append(trVectors)

objPointsRefCam=objPointsCameras[referenceCamera-1]
imgPointsRefCam=imgPointsCameras[referenceCamera-1]
matrixRefCam=matrixCameras[referenceCamera-1]
distortCoeffRefCam=distortCoeffCameras[referenceCamera-1]
rotVectorsRefCam=rotVectorsCameras[referenceCamera-1]
trVectorsRefCam=trVectorsCameras[referenceCamera-1]

for camera in range(1,cameraNumber+1):
    if camera!=referenceCamera:
        retval, matrix1, dist1, matrix2, dist2, R, T, F, E=cv2.stereoCalibrate(objPointsRefCam, imgPointsRefCam, imgPointsCameras[camera-1], gray.shape, matrixRefCam, distortCoeffRefCam, matrixCameras[camera-1], distortCoeffCameras[camera-1], R=None,T=None,F=None,E=None,criteria=None, flags=cv2.CALIB_FIX_INTRINSIC)

Thank for your help.

Amélie