In [50]:
from pathlib import Path
import cv2
import numpy as np
In [51]:
boards = Path('tests/images/').glob('left*.jpg')
boards
Out[51]:
In [52]:
for board in boards:
print(board, board.exists())
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)
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]:
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)
In [ ]: