-1

I am new to OpenCV and trying to follow an online tutorial on 3D reconstruction of a chessboard image.

I am using the following code for the process:

import glob
import cv2
import numpy as np

chessboard_size = (7,5)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
#objp = np.zeros((9*6,3), np.float32)
#objp[:,:2] = np.mgrid[0:6.0:9].T.reshape(-1,2)
objp = np.zeros((np.prod(chessboard_size),3),dtype=np.float32)
objp[:,:2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1,2)
objp = np.array([objp])

objpoints = []
imgpoints = []
images = glob.glob('./img/*')

for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret , corners = cv2.findChessboardCorners(gray,(6,9), None)
    #corners = np.array([[corner for [corner] in corners]])

    if ret == True:
        objpoints.append(objp)
        corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners)

        cv2.drawChessboardCorners(img, (6,9), corners2, ret)
    
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

However, I keep getting the following error:

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-95hbg2jt\opencv\modules\calib3d\src\calibration.cpp:3689: error: (-215:Assertion failed) nimages > 0 in function 'cv::calibrateCameraRO'

I have two questions here:

  1. What can I do to get past this error?
  2. How do I display the resulting image after 3D reconstruction?

I am using the following image as input: Input Image

TIA

saadurr
  • 57
  • 10
  • 1
    You should put "chessboard_size=(6,9)" in order to be consistent with what is below, shouldn't you? Also, the drawChessboardCorners won't give you anything unless you add: "cv2.imshow('test'', img); cv2.waitKey(); cv2.destroyAllWindows()" after it in order to produce a visible output. Regards. – S_Bersier Dec 24 '20 at 19:13

1 Answers1

1

Assertion failed, yes, but it also says that the assertion was nimages > 0

that means you're giving no data to calibrateCamera.

for that to happen, findChessboardCorners found no corners in the first picture.

please use a debugger or at least use print(...) to print values of variables.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36