In [50]:
from pathlib import Path
import cv2
import numpy as np

In [51]:
boards = Path('tests/images/').glob('left*.jpg')
boards


Out[51]:
<generator object Path.glob at 0x7f46063bc3b8>

In [52]:
for board in boards:
    print(board, board.exists())


tests/images/left04.jpg True
tests/images/left14.jpg True
tests/images/left03.jpg True
tests/images/left02.jpg True
tests/images/left06.jpg True
tests/images/left12.jpg True
tests/images/left13.jpg True
tests/images/left09.jpg True
tests/images/left01.jpg True
tests/images/left.jpg True
tests/images/left08.jpg True
tests/images/left11.jpg True
tests/images/left07.jpg True
tests/images/left05.jpg True

In [53]:
pattern_size = (9, 6)
square_size = 1.0
pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)            
pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)            
pattern_points *= square_size 
#pattern_points

In [54]:
obj_points = []                                                              
img_points = []                                                              
h, w = 0, 0                                                                  
img_names_undistort = []

In [75]:
for board in boards:
    img = cv2.imread(fn, 0)
    h, w = img.shape[:2]
    
    found, corners = cv2.findChessboardCorners(img, pattern_size)
    if found:                                                                
        term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)    
        cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
    else:                                                           
        print('chessboard not found')
        continue

    img_points.append(corners.reshape(-1, 2))
    obj_points.append(pattern_points)
    
print(h, w)


0 0

In [83]:
board = 'tests/images/left.jpg'
img = cv2.imread(board)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
shape = gray.shape[::-1]
shape


Out[83]:
(612, 459)

In [85]:
# I think I need img.shape[:2] in place of (h, w) but it is out of scope.
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, gray.shape[::-1], None, None)


---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-85-1d8e343c2695> in <module>()
      1 # I think I need img.shape[:2] in place of (h, w) but it is out of scope.
----> 2 ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, gray.shape[::-1], None, None)

error: /home/jsk/GitHub/opencv/modules/calib3d/src/calibration.cpp:3306: error: (-215) nimages > 0 in function calibrateCamera

In [ ]: