-
Notifications
You must be signed in to change notification settings - Fork 1
/
CameraCalibration.py
60 lines (48 loc) · 1.86 KB
/
CameraCalibration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import cv2
import glob
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
class CameraCalibration():
""" Class that calibrate camera using chessboard images.
Attributes:
mtx (np.array): Camera matrix
dist (np.array): Distortion coefficients
"""
def __init__(self, image_dir, nx, ny, debug=False):
""" Init CameraCalibration.
Parameters:
image_dir (str): path to folder contains chessboard images
nx (int): width of chessboard (number of squares)
ny (int): height of chessboard (number of squares)
"""
fnames = glob.glob("{}/*".format(image_dir))
objpoints = []
imgpoints = []
# Coordinates of chessboard's corners in 3D
objp = np.zeros((nx*ny, 3), np.float32)
objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)
# Go through all chessboard images
for f in fnames:
img = mpimg.imread(f)
# Convert to grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Find chessboard corners
ret, corners = cv2.findChessboardCorners(img, (nx, ny))
if ret:
imgpoints.append(corners)
objpoints.append(objp)
shape = (img.shape[1], img.shape[0])
ret, self.mtx, self.dist, _, _ = cv2.calibrateCamera(objpoints, imgpoints, shape, None, None)
if not ret:
raise Exception("Unable to calibrate camera")
def undistort(self, img):
""" Return undistort image.
Parameters:
img (np.array): Input image
Returns:
Image (np.array): Undistorted image
"""
# Convert to grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
return cv2.undistort(img, self.mtx, self.dist, None, self.mtx)