-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration.py
142 lines (96 loc) · 4.34 KB
/
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
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
import numpy as np
import cv2 as cv
import glob
import pickle
chessboardSize = (8,6)
# frameSize = (3648,5472)
frameSize = (3648//3,5472//3)
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((chessboardSize[0] * chessboardSize[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardSize[0],0:chessboardSize[1]].T.reshape(-1,2)
size_of_chessboard_squares_mm = 19
objp = objp * size_of_chessboard_squares_mm
objpoints = []
imgpoints = []
def calibMatrix(images,cameName):
workT = False
idx = 0
for image in images:
print(f"Adim: {idx}")
idx+= 1
img = cv.imread(image)
img = cv.resize(img,(3648//3,5472//3))
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, corners = cv.findChessboardCorners(gray, chessboardSize, None)
if ret == True:
workT = True
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
cv.drawChessboardCorners(img, chessboardSize, corners2, ret)
cv.imshow('img', cv.resize(img,(1920,1080)))
cv.waitKey(1000)
if workT == False:
assert ValueError("Görüntü Matrisi Yok")
cv.destroyAllWindows()
ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, frameSize, None, None)
pickle.dump((cameraMatrix, dist), open(f"calibMatrix_{cameName}_calibration.pkl", "wb" ))
pickle.dump(cameraMatrix, open(f"calibMatrix_{cameName}_cameraMatrix.pkl", "wb" ))
pickle.dump(dist, open(f"calibMatrix_{cameName}_dist.pkl", "wb" ))
img = cv.imread(images[1])
h, w = img.shape[:2]
newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(cameraMatrix, dist, (w,h), 1, (w,h))
dst = cv.undistort(img, cameraMatrix, dist, None, newCameraMatrix)
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite(f'calibMatrix_{cameName}_caliResult1.png', dst)
mapx, mapy = cv.initUndistortRectifyMap(cameraMatrix, dist, None, newCameraMatrix, (w,h), 5)
dst = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite(f'calibMatrix_{cameName}_caliResult2.png', dst)
mean_error = 0
for i in range(len(objpoints)):
imgpoints2, _ = cv.projectPoints(objpoints[i], rvecs[i], tvecs[i], cameraMatrix, dist)
error = cv.norm(imgpoints[i], imgpoints2, cv.NORM_L2)/len(imgpoints2)
mean_error += error
print( "total error: {}".format(mean_error/len(objpoints)) )
images1 = glob.glob('images/camera1/**')
images2 = glob.glob("images/camera2/**")
calibMatrix(images1,"camera1")
# calibMatrix(images2,"camera2")
# def calibImages():
# for image in images:
# img = cv.imread(image)
# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# ret, corners = cv.findChessboardCorners(gray, chessboardSize, None)
# if ret == True:
# objpoints.append(objp)
# corners2 = cv.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
# imgpoints.append(corners)
# cv.drawChessboardCorners(img, chessboardSize, corners2, ret)
# cv.imshow('img', cv.resize(img,(1920,1080)))
# cv.waitKey(1000)
# cv.destroyAllWindows()
# ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, frameSize, None, None)
# pickle.dump((cameraMatrix, dist), open( "calibration.pkl", "wb" ))
# pickle.dump(cameraMatrix, open( "cameraMatrix.pkl", "wb" ))
# pickle.dump(dist, open( "dist.pkl", "wb" ))
# img = cv.imread('images/camera1/Image_20240926130733201.bmp')
# h, w = img.shape[:2]
# newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(cameraMatrix, dist, (w,h), 1, (w,h))
# dst = cv.undistort(img, cameraMatrix, dist, None, newCameraMatrix)
# x, y, w, h = roi
# dst = dst[y:y+h, x:x+w]
# cv.imwrite('caliResult1.png', dst)
# mapx, mapy = cv.initUndistortRectifyMap(cameraMatrix, dist, None, newCameraMatrix, (w,h), 5)
# dst = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)
# x, y, w, h = roi
# dst = dst[y:y+h, x:x+w]
# cv.imwrite('caliResult2.png', dst)
# mean_error = 0
# for i in range(len(objpoints)):
# imgpoints2, _ = cv.projectPoints(objpoints[i], rvecs[i], tvecs[i], cameraMatrix, dist)
# error = cv.norm(imgpoints[i], imgpoints2, cv.NORM_L2)/len(imgpoints2)
# mean_error += error
# print( "total error: {}".format(mean_error/len(objpoints)) )