calib_dist.cpp
Go to the documentation of this file.
1 #include <math.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <malloc.h>
5 //#include <ARToolKitPlus/param.h>
6 //#include <ARToolKitPlus/matrix.h>
7 
9 
10 #include "calib_camera.h"
11 
13 static ARFloat check_error( ARFloat *x, ARFloat *y, int num, ARFloat dist_factor[4] );
15 static ARFloat get_size_factor( ARFloat dist_factor[4], int xsize, int ysize );
16 
18 {
19  int i, j;
20  ARFloat bx, by;
21  ARFloat bf[4];
22  ARFloat error, min;
23  ARFloat factor[4];
24 
25  bx = xsize / (ARFloat)2;
26  by = ysize / (ARFloat)2;
27  factor[0] = bx;
28  factor[1] = by;
29  factor[3] = (ARFloat)1.0;
30  min = calc_distortion2( patt, factor );
31  bf[0] = factor[0];
32  bf[1] = factor[1];
33  bf[2] = factor[2];
34  bf[3] = 1.0;
35 printf("[%5.1f, %5.1f, %5.1f] %f\n", bf[0], bf[1], bf[2], min);
36  for( j = -10; j <= 10; j++ ) {
37  factor[1] = by + j*5;
38  for( i = -10; i <= 10; i++ ) {
39  factor[0] = bx + i*5;
40  error = calc_distortion2( patt, factor );
41  if( error < min ) { bf[0] = factor[0]; bf[1] = factor[1];
42  bf[2] = factor[2]; min = error; }
43  }
44 printf("[%5.1f, %5.1f, %5.1f] %f\n", bf[0], bf[1], bf[2], min);
45  }
46 
47  bx = bf[0];
48  by = bf[1];
49  for( j = -10; j <= 10; j++ ) {
50  factor[1] = by + (ARFloat)0.5 * j;
51  for( i = -10; i <= 10; i++ ) {
52  factor[0] = bx + (ARFloat)0.5 * i;
53  error = calc_distortion2( patt, factor );
54  if( error < min ) { bf[0] = factor[0]; bf[1] = factor[1];
55  bf[2] = factor[2]; min = error; }
56  }
57 printf("[%5.1f, %5.1f, %5.1f] %f\n", bf[0], bf[1], bf[2], min);
58  }
59 
60  dist_factor[0] = bf[0];
61  dist_factor[1] = bf[1];
62  dist_factor[2] = bf[2];
63  dist_factor[3] = get_size_factor( bf, xsize, ysize );
64 }
65 
67 {
68  ARFloat ox, oy, ix, iy;
69  ARFloat olen, ilen;
70  ARFloat sf, sf1;
71 
72  sf = 100.0;
73 
74  ox = 0.0;
75  oy = dist_factor[1];
76  olen = dist_factor[0];
77  ARToolKitPlus::Tracker::arParamObserv2Ideal( dist_factor, ox, oy, &ix, &iy );
78  ilen = dist_factor[0] - ix;
79 printf("Olen = %f, Ilen = %f\n", olen, ilen);
80  if( ilen > 0 ) {
81  sf1 = ilen / olen;
82  if( sf1 < sf ) sf = sf1;
83  }
84 
85  ox = (ARFloat)xsize;
86  oy = dist_factor[1];
87  olen = xsize - dist_factor[0];
88  ARToolKitPlus::Tracker::arParamObserv2Ideal( dist_factor, ox, oy, &ix, &iy );
89  ilen = ix - dist_factor[0];
90 printf("Olen = %f, Ilen = %f\n", olen, ilen);
91  if( ilen > 0 ) {
92  sf1 = ilen / olen;
93  if( sf1 < sf ) sf = sf1;
94  }
95 
96  ox = dist_factor[0];
97  oy = 0.0;
98  olen = dist_factor[1];
99  ARToolKitPlus::Tracker::arParamObserv2Ideal( dist_factor, ox, oy, &ix, &iy );
100  ilen = dist_factor[1] - iy;
101 printf("Olen = %f, Ilen = %f\n", olen, ilen);
102  if( ilen > 0 ) {
103  sf1 = ilen / olen;
104  if( sf1 < sf ) sf = sf1;
105  }
106 
107  ox = dist_factor[0];
108  oy = (ARFloat)ysize;
109  olen = ysize - dist_factor[1];
110  ARToolKitPlus::Tracker::arParamObserv2Ideal( dist_factor, ox, oy, &ix, &iy );
111  ilen = iy - dist_factor[1];
112 printf("Olen = %f, Ilen = %f\n", olen, ilen);
113  if( ilen > 0 ) {
114  sf1 = ilen / olen;
115  if( sf1 < sf ) sf = sf1;
116  }
117 
118  if( sf == 0.0 ) sf = 1.0;
119 
120  return sf;
121 }
122 
124 {
125  ARFloat min, err, f, fb;
126  int i;
127 
128  dist_factor[2] = 0.0;
129  min = get_fitting_error( patt, dist_factor );
130 
131  f = dist_factor[2];
132  for( i = -100; i < 200; i+=10 ) {
133  dist_factor[2] = (ARFloat)i;
134  err = get_fitting_error( patt, dist_factor );
135  if( err < min ) { min = err; f = dist_factor[2]; }
136  }
137 
138  fb = f;
139  for( i = -10; i <= 10; i++ ) {
140  dist_factor[2] = fb + i;
141  //if( dist_factor[2] < 0 ) continue;
142  err = get_fitting_error( patt, dist_factor );
143  if( err < min ) { min = err; f = dist_factor[2]; }
144  }
145 
146  fb = f;
147  for( i = -10; i <= 10; i++ ) {
148  dist_factor[2] = fb + (ARFloat)0.1 * i;
149  //if( dist_factor[2] < 0 ) continue;
150  err = get_fitting_error( patt, dist_factor );
151  if( err < min ) { min = err; f = dist_factor[2]; }
152  }
153 
154  dist_factor[2] = f;
155  return min;
156 }
157 
159 {
160  ARFloat *x, *y;
161  ARFloat error;
162  int max;
163  int i, j, k, l;
164  int p, c;
165 
166  max = (patt->v_num > patt->h_num)? patt->v_num: patt->h_num;
167  x = (ARFloat *)malloc( sizeof(ARFloat)*max );
168  y = (ARFloat *)malloc( sizeof(ARFloat)*max );
169  if( x == NULL || y == NULL ) exit(0);
170 
171  error = 0.0;
172  c = 0;
173  for( i = 0; i < patt->loop_num; i++ ) {
174  for( j = 0; j < patt->v_num; j++ ) {
175  for( k = 0; k < patt->h_num; k++ ) {
176  x[k] = patt->point[i][j*patt->h_num+k].x_coord;
177  y[k] = patt->point[i][j*patt->h_num+k].y_coord;
178  }
179  error += check_error( x, y, patt->h_num, dist_factor );
180  c += patt->h_num;
181  }
182 
183  for( j = 0; j < patt->h_num; j++ ) {
184  for( k = 0; k < patt->v_num; k++ ) {
185  x[k] = patt->point[i][k*patt->h_num+j].x_coord;
186  y[k] = patt->point[i][k*patt->h_num+j].y_coord;
187  }
188  error += check_error( x, y, patt->v_num, dist_factor );
189  c += patt->v_num;
190  }
191 
192  for( j = 3 - patt->v_num; j < patt->h_num - 2; j++ ) {
193  p = 0;
194  for( k = 0; k < patt->v_num; k++ ) {
195  l = j+k;
196  if( l < 0 || l >= patt->h_num ) continue;
197  x[p] = patt->point[i][k*patt->h_num+l].x_coord;
198  y[p] = patt->point[i][k*patt->h_num+l].y_coord;
199  p++;
200  }
201  error += check_error( x, y, p, dist_factor );
202  c += p;
203  }
204 
205  for( j = 2; j < patt->h_num + patt->v_num - 3; j++ ) {
206  p = 0;
207  for( k = 0; k < patt->v_num; k++ ) {
208  l = j-k;
209  if( l < 0 || l >= patt->h_num ) continue;
210  x[p] = patt->point[i][k*patt->h_num+l].x_coord;
211  y[p] = patt->point[i][k*patt->h_num+l].y_coord;
212  p++;
213  }
214  error += check_error( x, y, p, dist_factor );
215  c += p;
216  }
217  }
218 
219  free( x );
220  free( y );
221 
222  return (ARFloat)sqrt(error/c);
223 }
224 
225 static ARFloat check_error( ARFloat *x, ARFloat *y, int num, ARFloat dist_factor[4] )
226 {
227  ARToolKitPlus::ARMat *input, *evec;
228  ARToolKitPlus::ARVec *ev, *mean;
229  ARFloat a, b, c;
230  ARFloat error;
231  int i;
232 
234  mean = ARToolKitPlus::Vector::alloc( 2 );
235  evec = ARToolKitPlus::Matrix::alloc( 2, 2 );
236 
237  input = ARToolKitPlus::Matrix::alloc( num, 2 );
238  for( i = 0; i < num; i++ ) {
239  ARToolKitPlus::Tracker::arParamObserv2Ideal( dist_factor, x[i], y[i],
240  &(input->m[i*2+0]), &(input->m[i*2+1]) );
241  }
242  if( ARToolKitPlus::Tracker::arMatrixPCA(input, evec, ev, mean) < 0 ) exit(0);
243  a = evec->m[1];
244  b = -evec->m[0];
245  c = -(a*mean->v[0] + b*mean->v[1]);
246 
247  error = 0.0;
248  for( i = 0; i < num; i++ ) {
249  error += (a*input->m[i*2+0] + b*input->m[i*2+1] + c)
250  * (a*input->m[i*2+0] + b*input->m[i*2+1] + c);
251  }
252  error /= (a*a + b*b);
253 
258 
259  return error;
260 }
ARFloat * v
Definition: vector.h:55
ARFloat * m
Definition: matrix.h:65
void calc_distortion(CALIB_PATT_T *patt, int xsize, int ysize, ARFloat dist_factor[4])
Definition: calib_dist.cpp:17
f
static int free(ARVec *v)
static int free(ARMat *m)
ARFloat x_coord
Definition: calib_camera.h:10
TFSIMD_FORCE_INLINE const tfScalar & y() const
static ARFloat get_fitting_error(CALIB_PATT_T *patt, ARFloat dist_factor[4])
Definition: calib_dist.cpp:158
static ARVec * alloc(int clm)
ARFloat y_coord
Definition: calib_camera.h:11
static ARFloat calc_distortion2(CALIB_PATT_T *patt, ARFloat dist_factor[4])
Definition: calib_dist.cpp:123
int h_num
Definition: calib_camera.h:18
ARFloat dist_factor[4]
TFSIMD_FORCE_INLINE const tfScalar & x() const
CALIB_COORD_T * point[LOOP_MAX]
Definition: calib_camera.h:17
#define NULL
Definition: PocketKnife.h:38
#define min(a, b)
Definition: rpp_quintic.cpp:45
int v_num
Definition: calib_camera.h:19
float ARFloat
Definition: config.h:60
int loop_num
Definition: calib_camera.h:20
#define max(a, b)
Definition: rpp_quintic.cpp:44
static ARFloat check_error(ARFloat *x, ARFloat *y, int num, ARFloat dist_factor[4])
Definition: calib_dist.cpp:225
static ARMat * alloc(int row, int clm)
static ARFloat get_size_factor(ARFloat dist_factor[4], int xsize, int ysize)
Definition: calib_dist.cpp:66


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33