-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrationstereocamera.cpp
204 lines (169 loc) · 6.32 KB
/
calibrationstereocamera.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "calibrationstereocamera.h"
CalibrationStereoCamera::CalibrationStereoCamera(
String pathCalibrationPicturesLeftCamera,
String pathCalibrationPicturesRightCamera)
: calibrationPictures(pathCalibrationPicturesLeftCamera,
pathCalibrationPicturesRightCamera) {}
CalibrationStereoCamera::~CalibrationStereoCamera()
{
destroyAllWindows();
}
void CalibrationStereoCamera::findAndStoreChessBoardPoints()
{
int numOfSuccessPictures = 0;
int maxNumOfSuccessPictures = 300;
for (int i{0}; i < this->chessBoardColumns; i++)
{
for (int j{0}; j < this->chessBoardRows; j++)
this->obj.push_back(cv::Point3f(squareSize * j, squareSize * i, 0));
}
// Left Camera
auto calibrationPicturesLeftCamera =
calibrationPictures.getCalibrationPicturesLeftCamera();
auto itCalPicLeftCam = calibrationPicturesLeftCamera.begin();
auto endItCalPicLeftCam = calibrationPicturesLeftCamera.end();
// Right Camera
auto calibrationPicturesRigtCamera =
calibrationPictures.getCalibrationPicturesRightCamera();
auto itCalPicRightCam = calibrationPicturesRigtCamera.begin();
auto endItCalPicRightCam = calibrationPicturesRigtCamera.end();
Size chessBoardSize = Size(chessBoardRows, chessBoardColumns);
while (itCalPicLeftCam != endItCalPicLeftCam &&
itCalPicRightCam != endItCalPicRightCam &&
numOfSuccessPictures < maxNumOfSuccessPictures)
{
leftPicture = imread(*(itCalPicLeftCam), IMREAD_GRAYSCALE);
rightPicture = imread(*(itCalPicRightCam), IMREAD_GRAYSCALE);
// Left Camera
bool patternFoundLeftCamera = findChessboardCorners(
leftPicture, chessBoardSize, cornersLeftCamera,
CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +
CALIB_CB_FAST_CHECK);
if (patternFoundLeftCamera)
{
cornerSubPix(
leftPicture, cornersLeftCamera, Size(11, 11), Size(-1, -1),
cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER,
30, 0.1));
}
// Right Camera
bool patternFoundRightCamera = findChessboardCorners(
rightPicture, chessBoardSize, cornersRightCamera,
CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +
CALIB_CB_FAST_CHECK);
if (patternFoundRightCamera)
{
cornerSubPix(
rightPicture, cornersRightCamera, Size(11, 11), Size(-1, -1),
cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER,
30, 0.1));
}
if (patternFoundLeftCamera && patternFoundRightCamera)
{
drawChessboardCorners(leftPicture, chessBoardSize, cornersLeftCamera,
patternFoundLeftCamera);
drawChessboardCorners(rightPicture, chessBoardSize, cornersRightCamera,
patternFoundRightCamera);
imagePointsLeftCamera.push_back(cornersLeftCamera);
imagePointsRightCamera.push_back(cornersRightCamera);
objectPoints.push_back(obj);
numOfSuccessPictures++;
}
// imshow("LeftImage", leftPicture);
// imshow("RightImage", rightPicture);
Mat concatImage;
hconcat(leftPicture, rightPicture, concatImage);
imshow("Calibrate Images", concatImage);
waitKey(1000);
cout << to_string(numOfSuccessPictures) << endl;
++itCalPicLeftCam;
++itCalPicRightCam;
}
}
void CalibrationStereoCamera::calibrateEachCamera()
{
// Calibrate left Camera
float errorLeftCamera =
calibrateCamera(objectPoints, imagePointsLeftCamera, leftPicture.size(),
mtxL, distL, R_L, T_L);
new_mtxL = getOptimalNewCameraMatrix(mtxL, distL, leftPicture.size(), 1,
leftPicture.size(), 0);
cout << "Project Error Left Camera : " << errorLeftCamera << endl;
float errorRightCamera =
calibrateCamera(objectPoints, imagePointsRightCamera, rightPicture.size(),
mtxR, distR, R_R, T_R);
new_mtxR = getOptimalNewCameraMatrix(mtxR, distR, rightPicture.size(), 1,
rightPicture.size(), 0);
cout << "Project Error Right Camera : " << errorRightCamera << endl;
}
void CalibrationStereoCamera::calibrateStereoCamera()
{
int flag = 0;
flag |= cv::CALIB_FIX_INTRINSIC;
float error = stereoCalibrate(
objectPoints, imagePointsLeftCamera, imagePointsRightCamera, new_mtxL,
distL, new_mtxR, distR, leftPicture.size(), Rot, Trns, Emat, Fmat, flag,
cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 30,
1e-6));
cout << "The stereo calibration error : " << error << endl;
stereoRectify(new_mtxL, distL, new_mtxR, distR, rightPicture.size(), Rot,
Trns, rect_l, rect_r, proj_mat_l, proj_mat_r, Q, 1);
}
void CalibrationStereoCamera::UndistortRectifyMap()
{
// left Camera
initUndistortRectifyMap(new_mtxL, distL, rect_l, proj_mat_l, leftPicture.size(), CV_16SC2, leftStereoMapX, leftStereoMapY);
// right Camera
initUndistortRectifyMap(new_mtxR, distR, rect_r, proj_mat_r, rightPicture.size(), CV_16SC2, rightStereoMapX, rightStereoMapY);
}
void CalibrationStereoCamera::saveFile()
{
FileStorage cvFile = FileStorage("calibrationParameters.xml", FileStorage::WRITE);
cvFile.write("leftStereoMapX", leftStereoMapX);
cvFile.write("leftStereoMapY", leftStereoMapY);
cvFile.write("rightStereoMapX", rightStereoMapX);
cvFile.write("rightStereoMapY", rightStereoMapY);
cvFile.write("Q", Q);
cvFile.release();
}
int CalibrationStereoCamera::getSquareSize() const
{
return squareSize;
}
void CalibrationStereoCamera::setSquareSize(int value)
{
squareSize = value;
}
int CalibrationStereoCamera::getChessBoardRows() const
{
return chessBoardRows;
}
void CalibrationStereoCamera::setChessBoardRows(int value)
{
chessBoardRows = value;
}
int CalibrationStereoCamera::getChessBoardColumns() const
{
return chessBoardColumns;
}
void CalibrationStereoCamera::setChessBoardColumns(int value)
{
chessBoardColumns = value;
}
void CalibrationStereoCamera::executeCaibrationStereoCamera()
{
cout << "Start Calibration Process..." << endl;
findAndStoreChessBoardPoints();
calibrateEachCamera();
calibrateStereoCamera();
UndistortRectifyMap();
saveFile();
}
template <class T>
void CalibrationStereoCamera::printAllPoint(const vector<T> &points)
{
for (auto const &point : points)
{
cout << point << endl;
}
}