-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrate.py
146 lines (120 loc) · 4.97 KB
/
calibrate.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import argparse
import json
import cv2
import cv2.aruco as aruco
import numpy as np
import glob as gl
import os
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return dict(type_id="opencv-matrix",
rows=obj.shape[0],
cols=obj.shape[1],
data=[item for sublist in obj.tolist() for item in sublist],
dt="d")
return json.JSONEncoder.default(self, obj)
def calibrate_camera(objPoints, imgPoints, imsize, board):
"""
Calibrates the camera using the dected corners.
"""
print("CAMERA CALIBRATION", imsize)
(ret, camera_matrix, distortion_coefficients0,
rotation_vectors, translation_vectors) = cv2.calibrateCamera(
objectPoints=objPoints,
imagePoints=imgPoints,
imageSize=imsize,
cameraMatrix=None,
distCoeffs=None)
return (ret, camera_matrix, distortion_coefficients0,
rotation_vectors, translation_vectors)
def read_chessboards(images, aruco_dict, board: aruco.CharucoBoard):
"""
Charuco base pose estimation.
"""
print("POSE ESTIMATION STARTS:")
allObjPoints = []
allImgPoints = []
allImages = []
charuco = cv2.aruco.CharucoParameters()
charuco.tryRefineMarkers = True
detector_params = cv2.aruco.DetectorParameters()
detector_params.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_CONTOUR
refine = cv2.aruco.RefineParameters()
detector = cv2.aruco.CharucoDetector(board,
charuco,
detector_params,
refine)
outdir = "out-cal"
if os.path.exists(outdir):
os.remove(outdir)
os.makedirs(outdir)
i = 0
for im in images:
print("=> Processing image {0}".format(im))
frame = cv2.imread(im)
if frame is None:
print("cannot read ", im)
continue
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(charucoCorners, charucoIds,
markerCorners, markerIds) = detector.detectBoard(gray)
if charucoCorners is not None and charucoIds is not None:
if (len(charucoCorners) > 35):
charucoCorners2 = cv2.cornerSubPix(
gray, charucoCorners, (3, 3), (-1, -1),
(cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 40, 0.001))
objPoints, imgPoints = board.matchImagePoints(
charucoCorners2, charucoIds)
# plt.figure()
cv2.aruco.drawDetectedCornersCharuco(
frame, charucoCorners, charucoIds,
(0, 0, 255))
cv2.aruco.drawDetectedCornersCharuco(
frame, charucoCorners2, charucoIds,
(0, 255, 0))
# cv2.aruco.drawDetectedMarkers(frame, markerCorners, markerIds)
cv2.imwrite(outdir + "/" + str(i) + ".png", frame)
# plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
# plt.show()
# include = input("Include this image? (y/n): ")
include = "y"
if include == "y":
allObjPoints.append(objPoints)
allImgPoints.append(imgPoints)
allImages.append(im)
i += 1
imsize = gray.shape[::-1]
return allObjPoints, allImgPoints, imsize, allImages
def main():
global dictionary
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--input')
parser.add_argument('--output')
parser.add_argument('--rows', "-r", type=int, default=11)
parser.add_argument('--cols', "-c", type=int, default=9)
parser.add_argument('--markersize', "-m", type=float, default=0.016)
parser.add_argument('--squaresize', "-s", type=float, default=0.022)
parser.add_argument('--dictionary', "-d", default="DICT_5X5_1000")
args = parser.parse_args()
dic = getattr(cv2.aruco, args.dictionary)
aruco_dict = cv2.aruco.getPredefinedDictionary(dic)
board = aruco.CharucoBoard((args.cols, args.rows),
args.squaresize, args.markersize, aruco_dict)
images = args.input
images = gl.glob(images)
objPoints, imgPoints, imsize, allImages = read_chessboards(
images, aruco_dict, board)
print(imsize)
ret, mtx, dist, rvecs, tvecs = calibrate_camera(objPoints, imgPoints, imsize, board)
print("result", ret, mtx, dist)
if args.output:
objPoints = []
imgPoints = []
o = dict(result=ret, camera_matrix=mtx, distortion_coefficients=dist,
rvecs=rvecs, tvecs=tvecs, imsize=imsize)
json.dump(o, open(args.output, "w"), indent=4, sort_keys=True, cls=NumpyEncoder)
print("result", ret)
if __name__ == '__main__':
main()