-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_calibration.py
45 lines (34 loc) · 1.3 KB
/
utils_calibration.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
import numpy as np
import cv2
import matplotlib.image as mpimg
import glob
# Read in and make a list of calibration images
images = glob.glob('camera_cal/calibration*.jpg')
# Array to store object points and image points from all the images
objpoints = [] # 3D points in real world space
imgpoints = [] # 2D points in image plane
def calib():
"""
To get an undistorted image, we need camera matrix & distortion coefficient
Calculate them with 9*6 20 chessboard images
"""
# Prepare object points
objp = np.zeros((6 * 9, 3), np.float32)
objp[:, :2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2) # x,y coordinates
for fname in images:
img = mpimg.imread(fname)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (9, 6), None)
# If corners are found, add object points, image points
if ret == True:
imgpoints.append(corners)
objpoints.append(objp)
else:
continue
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
return mtx, dist
def undistort(img, mtx, dist):
""" undistort image """
return cv2.undistort(img, mtx, dist, None, mtx)