-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVCalibration.cpp
167 lines (144 loc) · 4.93 KB
/
CVCalibration.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
#include "CVCalibration.h"
#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <fstream>
#include <iostream>
using namespace cv;
using namespace std;
int DEFAULT_FRAME_WIDTH = 640;
int DEFAULT_FRAME_HEIGHT = 480;
double DEFAULT_DIST_COEFFS[5] = {-0.442373, 0.347708, 0.000532287, 0.0078344, -0.263354};
double DEFAULT_CAMERA_MATRIX[9] = {698.934, 0, 275.679, 0, 699.571, 249.775, 0, 0, 1};
CVCalibration::CVCalibration(Size _chessboardDimensions,
float _chessboardTileSize) {
chessboardDimensions = _chessboardDimensions;
chessboardTileSize = _chessboardTileSize;
}
void CVCalibration::createKnownBoardPosition(vector<Point3f> &corners) {
for (int r = 0; r < chessboardDimensions.height; r++) {
for (int c = 0; c < chessboardDimensions.width; c++) {
corners.emplace_back(c * chessboardTileSize, r * chessboardTileSize,
0.0f);
}
}
}
void CVCalibration::getCornersFromImages(bool showResults) {
for (auto iter = calibrationImages.begin(); iter != calibrationImages.end();
iter++) {
vector<Point2f> pointBuf;
bool found = findChessboardCorners(*iter, chessboardDimensions, pointBuf,
CALIB_CB_ADAPTIVE_THRESH |
CALIB_CB_NORMALIZE_IMAGE);
if (found) {
allFoundCorners.push_back(pointBuf);
}
if (showResults) {
drawChessboardCorners(*iter, chessboardDimensions, pointBuf, found);
imshow("Chessboard Corners", *iter);
waitKey(0);
}
}
}
void CVCalibration::getCameraMatrices() {
vector<vector<Point2f>> chessboardImageSpacePoints;
getCornersFromImages();
vector<vector<Point3f>> worldSpaceCornerPoints(1);
createKnownBoardPosition(worldSpaceCornerPoints[0]);
worldSpaceCornerPoints.resize(chessboardImageSpacePoints.size(),
worldSpaceCornerPoints[0]);
vector<Mat> rVectors, tVectors;
distCoeffs = Mat::zeros(8, 1, CV_64F);
calibrateCamera(worldSpaceCornerPoints, chessboardImageSpacePoints,
chessboardDimensions, cameraMatrix, distCoeffs, rVectors,
tVectors);
calibrated = true;
}
bool CVCalibration::startStreamingCalibration(VideoCapture vid, string window) {
Mat frame, drawToFrame;
while (true) {
if (!vid.read(frame))
break;
vector<Vec2f> foundPoints;
bool found = findChessboardCorners(frame, chessboardDimensions, foundPoints,
CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);
frame.copyTo(drawToFrame);
drawChessboardCorners(drawToFrame, chessboardDimensions, foundPoints,
found);
imshow(window, found ? drawToFrame : frame);
char character = static_cast<char>(waitKey(1000 / framesPerSecond));
switch (character) {
case ' ': // Space key -> save image
if (found) {
Mat temp;
frame.copyTo(temp);
calibrationImages.push_back(temp);
cout << "Found Chessboard; Calibration Images Count: "
<< calibrationImages.size() << endl;
}
case 13: // Return key -> run calibration
if (calibrationImages.size() > 20) {
getCameraMatrices();
}
break;
case 27: // Esc key -> exit prog.
return 0;
default:
break;
}
}
return 1;
}
bool CVCalibration::isCalibrated() {
return calibrated;
}
bool CVCalibration::saveCalibrationMatrices(string fname) {
ofstream calibrationFile(fname);
if (calibrationFile) {
for (int r = 0; r < cameraMatrix.rows; r++) {
for (int c = 0; c < cameraMatrix.cols; c++) {
auto val = cameraMatrix.at<double>(r, c);
calibrationFile << val << endl;
}
}
for (int r = 0; r < distCoeffs.rows; r++) {
for (int c = 0; c < distCoeffs.cols; c++) {
auto val = distCoeffs.at<double>(r, c);
calibrationFile << val << endl;
}
}
}
calibrationFile.close();
return true;
}
bool CVCalibration::loadCalibrationMatrices(string fname) {
ifstream calibrationFile(fname);
string line;
if (calibrationFile.is_open()) {
getline(calibrationFile, line);
frameWidth = stoi(line);
getline(calibrationFile, line);
frameHeight = stoi(line);
for (int r = 0; r < cameraMatrix.rows; r++) {
for (int c = 0; c < cameraMatrix.cols; c++) {
getline(calibrationFile, line);
cameraMatrix.at<double>(r, c) = stod(line);
}
}
for (int r = 0; r < distCoeffs.rows; r++) {
for (int c = 0; c < distCoeffs.cols; c++) {
getline(calibrationFile, line);
distCoeffs.at<double>(r, c) = stod(line);
}
}
calibrationFile.close();
return true;
}
return false;
}
CVCalibration::CVCalibration(string fname) {
if (!loadCalibrationMatrices(fname)) {
cerr << "Invalid calibration file, using default params for ELP_CAM (640x480)\n";
cerr.flush();
}
}