-
Notifications
You must be signed in to change notification settings - Fork 1
/
Distortion
32 lines (25 loc) · 995 Bytes
/
Distortion
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
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)