0

I was doing stereo camera calibration using Python 2.7 and OpenCV 3.3. The code I used is (I got it from Stereo Calibration Opencv Python and Disparity Map):

import numpy as np
import cv2
import glob

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# 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 = [] # 3d point in real world space
imgpointsL = [] # 2d points in image plane.
objpointsR = []
imgpointsR = []

images = glob.glob('left*.jpg')

for fname in images:
    img = cv2.imread(fname)
    grayL = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

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

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

images = glob.glob('right*.jpg')

for fname in images:
    img = cv2.imread(fname)
    grayR = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

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

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

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


retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, (640,480))

But I got error like:

retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, (640,480))
TypeError: Required argument 'distCoeffs1' (pos 5) not found

I have my code, left and rights images in the same folder. I have read other similar answers but they don't get this error. (Stereo Calibration Opencv Python and Disparity Map). I want to know why this error and how to solve this?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Naseeb Gill
  • 661
  • 10
  • 23
  • You can try assigning the returned value of `cv2.stereroCalibrate()` to a `list` and check the list for the values. – Lakshya Kejriwal Sep 02 '17 at 22:48
  • Thanks for reply @shawshank. But I didn't get you because I just start learning python. i think you asked me to try: output = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, (480,640)). But with this too error is same. – Naseeb Gill Sep 02 '17 at 23:15

1 Answers1

1

Try changing the last lines of your code to

cameraMatrix1 = None
distCoeffs1 = None
cameraMatrix2 = None
distCoeffs2 = None
R = None
T = None
E = None
F = None
retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpointsL, imgpointsL, imgpointsR, (640,480), flags=cv2.cv.CV_CALIB_FIX_INTRINSIC)
Lakshya Kejriwal
  • 1,340
  • 4
  • 17
  • 27
  • As cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2 are not defined in my above code anywhere (I have to find out these), error comes out as NameError: name 'cameraMatrix1' is not defined. – Naseeb Gill Sep 02 '17 at 23:38
  • I tried this but error comes as AttributeError: 'module' object has no attribute 'cv'. I also tried to run code by removing cv but then it shows AttributeError: 'module' object has no attribute 'CV_CALIB_FIX_INTRINSIC'. – Naseeb Gill Sep 03 '17 at 03:30
  • I tried this too : retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objpointsL,imgpointsL, imgpointsR,cameraMatrix1, distCoeffs1,cameraMatrix2, distCoeffs2, (640,480),R,T,E,F, flags=cv2.CALIB_FIX_INTRINSIC) then it shows error :cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\calib3d\src\calibration.cpp:3104: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function cv::collectCalibrationData – Naseeb Gill Sep 03 '17 at 03:52
  • You can remove the argument `flags=cv2.cv.CALIB_FIX_INTRINSIC` – Lakshya Kejriwal Sep 03 '17 at 06:30
  • As for the second error that you have it means that your 3D points and 2D points are not of the same length. You have to ensure that they are of same length. – Lakshya Kejriwal Sep 03 '17 at 06:34
  • Yes, you are right that mine points were not of same size. Mine imgpointsL contains values for 11 images while imgpointsR contains values for 10 images. I have 13 images for each. I don't know why it shows 11 and 10? But when I used (9x6) size instead of (7x6), both shows values for 13 images. And I got no error. Why it happens so, by changing the size it detected all images? – Naseeb Gill Sep 03 '17 at 17:48
  • I am not sure but it doesn't look like that that is an error because of OpenCV – Lakshya Kejriwal Sep 03 '17 at 18:07