-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibrate-camera.py
More file actions
61 lines (47 loc) · 1.63 KB
/
Copy pathcalibrate-camera.py
File metadata and controls
61 lines (47 loc) · 1.63 KB
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
61
import cv2
import numpy as np
# Define the chessboard size
chessboard_size = (9, 6)
frame_interval = 20
square_size = 2.5 # 2.5cm
# Prepare object points
objp = np.zeros((chessboard_size[0] * chessboard_size[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
objp *= square_size
# Arrays to store object points and image points from all the images
objpoints = []
imgpoints = []
# Capture video from the default camera
# video_path = 'videos/videos10/cal10.mp4'
video_path = 'videos/working-videos/output_cal_planner.avi'
cap = cv2.VideoCapture(video_path)
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
if frame_count % frame_interval == 0:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)
if ret:
objpoints.append(objp)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(frame, chessboard_size, corners, ret)
cv2.imshow('Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
print('image points:', len(imgpoints))
# Calibrate the camera
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
if not ret:
print("Camera calibration failed.")
exit()
# Save the calibration results
np.savez('calibration_data.npz', mtx=mtx, dist=dist)
print("Camera calibrated successfully.")
print("Camera matrix:\n", mtx)
print("Distortion coefficients:\n", dist)