41
41
#include < opencv2/calib3d/calib3d.hpp>
42
42
#include < opencv2/core/core_c.h>
43
43
44
- static ARdouble getSizeFactor (ARdouble dist_factor[], int xsize, int ysize, int dist_function_version);
45
- static void convParam ( float intr[ 3 ][ 4 ], float dist[ 4 ], int xsize, int ysize, ARParam *param );
44
+ static void convParam ( const float intr[ 3 ][ 4 ], const float dist[AR_DIST_FACTOR_NUM_MAX], const int xsize, const int ysize, const int dist_function_version, ARParam *param );
45
+ static ARdouble getSizeFactor (ARdouble const dist_factor[AR_DIST_FACTOR_NUM_MAX ], const int xsize, const int ysize, const int dist_function_version );
46
46
47
47
static void calcChessboardCorners (const Calibration::CalibrationPatternType patternType, cv::Size patternSize, float patternSpacing, std::vector<cv::Point3f>& corners)
48
48
{
@@ -74,20 +74,28 @@ void calc(const int capturedImageNum,
74
74
const std::vector<std::vector<cv::Point2f> >& cornerSet,
75
75
const int width,
76
76
const int height,
77
+ const int dist_function_version,
77
78
ARParam *param_out,
78
79
ARdouble *err_min_out,
79
80
ARdouble *err_avg_out,
80
81
ARdouble *err_max_out)
81
82
{
82
- int i, j, k;
83
+ if (dist_function_version != 5 && dist_function_version != 4 ) {
84
+ ARLOGe (" Unsupported distortion function version %d.\n " , dist_function_version);
85
+ return ;
86
+ }
83
87
84
- // Options .
88
+ // Set version .
85
89
int flags = 0 ;
90
+ cv::Mat distortionCoeff;
91
+ if (dist_function_version == 5 ) {
92
+ distortionCoeff = cv::Mat::zeros (12 , 1 , CV_64F);
93
+ flags |= cv::CALIB_RATIONAL_MODEL|cv::CALIB_THIN_PRISM_MODEL;
94
+ } else /* dist_function_version == 4 */ {
95
+ distortionCoeff = cv::Mat::zeros (5 , 1 , CV_64F);
96
+ flags |= cv::CALIB_FIX_K3;
97
+ }
86
98
double aspectRatio = 1.0 ;
87
- // flags |= cv::CALIB_USE_INTRINSIC_GUESS;
88
- // flags |= cv::CALIB_FIX_ASPECT_RATIO;
89
- // flags |= cv::CALIB_FIX_PRINCIPAL_POINT;
90
- // flags |= cv::CALIB_ZERO_TANGENT_DIST;
91
99
92
100
// Set up object points.
93
101
std::vector<std::vector<cv::Point3f> > objectPoints (1 );
@@ -98,12 +106,11 @@ void calc(const int capturedImageNum,
98
106
if (flags & cv::CALIB_FIX_ASPECT_RATIO)
99
107
intrinsics.at <double >(0 ,0 ) = aspectRatio;
100
108
101
- cv::Mat distortionCoeff = cv::Mat::zeros (4 , 1 , CV_64F);
102
109
std::vector<cv::Mat> rotationVectors;
103
110
std::vector<cv::Mat> translationVectors;
104
111
105
112
double rms = calibrateCamera (objectPoints, cornerSet, cv::Size (width, height), intrinsics,
106
- distortionCoeff, rotationVectors, translationVectors, flags|cv::CALIB_FIX_K3|cv::CALIB_FIX_K4|cv::CALIB_FIX_K5 );
113
+ distortionCoeff, rotationVectors, translationVectors, flags);
107
114
108
115
ARLOGi (" RMS error reported by calibrateCamera: %g\n " , rms);
109
116
@@ -112,19 +119,22 @@ void calc(const int capturedImageNum,
112
119
113
120
114
121
float intr[3 ][4 ];
115
- float dist[4 ];
122
+ float dist[AR_DIST_FACTOR_NUM_MAX ];
116
123
ARParam param;
124
+ int i, j, k;
117
125
118
126
for (j = 0 ; j < 3 ; j++) {
119
127
for (i = 0 ; i < 3 ; i++) {
120
128
intr[j][i] = (float )intrinsics.at <double >(j, i);
121
129
}
122
130
intr[j][3 ] = 0 .0f ;
123
131
}
124
- for (i = 0 ; i < 4 ; i++) {
125
- dist[i] = (float )distortionCoeff.at <double >(i);
132
+ if (dist_function_version == 5 ) {
133
+ for (i = 0 ; i < 12 ; i++) dist[i] = (float )distortionCoeff.at <double >(i);
134
+ } else /* dist_function_version == 4 */ {
135
+ for (i = 0 ; i < 4 ; i++) dist[i] = (float )distortionCoeff.at <double >(i);
126
136
}
127
- convParam (intr, dist, width, height, ¶m);
137
+ convParam (intr, dist, width, height, dist_function_version, ¶m);
128
138
arParamDisp (¶m);
129
139
130
140
CvMat *rotationVector;
@@ -187,24 +197,45 @@ void calc(const int capturedImageNum,
187
197
cvReleaseMat (&rotationMatrix);
188
198
}
189
199
190
- void convParam (float intr[3 ][4 ], float dist[4 ], int xsize, int ysize, ARParam *param)
200
+ static void convParam (const float intr[3 ][4 ], const float dist[AR_DIST_FACTOR_NUM_MAX ], const int xsize, const int ysize, const int dist_function_version , ARParam *param)
191
201
{
192
202
double s;
193
203
int i, j;
194
-
195
- param->dist_function_version = 4 ;
204
+
205
+ if (dist_function_version != 5 && dist_function_version != 4 ) {
206
+ ARLOGe (" Unsupported distortion function version %d.\n " , dist_function_version);
207
+ return ;
208
+ }
209
+
210
+ param->dist_function_version = dist_function_version;
196
211
param->xsize = xsize;
197
212
param->ysize = ysize;
198
213
199
- param->dist_factor [0 ] = (ARdouble)dist[0 ]; /* k1 */
200
- param->dist_factor [1 ] = (ARdouble)dist[1 ]; /* k2 */
201
- param->dist_factor [2 ] = (ARdouble)dist[2 ]; /* p1 */
202
- param->dist_factor [3 ] = (ARdouble)dist[3 ]; /* p2 */
203
- param->dist_factor [4 ] = (ARdouble)intr[0 ][0 ]; /* fx */
204
- param->dist_factor [5 ] = (ARdouble)intr[1 ][1 ]; /* fy */
205
- param->dist_factor [6 ] = (ARdouble)intr[0 ][2 ]; /* x0 */
206
- param->dist_factor [7 ] = (ARdouble)intr[1 ][2 ]; /* y0 */
207
- param->dist_factor [8 ] = (ARdouble)1.0 ; /* s */
214
+ param->dist_factor [0 ] = (ARdouble)dist[0 ]; /* k1 */
215
+ param->dist_factor [1 ] = (ARdouble)dist[1 ]; /* k2 */
216
+ param->dist_factor [2 ] = (ARdouble)dist[2 ]; /* p1 */
217
+ param->dist_factor [3 ] = (ARdouble)dist[3 ]; /* p2 */
218
+ if (dist_function_version == 5 ) {
219
+ param->dist_factor [4 ] = (ARdouble)dist[4 ]; /* k3 */
220
+ param->dist_factor [5 ] = (ARdouble)dist[5 ]; /* k4 */
221
+ param->dist_factor [6 ] = (ARdouble)dist[6 ]; /* k5 */
222
+ param->dist_factor [7 ] = (ARdouble)dist[7 ]; /* k6 */
223
+ param->dist_factor [8 ] = (ARdouble)dist[8 ]; /* s1 */
224
+ param->dist_factor [9 ] = (ARdouble)dist[9 ]; /* s2 */
225
+ param->dist_factor [10 ] = (ARdouble)dist[10 ]; /* s3 */
226
+ param->dist_factor [11 ] = (ARdouble)dist[11 ]; /* s4 */
227
+ param->dist_factor [12 ] = (ARdouble)intr[0 ][0 ]; /* fx */
228
+ param->dist_factor [13 ] = (ARdouble)intr[1 ][1 ]; /* fy */
229
+ param->dist_factor [14 ] = (ARdouble)intr[0 ][2 ]; /* cx */
230
+ param->dist_factor [15 ] = (ARdouble)intr[1 ][2 ]; /* cy */
231
+ param->dist_factor [16 ] = (ARdouble)1.0 ; /* s */
232
+ } else /* dist_function_version == 4 */ {
233
+ param->dist_factor [4 ] = (ARdouble)intr[0 ][0 ]; /* fx */
234
+ param->dist_factor [5 ] = (ARdouble)intr[1 ][1 ]; /* fy */
235
+ param->dist_factor [6 ] = (ARdouble)intr[0 ][2 ]; /* cx */
236
+ param->dist_factor [7 ] = (ARdouble)intr[1 ][2 ]; /* cy */
237
+ param->dist_factor [8 ] = (ARdouble)1.0 ; /* s */
238
+ }
208
239
209
240
for (j = 0 ; j < 3 ; j++) {
210
241
for (i = 0 ; i < 4 ; i++) {
@@ -217,55 +248,71 @@ void convParam(float intr[3][4], float dist[4], int xsize, int ysize, ARParam *p
217
248
param->mat [0 ][1 ] /= s;
218
249
param->mat [1 ][0 ] /= s;
219
250
param->mat [1 ][1 ] /= s;
220
- param->dist_factor [8 ] = s;
251
+ if (dist_function_version == 5 ) {
252
+ param->dist_factor [16 ] = s;
253
+ } else /* dist_function_version == 4 */ {
254
+ param->dist_factor [8 ] = s;
255
+ }
221
256
}
222
257
223
- ARdouble getSizeFactor (ARdouble dist_factor[], int xsize, int ysize, int dist_function_version)
258
+ static ARdouble getSizeFactor (const ARdouble dist_factor[AR_DIST_FACTOR_NUM_MAX ], const int xsize, const int ysize, const int dist_function_version)
224
259
{
225
260
ARdouble ox, oy, ix, iy;
226
261
ARdouble olen, ilen;
227
262
ARdouble sf, sf1;
263
+ ARdouble cx, cy;
264
+
265
+ if (dist_function_version == 5 ) {
266
+ cx = dist_factor[14 ];
267
+ cy = dist_factor[15 ];
268
+ } else if (dist_function_version == 4 ) {
269
+ cx = dist_factor[6 ];
270
+ cy = dist_factor[7 ];
271
+ } else {
272
+ ARLOGe (" Unsupported distortion function version %d.\n " , dist_function_version);
273
+ return 1.0 ;
274
+ }
228
275
229
276
sf = 100 .0f ;
230
277
231
278
ox = 0 .0f ;
232
- oy = dist_factor[ 7 ] ;
233
- olen = dist_factor[ 6 ] ;
279
+ oy = cy ;
280
+ olen = cx ;
234
281
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
235
- ilen = dist_factor[ 6 ] - ix;
282
+ ilen = cx - ix;
236
283
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
237
284
if (ilen > 0 .0f ) {
238
285
sf1 = ilen / olen;
239
286
if (sf1 < sf) sf = sf1;
240
287
}
241
288
242
289
ox = xsize;
243
- oy = dist_factor[ 7 ] ;
244
- olen = xsize - dist_factor[ 6 ] ;
290
+ oy = cy ;
291
+ olen = xsize - cx ;
245
292
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
246
- ilen = ix - dist_factor[ 6 ] ;
293
+ ilen = ix - cx ;
247
294
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
248
295
if (ilen > 0 .0f ) {
249
296
sf1 = ilen / olen;
250
297
if (sf1 < sf) sf = sf1;
251
298
}
252
299
253
- ox = dist_factor[ 6 ] ;
300
+ ox = cx ;
254
301
oy = 0.0 ;
255
- olen = dist_factor[ 7 ] ;
302
+ olen = cy ;
256
303
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
257
- ilen = dist_factor[ 7 ] - iy;
304
+ ilen = cy - iy;
258
305
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
259
306
if (ilen > 0 .0f ) {
260
307
sf1 = ilen / olen;
261
308
if (sf1 < sf) sf = sf1;
262
309
}
263
310
264
- ox = dist_factor[ 6 ] ;
311
+ ox = cx ;
265
312
oy = ysize;
266
- olen = ysize - dist_factor[ 7 ] ;
313
+ olen = ysize - cy ;
267
314
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
268
- ilen = iy - dist_factor[ 7 ] ;
315
+ ilen = iy - cy ;
269
316
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
270
317
if (ilen > 0 .0f ) {
271
318
sf1 = ilen / olen;
@@ -276,15 +323,15 @@ ARdouble getSizeFactor(ARdouble dist_factor[], int xsize, int ysize, int dist_fu
276
323
ox = 0 .0f ;
277
324
oy = 0 .0f ;
278
325
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
279
- ilen = dist_factor[ 6 ] - ix;
280
- olen = dist_factor[ 6 ] ;
326
+ ilen = cx - ix;
327
+ olen = cx ;
281
328
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
282
329
if (ilen > 0 .0f ) {
283
330
sf1 = ilen / olen;
284
331
if (sf1 < sf) sf = sf1;
285
332
}
286
- ilen = dist_factor[ 7 ] - iy;
287
- olen = dist_factor[ 7 ] ;
333
+ ilen = cy - iy;
334
+ olen = cy ;
288
335
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
289
336
if (ilen > 0 .0f ) {
290
337
sf1 = ilen / olen;
@@ -294,15 +341,15 @@ ARdouble getSizeFactor(ARdouble dist_factor[], int xsize, int ysize, int dist_fu
294
341
ox = xsize;
295
342
oy = 0 .0f ;
296
343
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
297
- ilen = ix - dist_factor[ 6 ] ;
298
- olen = xsize - dist_factor[ 6 ] ;
344
+ ilen = ix - cx ;
345
+ olen = xsize - cx ;
299
346
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
300
347
if (ilen > 0 .0f ) {
301
348
sf1 = ilen / olen;
302
349
if (sf1 < sf) sf = sf1;
303
350
}
304
- ilen = dist_factor[ 7 ] - iy;
305
- olen = dist_factor[ 7 ] ;
351
+ ilen = cy - iy;
352
+ olen = cy ;
306
353
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
307
354
if (ilen > 0 .0f ) {
308
355
sf1 = ilen / olen;
@@ -312,15 +359,15 @@ ARdouble getSizeFactor(ARdouble dist_factor[], int xsize, int ysize, int dist_fu
312
359
ox = 0 .0f ;
313
360
oy = ysize;
314
361
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
315
- ilen = dist_factor[ 6 ] - ix;
316
- olen = dist_factor[ 6 ] ;
362
+ ilen = cx - ix;
363
+ olen = cx ;
317
364
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
318
365
if (ilen > 0 .0f ) {
319
366
sf1 = ilen / olen;
320
367
if (sf1 < sf) sf = sf1;
321
368
}
322
- ilen = iy - dist_factor[ 7 ] ;
323
- olen = ysize - dist_factor[ 7 ] ;
369
+ ilen = iy - cy ;
370
+ olen = ysize - cy ;
324
371
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
325
372
if (ilen > 0 .0f ) {
326
373
sf1 = ilen / olen;
@@ -330,15 +377,15 @@ ARdouble getSizeFactor(ARdouble dist_factor[], int xsize, int ysize, int dist_fu
330
377
ox = xsize;
331
378
oy = ysize;
332
379
arParamObserv2Ideal (dist_factor, ox, oy, &ix, &iy, dist_function_version);
333
- ilen = ix - dist_factor[ 6 ] ;
334
- olen = xsize - dist_factor[ 6 ] ;
380
+ ilen = ix - cx ;
381
+ olen = xsize - cx ;
335
382
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
336
383
if (ilen > 0 .0f ) {
337
384
sf1 = ilen / olen;
338
385
if (sf1 < sf) sf = sf1;
339
386
}
340
- ilen = iy - dist_factor[ 7 ] ;
341
- olen = ysize - dist_factor[ 7 ] ;
387
+ ilen = iy - cy ;
388
+ olen = ysize - cy ;
342
389
// ARPRINT("Olen = %f, Ilen = %f, s = %f\n", olen, ilen, ilen / olen);
343
390
if (ilen > 0 .0f ) {
344
391
sf1 = ilen / olen;
0 commit comments