-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam_calib.txt
335 lines (283 loc) · 10.6 KB
/
cam_calib.txt
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
Cam Calibration
create a new project called camera_calibration
uncheck Debug
add the following PATHs in projectfile cam_calib.pro at the end:
INCLUDEPATH += U:\opencv401\include
LIBS += U:\opencv401\bin\libopencv_core401.dll
LIBS += U:\opencv401\bin\libopencv_highgui401.dll
LIBS += U:\opencv401\bin\libopencv_imgcodecs401.dll
LIBS += U:\opencv401\bin\libopencv_imgproc401.dll
LIBS += U:\opencv401\bin\libopencv_videoio401.dll
LIBS += U:\opencv401\bin\libopencv_video401.dll
LIBS += U:\opencv401\bin\libopencv_features2d401.dll
LIBS += U:\opencv401\bin\libopencv_calib3d401.dll
LIBS += U:\opencv401\bin\libopencv_aruco401.dll
----------------------------------------
include in mainwindow.cpp under the #include "ui_mainwindow.h" further files for openCV-routines:
//opencv
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/aruco.hpp>
#include "opencv2/core.hpp"
#include <opencv2/calib3d.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace cv;
using namespace std;
--------------------------------------
unter using namespace std;
define additional parameters which we can use to adjust the behaviour of the program:
// Global variables
Mat frame1;
int keyboard; //input from keyboard
const float calibrationSquareDimension=0.0255f; //size of chessboard fields in [m]
const float arucoSquareDimension=0.05f; //size of our future aruco markers
const Size chessboardDimensions=Size(9,6); //this is the crosses between the chessboard fields
Mat cameraMatrix = Mat::eye(3,3,CV_64F); //creates a Matlab-style identity matrix
Mat distanceCoefficients;
--------------------------
in function MainWindow under
ui->setupUi(this);
call our function:
processvideo();
----------------------
after the destructor MainWindow::~MainWindow()
{
delete ui;
}
let's define what our function does:
void MainWindow::processvideo()
{
//createArucoMarkers();
CameraCalibrationProcess(cameraMatrix, distanceCoefficients);
//loadCameraCalibration("Cameracalibration", cameraMatrix, distanceCoefficients);
//startWebcamMonitoring(cameraMatrix, distanceCoefficients, arucoSquareDimension);
}
-------------------------
Our function processvideo simply calls other functions. We still have to define them:
void MainWindow::createArucoMarkers()
{
Mat outputMarker;
Ptr<aruco::Dictionary> markerDictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
for(int i = 0; i < 50; i++)
{
aruco::drawMarker(markerDictionary, i, 500, outputMarker, 1);
ostringstream convert;
string imageName="4x4Marker_";
convert << imageName << i << ".jpg";
imwrite(convert.str(), outputMarker);
}
}
void MainWindow::createKnownBoardPosition(Size boardSize, float squareEdgeLength, vector<Point3f>& corners)
{
for(int i =0; i < boardSize.height; i++)
{
for (int j=0; j< boardSize.width; j++)
{
corners.push_back(Point3f(j*squareEdgeLength, i*squareEdgeLength, 0.0f));
}
}
}
void MainWindow::getChessboardCorners(vector<Mat> images, vector<vector<Point2f>>& allfoundCorners, bool showResults)
{
for (vector<Mat>::iterator iter=images.begin(); iter !=images.end(); iter++)
{
vector<Point2f> pointBuf;
bool found = findChessboardCorners(*iter, Size(9,6), pointBuf, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);
if (found)
{
allfoundCorners.push_back(pointBuf);
}
if(showResults)
{
drawChessboardCorners(*iter, Size(9,6), pointBuf, found);
imshow("Looking for Corners",*iter);
waitKey();
}
}
}
void MainWindow::cameraCalibration(vector<Mat> calibrationImages, Size boardSize, float squareEdgeLength, Mat& cameraMatrix, Mat& distanceCoefficients)
{
vector<vector<Point2f>>checkerboardImageSpacePoints;
getChessboardCorners(calibrationImages, checkerboardImageSpacePoints, false);
vector<vector<Point3f>> worldSpaceCornerPoints(1);
createKnownBoardPosition(boardSize, squareEdgeLength, worldSpaceCornerPoints[0]);
worldSpaceCornerPoints.resize(checkerboardImageSpacePoints.size(), worldSpaceCornerPoints[0]);
vector<Mat> rVectors, tVectors;
distanceCoefficients=Mat::zeros(8,1,CV_64F);
calibrateCamera(worldSpaceCornerPoints, checkerboardImageSpacePoints, boardSize, cameraMatrix, distanceCoefficients, rVectors, tVectors);
}
bool MainWindow::saveCameraCalibration(String name, Mat cameraMatrix, Mat distanceCoefficients)
{
ofstream outStream(name);
if(outStream)
{
uint16_t rows=cameraMatrix.rows;
uint16_t columns=cameraMatrix.cols;
outStream<<rows<<endl;
outStream<<columns<<endl;
for(int r = 0; r < rows; r++)
{
for(int c = 0; c < columns; c++)
{
double value = cameraMatrix.at<double>(r,c);
outStream<<value<<endl;
}
}
rows = distanceCoefficients.rows;
columns = distanceCoefficients.cols;
outStream<<rows<<endl;
outStream<<columns<<endl;
for(int r = 0; r < rows; r++)
{
for(int c = 0; c < columns; c++)
{
double value = distanceCoefficients.at<double>(r,c);
outStream<<value<<endl;
}
}
outStream.close();
return true;
}
return false;
}
bool MainWindow::loadCameraCalibration(string name, Mat& cameraMatrix, Mat& distanceCoefficients)
{
ifstream inStream(name);
if (inStream)
{
uint16_t rows;
uint16_t columns;
inStream>>rows;
inStream>>columns;
cameraMatrix=Mat(Size(columns, rows), CV_64F);
for(int r=0; r<rows; r++)
{
for(int c=0; c < columns; c++)
{
double read=0.0f;
inStream>>read;
cameraMatrix.at<double>(r,c)=read;
cout<<cameraMatrix.at<double>(r,c)<<"\n";
}
}
//Distance Coefficients
inStream>>rows;
inStream>>columns;
distanceCoefficients=Mat::zeros(rows, columns, CV_64F);
for(int r=0; r<rows; r++)
{
for(int c=0; c < columns; c++)
{
double read=0.0f;
inStream>>read;
distanceCoefficients.at<double>(r,c)=read;
cout<<distanceCoefficients.at<double>(r,c)<< "\n";
}
}
inStream.close();
return true;
}
return false;
}
void MainWindow::CameraCalibrationProcess(Mat& cameraMatrix, Mat& distanceCoefficients)
{
Mat frame;
Mat drawToFrame;
vector<Mat> savedImages;
namedWindow("Webcam", WINDOW_AUTOSIZE);
VideoCapture vid(0);
if(!vid.isOpened())
{
cout<<"ERROR ACQUIRING VIDEO FEED\n";
exit(EXIT_FAILURE);
}
while(true)
{
if(!vid.read(frame))
break;
vector<Vec2f> foundPoints;
bool found = false;
found = findChessboardCorners(frame, chessboardDimensions, foundPoints, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);
frame.copyTo(drawToFrame);
drawChessboardCorners(drawToFrame, chessboardDimensions, foundPoints, found);
if(found)
imshow("Webcam", drawToFrame);
else
imshow("Webcam", frame);
switch(waitKey(10))
{
case' ':
//saving image by pressing SPACE
if(found)
{
Mat temp;
frame.copyTo(temp);
savedImages.push_back(temp);
cout<<"Foto aufgenommen. Anzahl:"<<savedImages.size()<<endl;
}
break;
case 13:
//start calibration process of saved images by pressing ENTER
if(savedImages.size()>15)
{
cout<<"calibration started..."<<endl;
cameraCalibration(savedImages, chessboardDimensions, calibrationSquareDimension, cameraMatrix, distanceCoefficients);
saveCameraCalibration("Cameracalibration", cameraMatrix, distanceCoefficients);
cout<<"saved calibration."<<endl;
}
break;
case 27:
//exit by pressing ESC
destroyAllWindows();
cout<<"Program finished."<<endl;
return;
}
}
}
-----------------------
Now, we still have to declare these functions in the headerfile.
switch to mainwindow.h
Add under ~MainWindow();:
void createArucoMarkers();
void processvideo();
void getChessboardCorners(vector<Mat> images, vector<vector<Point2f>>& allfoundCorners, bool showResults);
void createKnownBoardPosition(Size boardSize, float squareEdgeLength, vector<Point3f>& corners);
void cameraCalibration(vector<Mat> calibrationImages, Size boardSize, float squareEdgeLength, Mat& cameraMatrix, Mat& distanceCoefficients);
bool loadCameraCalibration(string name, Mat& cameraMatrix, Mat& distanceCoefficients);
bool saveCameraCalibration(String name, Mat cameraMatrix, Mat distanceCoefficients);
void CameraCalibrationProcess(Mat& cameraMatrix, Mat& distanceCoefficients);
int startWebcamMonitoring(const Mat& cameraMatrix, const Mat& distanceCoefficients, float arucoSquareDimension);
-------------------------
To avoid writing cv::Mat etc und std::vector etc every time in the header file, add after #include <QMainWindow>:
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
------------------------
-save all
-build (= compile the individual cpp-files + link them to a .exe file)
-copy the dlls into the newly created folder
-attach a webcam
A typical cam calib.matrix looks like this:
3 //means the calib matrix has 3 rows
3 //means the calib matrix has 3 columns
803.54 //camera const in x-direction
0 //cam const * shear --> here shear is zero
235.785 //optical center-x expressed in pixels
0 //always zero
819.909 //cam const times (1+scale difference)
539.964 //optical center-y expressed in pixels
0 //always zero
0 //always zero
1 //always 1
5 //the non linearity is expressed in 5 columns
1 //and one row (= it is a 5x1 Vector)
-0.45418 //radial distortion koefficient k1
0.350919 //radial distortion koefficient k2
-0.0437492 //tangential distortion koefficient p1
-0.00157205 //tangential distortion koefficient p2
-0.142265 //radial distortion koefficient k3