Error with stereocalibrate function in python

asked 2017-06-27 09:42:34 -0600

Amelie gravatar image

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 ... (more)

edit retag flag offensive close merge delete

Comments

that's a WALL of code !

do us at least the favour, and hint us at where that error is happening.

berak gravatar imageberak ( 2017-06-27 10:16:37 -0600 )edit
1

In fact is the all code. The error appears at the last line with the stereocalibrate function. I have no idea which part of the code I can remove without losting information.

Amelie gravatar imageAmelie ( 2017-06-27 10:45:35 -0600 )edit

ok, so it's probably this part: R=None,T=None,F=None,E=None

your numpy version ?

(again, i forgot the proper answer, but assigning Noneto those values might be the culprit)

berak gravatar imageberak ( 2017-06-27 10:48:40 -0600 )edit

just an idea, what if you simply omit those ? like:

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],  flags=cv2.CALIB_FIX_INTRINSIC)
berak gravatar imageberak ( 2017-06-27 10:51:25 -0600 )edit

It gives the same system error.

Amelie gravatar imageAmelie ( 2017-06-27 11:05:14 -0600 )edit

last idea: () instead of None ?

berak gravatar imageberak ( 2017-06-27 11:06:53 -0600 )edit

I tried this idea and it did not work.

Amelie gravatar imageAmelie ( 2017-06-28 00:53:20 -0600 )edit

My version of numpy is 1.11.0

Amelie gravatar imageAmelie ( 2017-06-28 03:11:51 -0600 )edit