-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration_with_mp4.py
53 lines (38 loc) · 1.68 KB
/
calibration_with_mp4.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
import cv2 as cv
import numpy as np
termiation_criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
chessboard_width = 7
chessboard_length = 10
objp = np.zeros((chessboard_width * chessboard_length, 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboard_length, 0:chessboard_width].T.reshape(-1, 2)
realworldpoints = []
imgpoints = []
videofilename = "2025-02-11-111507-h-3.mp4"
videopath = f'input/{videofilename}'
cap = cv.VideoCapture(videopath)
if not cap:
print("Error occured while instantiating Video Capture")
idx = 0
while (cap.isOpened()):
isframeavailable, frame = cap.read()
if isframeavailable != True:
print("Fetching Frame ended in INDEX: ", idx)
break
idx += 1
grayscaledimg = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
patternfound, corners = cv.findChessboardCorners(grayscaledimg, (chessboard_length, chessboard_width), None)
if idx % 50 == 0: print(f"chesssboard pattern of {idx}th frame has been found:", patternfound)
if patternfound:
realworldpoints.append(objp)
corners2 = cv.cornerSubPix(grayscaledimg, corners, (11,11), (-1,-1), termiation_criteria)
imgpoints.append(corners2)
cv.drawChessboardCorners(frame, (chessboard_length, chessboard_width), corners2, patternfound)
print("\nCalibrating the Camera matrix and distortion coefficient\n")
iscalibrated, mtx, dist, rvecs, tvecs = cv.calibrateCamera(realworldpoints, imgpoints, grayscaledimg.shape[::-1], None, None)
if iscalibrated:
print("Calibration is successfully done.")
print("Camera Matrix: \n", mtx, "\n")
print("Distortion Coefficient: \n", dist, "\n")
else:
print("Calibration failed")
cap.release()