This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
calc.cpp
339 lines (296 loc) · 11.6 KB
/
calc.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
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
335
336
337
338
/*
* calc.cpp
* ARToolKit6
*
* This file is part of ARToolKit.
*
* Copyright 2015-2017 Daqri LLC. All Rights Reserved.
* Copyright 2012-2015 ARToolworks, Inc. All Rights Reserved.
*
* Author(s): Philip Lamb, Hirokazu Kato
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "calc.hpp"
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core_c.h>
static ARdouble getSizeFactor(ARdouble dist_factor[], int xsize, int ysize, int dist_function_version);
static void convParam(float intr[3][4], float dist[4], int xsize, int ysize, ARParam *param);
static void calcChessboardCorners(const Calibration::CalibrationPatternType patternType, cv::Size patternSize, float patternSpacing, std::vector<cv::Point3f>& corners)
{
corners.resize(0);
switch (patternType) {
case Calibration::CalibrationPatternType::CHESSBOARD:
case Calibration::CalibrationPatternType::CIRCLES_GRID:
for (int j = 0; j < patternSize.height; j++)
for (int i = 0; i < patternSize.width; i++)
corners.push_back(cv::Point3f(float(i*patternSpacing), float(j*patternSpacing), 0));
break;
case Calibration::CalibrationPatternType::ASYMMETRIC_CIRCLES_GRID:
for (int j = 0; j < patternSize.height; j++)
for (int i = 0; i < patternSize.width; i++)
corners.push_back(cv::Point3f(float((2*i + j % 2)*patternSpacing), float(j*patternSpacing), 0));
break;
default:
ARLOGe("Unknown pattern type.\n");
}
}
void calc(const int capturedImageNum,
const Calibration::CalibrationPatternType patternType,
const cv::Size patternSize,
const float patternSpacing,
const std::vector<std::vector<cv::Point2f> >& cornerSet,
const int width,
const int height,
ARParam *param_out,
ARdouble *err_min_out,
ARdouble *err_avg_out,
ARdouble *err_max_out)
{
int i, j, k;
// Options.
int flags = 0;
double aspectRatio = 1.0;
//flags |= cv::CALIB_USE_INTRINSIC_GUESS;
//flags |= cv::CALIB_FIX_ASPECT_RATIO;
//flags |= cv::CALIB_FIX_PRINCIPAL_POINT;
//flags |= cv::CALIB_ZERO_TANGENT_DIST;
// Set up object points.
std::vector<std::vector<cv::Point3f> > objectPoints(1);
calcChessboardCorners(patternType, patternSize, patternSpacing, objectPoints[0]);
objectPoints.resize(capturedImageNum, objectPoints[0]);
cv::Mat intrinsics = cv::Mat::eye(3, 3, CV_64F);
if (flags & cv::CALIB_FIX_ASPECT_RATIO)
intrinsics.at<double>(0,0) = aspectRatio;
cv::Mat distortionCoeff = cv::Mat::zeros(4, 1, CV_64F);
std::vector<cv::Mat> rotationVectors;
std::vector<cv::Mat> translationVectors;
double rms = calibrateCamera(objectPoints, cornerSet, cv::Size(width, height), intrinsics,
distortionCoeff, rotationVectors, translationVectors, flags|cv::CALIB_FIX_K3|cv::CALIB_FIX_K4|cv::CALIB_FIX_K5);
ARLOGi("RMS error reported by calibrateCamera: %g\n", rms);
bool ok = checkRange(intrinsics) && checkRange(distortionCoeff);
if (!ok) ARLOGe("cv::checkRange(intrinsics) && cv::checkRange(distortionCoeff) reported not OK.\n");
float intr[3][4];
float dist[4];
ARParam param;
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
intr[j][i] = (float)intrinsics.at<double>(j, i);
}
intr[j][3] = 0.0f;
}
for (i = 0; i < 4; i++) {
dist[i] = (float)distortionCoeff.at<double>(i);
}
convParam(intr, dist, width, height, ¶m);
arParamDisp(¶m);
CvMat *rotationVector;
CvMat *rotationMatrix;
double trans[3][4];
ARdouble cx, cy, cz, hx, hy, h, sx, sy, ox, oy, err;
ARdouble err_min = 1000000.0f, err_avg = 0.0f, err_max = 0.0f;
rotationVector = cvCreateMat(1, 3, CV_32FC1);
rotationMatrix = cvCreateMat(3, 3, CV_32FC1);
for (k = 0; k < capturedImageNum; k++) {
for (i = 0; i < 3; i++) {
((float *)(rotationVector->data.ptr))[i] = (float)rotationVectors.at(k).at<double>(i);
}
cvRodrigues2(rotationVector, rotationMatrix, 0);
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
trans[j][i] = ((float *)(rotationMatrix->data.ptr + rotationMatrix->step*j))[i];
}
trans[j][3] = (float)translationVectors.at(k).at<double>(j);
}
//arParamDispExt(trans);
err = 0.0;
for (i = 0; i < patternSize.width; i++) {
for (j = 0; j < patternSize.height; j++) {
float x = objectPoints[0][i * patternSize.height + j].x;
float y = objectPoints[0][i * patternSize.height + j].y;
cx = trans[0][0] * x + trans[0][1] * y + trans[0][3];
cy = trans[1][0] * x + trans[1][1] * y + trans[1][3];
cz = trans[2][0] * x + trans[2][1] * y + trans[2][3];
hx = param.mat[0][0] * cx + param.mat[0][1] * cy + param.mat[0][2] * cz + param.mat[0][3];
hy = param.mat[1][0] * cx + param.mat[1][1] * cy + param.mat[1][2] * cz + param.mat[1][3];
h = param.mat[2][0] * cx + param.mat[2][1] * cy + param.mat[2][2] * cz + param.mat[2][3];
if (h == 0.0) continue;
sx = hx / h;
sy = hy / h;
arParamIdeal2Observ(param.dist_factor, sx, sy, &ox, &oy, param.dist_function_version);
sx = (ARdouble)cornerSet[k][i * patternSize.height + j].x;
sy = (ARdouble)cornerSet[k][i * patternSize.height + j].y;
err += (ox - sx)*(ox - sx) + (oy - sy)*(oy - sy);
}
}
err = sqrtf(err/(patternSize.width*patternSize.height));
ARLOG("Err[%2d]: %f[pixel]\n", k + 1, err);
// Track min, avg, and max error.
if (err < err_min) err_min = err;
err_avg += err;
if (err > err_max) err_max = err;
}
err_avg /= (ARdouble)(capturedImageNum + 1);
*err_min_out = err_min;
*err_avg_out = err_avg;
*err_max_out = err_max;
*param_out = param;
cvReleaseMat(&rotationVector);
cvReleaseMat(&rotationMatrix);
}
void convParam(float intr[3][4], float dist[4], int xsize, int ysize, ARParam *param)
{
double s;
int i, j;
param->dist_function_version = 4;
param->xsize = xsize;
param->ysize = ysize;
param->dist_factor[0] = (ARdouble)dist[0]; /* k1 */
param->dist_factor[1] = (ARdouble)dist[1]; /* k2 */
param->dist_factor[2] = (ARdouble)dist[2]; /* p1 */
param->dist_factor[3] = (ARdouble)dist[3]; /* p2 */
param->dist_factor[4] = (ARdouble)intr[0][0]; /* fx */
param->dist_factor[5] = (ARdouble)intr[1][1]; /* fy */
param->dist_factor[6] = (ARdouble)intr[0][2]; /* x0 */
param->dist_factor[7] = (ARdouble)intr[1][2]; /* y0 */
param->dist_factor[8] = (ARdouble)1.0; /* s */
for (j = 0; j < 3; j++) {
for (i = 0; i < 4; i++) {
param->mat[j][i] = (ARdouble)intr[j][i];
}
}
s = getSizeFactor(param->dist_factor, xsize, ysize, param->dist_function_version);
param->mat[0][0] /= s;
param->mat[0][1] /= s;
param->mat[1][0] /= s;
param->mat[1][1] /= s;
param->dist_factor[8] = s;
}
ARdouble getSizeFactor(ARdouble dist_factor[], int xsize, int ysize, int dist_function_version)
{
ARdouble ox, oy, ix, iy;
ARdouble olen, ilen;
ARdouble sf, sf1;
sf = 100.0f;
ox = 0.0f;
oy = dist_factor[7];
olen = dist_factor[6];
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = dist_factor[6] - ix;
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = xsize;
oy = dist_factor[7];
olen = xsize - dist_factor[6];
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = ix - dist_factor[6];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = dist_factor[6];
oy = 0.0;
olen = dist_factor[7];
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = dist_factor[7] - iy;
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = dist_factor[6];
oy = ysize;
olen = ysize - dist_factor[7];
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = iy - dist_factor[7];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = 0.0f;
oy = 0.0f;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = dist_factor[6] - ix;
olen = dist_factor[6];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = dist_factor[7] - iy;
olen = dist_factor[7];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = xsize;
oy = 0.0f;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = ix - dist_factor[6];
olen = xsize - dist_factor[6];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = dist_factor[7] - iy;
olen = dist_factor[7];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = 0.0f;
oy = ysize;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = dist_factor[6] - ix;
olen = dist_factor[6];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = iy - dist_factor[7];
olen = ysize - dist_factor[7];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ox = xsize;
oy = ysize;
arParamObserv2Ideal(dist_factor, ox, oy, &ix, &iy, dist_function_version);
ilen = ix - dist_factor[6];
olen = xsize - dist_factor[6];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
ilen = iy - dist_factor[7];
olen = ysize - dist_factor[7];
//ARLOG("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
if (ilen > 0.0f) {
sf1 = ilen / olen;
if (sf1 < sf) sf = sf1;
}
if (sf == 100.0f) sf = 1.0f;
return sf;
}