tools/CameraCalib/src/main.cpp
Go to the documentation of this file.
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include <GL/gl.h>
7 #include <GL/glut.h>
8 //#include <AR/gsub.h>
9 //#include <ARToolKitPlus/param.h>
10 //#include <ARToolKitPlus/ar.h>
11 //#include <ARToolKitPlus/matrix.h>
12 //#include <AR/ARFrameGrabber.h>
13 
14 #include <ARToolKitPlus/Tracker.h>
15 
16 #include "ARFileGrabber.h"
17 #include "calib_camera.h"
18 
19 //#define USE_TEXMAP 0
20 
21 #ifndef GL_ABGR
22 #define GL_ABGR GL_ABGR_EXT
23 #endif
24 #ifndef GL_BGRA
25 #define GL_BGRA GL_BGRA_EXT
26 #endif
27 #ifndef GL_BGR
28 #define GL_BGR GL_BGR_EXT
29 #endif
30 
31 int win;
32 char *vconf = "";
33 int xsize, ysize;
34 int thresh = THRESH;
35 int count = 0;
36 unsigned char *clipImage;
37 
40 ARFloat mat[3][4];
41 
43 int sx, sy, ex, ey;
44 
45 int status;
47 
48 #ifdef USE_TEXMAP
49 static GLuint glid[2];
50 static int tex1Xsize1 = 1;
51 static int tex1Xsize2 = 1;
52 static int tex1Ysize = 1;
53 static void dispImageTex1( unsigned char *pimage );
54 static void dispImageTex2( unsigned char *pimage );
55 #endif
56 
57 static void init(void);
58 static void mouseEvent(int button, int state, int x, int y);
59 static void motionEvent( int x, int y );
60 static void keyEvent(unsigned char key, int x, int y);
61 static void dispImage(void);
62 static void dispImage2( unsigned char *pimage );
63 static void dispClipImage( int sx, int sy, int xsize, int ysize, ARToolKitPlus::ARUint8 *clipImage );
64 static void draw_warp_line( ARFloat a, ARFloat b , ARFloat c );
65 static void draw_line(void);
66 static void draw_line2( ARFloat *x, ARFloat *y, int num );
67 static void draw_warp_line( ARFloat a, ARFloat b , ARFloat c );
68 static void print_comment( int status );
69 static void save_param(void);
70 
71 
72 #pragma message("WARNING: make sure that you compile ARToolKitPlus with:")
73 #pragma message(" - no 8-bit image support")
74 #pragma message(" - ARFloat as double")
75 #pragma message("--> otherwise linker errors will occur")
76 
77 // we don't use ARFrameGrabber...
78 //
79 // ARFrameGrabber camera;
80 
81 // this camera calib version uses the ARFileGrabber class
82 // instead of ARFrameGrabber. which will load files as specified
83 // in the constructor...
84 // (filename pattern, width, height, bytes_per_component)
85 //
86 #define CAM_W 640
87 #define CAM_H 480
88 ARFileGrabber camera("dump_%02d.raw", CAM_W,CAM_H,4);
89 
91 
92 static void save_param(void)
93 {
94  char name[256];
96  int i, j;
97 
98  param.xsize = xsize;
99  param.ysize = ysize;
100  for( i = 0; i < 4; i++ ) param.dist_factor[i] = dist_factor[i];
101  for( j = 0; j < 3; j++ ) {
102  for( i = 0; i < 4; i++ ) {
103  param.mat[j][i] = mat[j][i];
104  }
105  }
106 
107  ARToolKitPlus::Param::display( &param );
108 
109  printf("Filename: ");
110  scanf( "%s", name ); while( getchar()!= '\n' );
111  ARToolKitPlus::Tracker::arParamSave( name, 1, &param );
112 
113  return;
114 }
115 
116 int main()
117 {
118  if(ARToolKitPlus::getPixelFormat()!=ARToolKitPlus::PIXEL_FORMAT_ABGR &&
119  ARToolKitPlus::getPixelFormat()!=ARToolKitPlus::PIXEL_FORMAT_BGRA &&
120  ARToolKitPlus::getPixelFormat()!=ARToolKitPlus::PIXEL_FORMAT_BGR)
121  {
122  printf("ERROR: CameraCalib needs one of: PIXEL_FORMAT_ABGR, PIXEL_FORMAT_BGRA or PIXEL_FORMAT_BGR\n");
123  exit(-1);
124  }
125 
127  {
128  printf("ERROR: CameraCalib needs double precision\n");
129  exit(-1);
130  }
131 
132 
133 
134  CoInitialize(NULL);
135  //initialize applications
136  init();
137 
138  glutKeyboardFunc(keyEvent);
139  glutMouseFunc(mouseEvent);
140  glutMotionFunc(motionEvent);
141  glutIdleFunc(dispImage);
142  glutDisplayFunc(dispImage);
143 
144  print_comment(0);
145  status = 0;
146  point_num = 0;
147 
148  //start the video capture
149  //camera.Init(0);
150  //camera.DisplayProperties();
151  //camera.SetFlippedImage(true);
152 
153  //start the main event loop
154  glutMainLoop();
155  CoUninitialize();
156  return 0;
157 }
158 
159 static void init(void)
160 {
161  ARFloat length;
162  int i, j;
163 
164  patt.h_num = H_NUM;
165  patt.v_num = V_NUM;
166  patt.loop_num = 0;
167  if( patt.h_num < 3 || patt.v_num < 3 ) exit(0);
168 
169  printf("Input the length between each markers: ");
170  scanf("%lf", &length); while( getchar()!='\n' );
171  patt.world_coord = (CALIB_COORD_T *)malloc( sizeof(CALIB_COORD_T)*patt.h_num*patt.v_num );
172  for( j = 0; j < patt.v_num; j++ ) {
173  for( i = 0; i < patt.h_num; i++ ) {
174  patt.world_coord[j*patt.h_num+i].x_coord = length * i;
175  patt.world_coord[j*patt.h_num+i].y_coord = length * j;
176  }
177  }
178 
179  /* Note: the video image captured is hard coded to CAM_WxCAM_H */
180  xsize = CAM_W; ysize = CAM_H;
181  printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
182 
183  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
184  glutInitWindowSize(xsize, ysize);
185  glutInitWindowPosition(100,100);
186  win = glutCreateWindow("Calib distotion param");
187 
188  glMatrixMode(GL_MODELVIEW);
189  glLoadIdentity();
190  glMatrixMode(GL_PROJECTION);
191  glLoadIdentity();
192  glOrtho(-0.5, xsize-0.5, -0.5, ysize-0.5, -1.0, 1.0);
193  glViewport(0, 0, xsize, ysize);
194 
195  clipImage = (unsigned char *)malloc( xsize*ysize*PIX_SIZE );
196  if( clipImage == NULL ) exit(0);
197 
198 #ifdef USE_TEXMAP
199  glGenTextures(2, glid);
200  glBindTexture( GL_TEXTURE_2D, glid[0] );
201  glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
202  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
203  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
204  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
205  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
206  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
207  glBindTexture( GL_TEXTURE_2D, glid[1] );
208  glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
209  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
210  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
211  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
212  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
213  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
214  if( xsize > 512 ) {
215  tex1Xsize1 = 512;
216  tex1Xsize2 = 1;
217  while( tex1Xsize2 < xsize - tex1Xsize1 ) tex1Xsize2 *= 2;
218  }
219  else {
220  tex1Xsize1 = 1;
221  while( tex1Xsize1 < xsize ) tex1Xsize1 *= 2;
222  }
223  tex1Ysize = 1;
224  while( tex1Ysize < ysize ) tex1Ysize *= 2;
225 #endif
226 }
227 
228 static void motionEvent( int x, int y )
229 {
230  unsigned char *p, *p1;
231  int ssx, ssy, eex, eey;
232  int i, j, k;
233 
234  if( status == 1 && sx != -1 && sy != -1 ) {
235  ex = x;
236  ey = y;
237 
238  if( sx < ex ) { ssx = sx; eex = ex; }
239  else { ssx = ex; eex = sx; }
240  if( sy < ey ) { ssy = sy; eey = ey; }
241  else { ssy = ey; eey = sy; }
242  p1 = clipImage;
243  for( j = ssy; j <= eey; j++ ) {
244  p = &(patt.savedImage[patt.loop_num-1][(j*xsize+ssx)*PIX_SIZE]);
245  for( i = ssx; i <= eex; i++ ) {
246 #ifdef AR_PIX_FORMAT_BGRA
247  k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
248  if( k < thresh ) k = 0;
249  else k = 255;
250  *(p1+0) = *(p1+1) = *(p1+2) = k;
251 #endif
252 #ifdef AR_PIX_FORMAT_ABGR
253  k = (255*3 - (*(p+1) + *(p+2) + *(p+3))) / 3;
254  if( k < thresh ) k = 0;
255  else k = 255;
256  *(p1+1) = *(p1+2) = *(p1+3) = k;
257 #endif
258 #ifdef AR_PIX_FORMAT_BGR
259  k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
260  if( k < thresh ) k = 0;
261  else k = 255;
262  *(p1+0) = *(p1+1) = *(p1+2) = k;
263 #endif
264  p += PIX_SIZE;
265  p1 += PIX_SIZE;
266  }
267  }
268  }
269 }
270 
271 static void mouseEvent(int button, int state, int x, int y)
272 {
273  unsigned char *p, *p1;
274  int ssx, ssy, eex, eey;
275  int i, j, k;
276 
277  if( button == GLUT_RIGHT_BUTTON && state == GLUT_UP ) {
278  if( status == 0 ) {
279  if( patt.loop_num > 0 ) {
281  printf("--------------\n");
282  printf("Center X: %f\n", dist_factor[0]);
283  printf(" Y: %f\n", dist_factor[1]);
284  printf("Dist Factor: %f\n", dist_factor[2]);
285  printf("Size Adjust: %f\n", dist_factor[3]);
286  printf("--------------\n");
287  status = 2;
288  check_num = 0;
289  print_comment(5);
290  }
291  else {
292  glutDestroyWindow( win );
293  exit(0);
294  }
295  }
296  else if( status == 1 ) {
297  if( patt.loop_num == 0 ) {printf("error!!\n"); exit(0);}
298  patt.loop_num--;
299  free( patt.point[patt.loop_num] );
300  free( patt.savedImage[patt.loop_num] );
301  status = 0;
302  point_num = 0;
303 
304  if( patt.loop_num == 0 ) print_comment(0);
305  else print_comment(4);
306  }
307  }
308 
309  if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {
310  if( status == 1 && point_num < patt.h_num*patt.v_num ) {
311  sx = ex = x;
312  sy = ey = y;
313 
314  p = &(patt.savedImage[patt.loop_num-1][(y*xsize+x)*PIX_SIZE]);
315  p1 = &(clipImage[0]);
316 #ifdef AR_PIX_FORMAT_BGRA
317  k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
318  if( k < thresh ) k = 0;
319  else k = 255;
320  *(p1+0) = *(p1+1) = *(p1+2) = k;
321 #endif
322 #ifdef AR_PIX_FORMAT_ABGR
323  k = (255*3 - (*(p+1) + *(p+2) + *(p+3))) / 3;
324  if( k < thresh ) k = 0;
325  else k = 255;
326  *(p1+1) = *(p1+2) = *(p1+3) = k;
327 #endif
328 #ifdef AR_PIX_FORMAT_BGR
329  k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
330  if( k < thresh ) k = 0;
331  else k = 255;
332  *(p1+0) = *(p1+1) = *(p1+2) = k;
333 #endif
334  }
335  }
336 
337  if( button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN )
338  camera.NextFile();
339 
340  if( button == GLUT_LEFT_BUTTON && state == GLUT_UP ) {
341  if( status == 0 && patt.loop_num < LOOP_MAX ) {
342  camera.GrabFrame();
343  while( (p = (unsigned char *)camera.GetBuffer()) == NULL ) {
344  camera.GrabFrame();
345  //arUtilSleep(2);
346  Sleep(2);
347  }
348 #ifdef USE_TEXMAP
349  patt.savedImage[patt.loop_num] = (unsigned char *)malloc( xsize*tex1Ysize*PIX_SIZE );
350 #else
351  patt.savedImage[patt.loop_num] = (unsigned char *)malloc( xsize*ysize*PIX_SIZE );
352 #endif
353  if( patt.savedImage[patt.loop_num] == NULL ) exit(0);
354 
355  p1 = patt.savedImage[patt.loop_num];
356  for(i=0;i<xsize*ysize*PIX_SIZE;i++) *(p1++) = *(p++);
357  //arVideoCapStop();
358 
359  patt.point[patt.loop_num] = (CALIB_COORD_T *)malloc( sizeof(CALIB_COORD_T)*patt.h_num*patt.v_num );
360  if( patt.point[patt.loop_num] == NULL ) exit(0);
361 
362  patt.loop_num++;
363  status = 1;
364  sx = sy = ex= ey = -1;
365 
366  print_comment(1);
367  }
368  else if( status == 1 && point_num == patt.h_num*patt.v_num ) {
369  status = 0;
370  point_num = 0;
371  //arVideoCapStart();
372 
373  printf("### No.%d ###\n", patt.loop_num);
374  for( j = 0; j < patt.v_num; j++ ) {
375  for( i = 0; i < patt.h_num; i++ ) {
376  printf("%2d, %2d: %6.2f, %6.2f\n", i+1, j+1,
377  patt.point[patt.loop_num-1][j*patt.h_num+i].x_coord,
378  patt.point[patt.loop_num-1][j*patt.h_num+i].y_coord);
379  }
380  }
381  printf("\n\n");
382  if( patt.loop_num < LOOP_MAX ) print_comment(4);
383  else print_comment(6);
384  }
385  else if( status == 1 ) {
386  if( sx < ex ) { ssx = sx; eex = ex; }
387  else { ssx = ex; eex = sx; }
388  if( sy < ey ) { ssy = sy; eey = ey; }
389  else { ssy = ey; eey = sy; }
390 
391  patt.point[patt.loop_num-1][point_num].x_coord = 0.0;
392  patt.point[patt.loop_num-1][point_num].y_coord = 0.0;
393  p = clipImage;
394  k = 0;
395  for( j = 0; j < (eey-ssy+1); j++ ) {
396  for( i = 0; i < (eex-ssx+1); i++ ) {
397  patt.point[patt.loop_num-1][point_num].x_coord += i * *(p+1);
398  patt.point[patt.loop_num-1][point_num].y_coord += j * *(p+1);
399  k += *(p+1);
400  p += PIX_SIZE;
401  }
402  }
403  if( k != 0 ) {
404  patt.point[patt.loop_num-1][point_num].x_coord /= k;
405  patt.point[patt.loop_num-1][point_num].y_coord /= k;
406  patt.point[patt.loop_num-1][point_num].x_coord += ssx;
407  patt.point[patt.loop_num-1][point_num].y_coord += ssy;
408  point_num++;
409  }
410  sx = sy = ex= ey = -1;
411 
412  printf(" # %d/%d\n", point_num, patt.h_num*patt.v_num);
413  if( point_num == patt.h_num*patt.v_num ) print_comment(2);
414  }
415  else if( status == 2 ) {
416  check_num++;
417  if( check_num == patt.loop_num ) {
418  if(patt.loop_num >= 2) {
419  if( calc_inp(&patt, dist_factor, xsize, ysize, mat) < 0 ) {
420  printf("Calibration failed.\n");
421  exit(0);
422  }
423  save_param();
424  }
425  glutDestroyWindow( win );
426  exit(0);
427  }
428 
429  if( check_num+1 == patt.loop_num ) {
430  printf("\nLeft Mouse Button: Next Step.\n");
431  }
432  else {
433  printf(" %d/%d.\n", check_num+1, patt.loop_num);
434  }
435  }
436  }
437 }
438 
439 static void keyEvent(unsigned char key, int x, int y)
440 {
441  if( key == 't' ) {
442  printf("Enter new threshold value (now = %d): ", thresh);
443  scanf("%d",&thresh); while( getchar()!='\n' );
444  printf("\n");
445  }
446 }
447 
448 static void dispImage(void)
449 {
450  unsigned char *dataPtr;
451  ARFloat x, y;
452  int ssx, eex, ssy, eey;
453  int i;
454 
455  if( status == 0 ) {
456  /* grab a video frame */
457  camera.GrabFrame();
458  if( (dataPtr = (ARToolKitPlus::ARUint8 *)camera.GetBuffer()) == NULL ) {
459  //arUtilSleep(2);
460  Sleep(2);
461  return;
462  }
463  dispImage2( dataPtr );
464  }
465 
466  else if( status == 1 ) {
467  dispImage2( patt.savedImage[patt.loop_num-1] );
468 
469  for( i = 0; i < point_num; i++ ) {
470  x = patt.point[patt.loop_num-1][i].x_coord;
471  y = patt.point[patt.loop_num-1][i].y_coord;
472  glColor3f( 1.0, 0.0, 0.0 );
473  glBegin(GL_LINES);
474  glVertex2f( (float)(x-10), (float)(ysize-1-y) );
475  glVertex2f( (float)(x+10), (float)(ysize-1-y) );
476  glVertex2f( (float)x, (float)((ysize-1)-(y-10)) );
477  glVertex2f( (float)x, (float)((ysize-1)-(y+10)) );
478  glEnd();
479  }
480 
481  if( sx != -1 && sy != -1 ) {
482  if( sx < ex ) { ssx = sx; eex = ex; }
483  else { ssx = ex; eex = sx; }
484  if( sy < ey ) { ssy = sy; eey = ey; }
485  else { ssy = ey; eey = sy; }
486  dispClipImage( ssx, ysize-1-ssy, eex-ssx+1, eey-ssy+1, clipImage );
487 #if 0
488  glColor3f( 0.0, 0.0, 1.0 );
489  glBegin(GL_LINE_LOOP);
490  glVertex2f( sx, (ysize-1)-sy );
491  glVertex2f( ex, (ysize-1)-sy );
492  glVertex2f( ex, (ysize-1)-ey );
493  glVertex2f( sx, (ysize-1)-ey );
494  glEnd();
495 #endif
496  }
497  }
498 
499  else if( status == 2 ) {
501  for( i = 0; i < patt.h_num*patt.v_num; i++ ) {
502  x = patt.point[check_num][i].x_coord;
503  y = patt.point[check_num][i].y_coord;
504  glColor3f( 1.0, 0.0, 0.0 );
505  glBegin(GL_LINES);
506  glVertex2f( (float)(x-10), (float)((ysize-1)-y) );
507  glVertex2f( (float)(x+10), (float)((ysize-1)-y) );
508  glVertex2f( (float)x, (float)((ysize-1)-(y-10)) );
509  glVertex2f( (float)x, (float)((ysize-1)-(y+10)) );
510  glEnd();
511  }
512  draw_line();
513  }
514 
515  glutSwapBuffers();
516 }
517 
518 /* cleanup function called when program exits */
519 static void cleanup(void)
520 {
521  //argCleanup();
522 }
523 
524 static void dispImage2( unsigned char *pimage )
525 {
526 #ifndef USE_TEXMAP
527  int sx, sy;
528 
529  sx = 0;
530  sy = ysize - 1;
531  glPixelZoom( 1.0, -1.0);
532  glRasterPos3i( sx, sy, 0 );
533 
534 #ifdef AR_PIX_FORMAT_ABGR
535  glDrawPixels( xsize, ysize, GL_ABGR, GL_UNSIGNED_BYTE, pimage );
536 #endif
537 #ifdef AR_PIX_FORMAT_BGRA
538  glDrawPixels( xsize, ysize, GL_BGRA, GL_UNSIGNED_BYTE, pimage );
539 #endif
540 #ifdef AR_PIX_FORMAT_BGR
541  glDrawPixels( xsize, ysize, GL_BGR, GL_UNSIGNED_BYTE, pimage );
542 #endif
543 #else
544  glDisable( GL_DEPTH_TEST );
545  if( xsize > tex1Xsize1 ) dispImageTex1( pimage );
546  else dispImageTex2( pimage );
547 #endif
548 }
549 
550 #ifdef USE_TEXMAP
551 static void dispImageTex1( unsigned char *pimage )
552 {
553  float sx, sy, ex, ey, z;
554  float tx, ty;
555 
556  glEnable( GL_TEXTURE_2D );
557  glMatrixMode(GL_TEXTURE);
558  glLoadIdentity();
559  glMatrixMode(GL_MODELVIEW);
560 
561  glPixelStorei( GL_UNPACK_ROW_LENGTH, xsize );
562  glPixelStorei( GL_UNPACK_IMAGE_HEIGHT, ysize );
563 
564  glBindTexture( GL_TEXTURE_2D, glid[0] );
565 #ifdef AR_PIX_FORMAT_ABGR
566  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0, GL_ABGR, GL_UNSIGNED_BYTE, pimage );
567 #endif
568 #ifdef AR_PIX_FORMAT_BGRA
569  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0, GL_BGRA, GL_UNSIGNED_BYTE, pimage );
570 #endif
571 #ifdef AR_PIX_FORMAT_BGR
572  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0, GL_BGR, GL_UNSIGNED_BYTE, pimage );
573 #endif
574  tx = 1.0;
575  ty = (ARFloat)ysize / (ARFloat)tex1Ysize;
576  sx = -1.0;
577  sy = ysize - 1;
578  ex = sx + tex1Xsize1;
579  ey = sy - ysize;
580  z = 1.0;
581  glBegin(GL_QUADS );
582  glTexCoord2f( 0.0, 0.0 ); glVertex3f( sx, sy, z );
583  glTexCoord2f( tx, 0.0 ); glVertex3f( ex, sy, z );
584  glTexCoord2f( tx, ty ); glVertex3f( ex, ey, z );
585  glTexCoord2f( 0.0, ty ); glVertex3f( sx, ey, z );
586  glEnd();
587 
588  glBindTexture( GL_TEXTURE_2D, glid[1] );
589 #ifdef AR_PIX_FORMAT_ABGR
590  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize2, tex1Ysize, 0, GL_ABGR, GL_UNSIGNED_BYTE, pimage+tex1Xsize1*PIX_SIZE );
591 #endif
592 #ifdef AR_PIX_FORMAT_BGRA
593  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize2, tex1Ysize, 0, GL_BGRA, GL_UNSIGNED_BYTE, pimage+tex1Xsize1*PIX_SIZE );
594 #endif
595 #ifdef AR_PIX_FORMAT_BGR
596  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize2, tex1Ysize, 0, GL_BGR, GL_UNSIGNED_BYTE, pimage+tex1Xsize1*PIX_SIZE );
597 #endif
598  tx = (ARFloat)(xsize-tex1Xsize1) / (ARFloat)tex1Xsize2;
599  ty = (ARFloat)ysize / (ARFloat)tex1Ysize;
600  sx = tex1Xsize1-1;
601  sy = ysize - 1;
602  ex = sx + tex1Xsize2;
603  ey = sy - ysize;
604  z = 1.0;
605  glBegin(GL_QUADS );
606  glTexCoord2f( 0.0, 0.0 ); glVertex3f( sx, sy, z );
607  glTexCoord2f( tx, 0.0 ); glVertex3f( ex, sy, z );
608  glTexCoord2f( tx, ty ); glVertex3f( ex, ey, z );
609  glTexCoord2f( 0.0, ty ); glVertex3f( sx, ey, z );
610  glEnd();
611 
612  glBindTexture( GL_TEXTURE_2D, 0 );
613  glDisable( GL_TEXTURE_2D );
614  glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
615  glPixelStorei( GL_UNPACK_IMAGE_HEIGHT, 0 );
616 }
617 
618 static void dispImageTex2( unsigned char *pimage )
619 {
620  float sx, sy, ex, ey, z;
621  float tx, ty;
622 
623  glEnable( GL_TEXTURE_2D );
624  glMatrixMode(GL_TEXTURE);
625  glLoadIdentity();
626  glMatrixMode(GL_MODELVIEW);
627 
628  glPixelStorei( GL_UNPACK_ROW_LENGTH, xsize );
629  glPixelStorei( GL_UNPACK_IMAGE_HEIGHT, ysize );
630 
631  glBindTexture( GL_TEXTURE_2D, glid[0] );
632 #ifdef AR_PIX_FORMAT_ABGR
633  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0, GL_ABGR, GL_UNSIGNED_BYTE, pimage );
634 #endif
635 #ifdef AR_PIX_FORMAT_BGRA
636  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0, GL_BGRA, GL_UNSIGNED_BYTE, pimage );
637 #endif
638 #ifdef AR_PIX_FORMAT_BGR
639  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0, GL_BGR, GL_UNSIGNED_BYTE, pimage );
640 #endif
641  tx = (ARFloat)xsize / (ARFloat)tex1Xsize1;
642  ty = (ARFloat)ysize / (ARFloat)tex1Ysize;
643  sx = -1;
644  sy = ysize - 1;
645  ex = sx + xsize;
646  ey = sy - ysize;
647  z = 1.0;
648  glBegin(GL_QUADS );
649  glTexCoord2f( 0.0, 0.0 ); glVertex3f( sx, sy, z );
650  glTexCoord2f( tx, 0.0 ); glVertex3f( ex, sy, z );
651  glTexCoord2f( tx, ty ); glVertex3f( ex, ey, z );
652  glTexCoord2f( 0.0, ty ); glVertex3f( sx, ey, z );
653  glEnd();
654 
655  glBindTexture( GL_TEXTURE_2D, 0 );
656  glDisable( GL_TEXTURE_2D );
657  glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
658  glPixelStorei( GL_UNPACK_IMAGE_HEIGHT, 0 );
659 }
660 #endif
661 
662 static void draw_line(void)
663 {
664  ARFloat *x, *y;
665  int max;
666  int i, j, k, l;
667  int p;
668 
669  max = (patt.v_num > patt.h_num)? patt.v_num: patt.h_num;
670  x = (ARFloat *)malloc( sizeof(ARFloat)*max );
671  y = (ARFloat *)malloc( sizeof(ARFloat)*max );
672  if( x == NULL || y == NULL ) exit(0);
673 
674  i = check_num;
675 
676  for( j = 0; j < patt.v_num; j++ ) {
677  for( k = 0; k < patt.h_num; k++ ) {
678  x[k] = patt.point[i][j*patt.h_num+k].x_coord;
679  y[k] = patt.point[i][j*patt.h_num+k].y_coord;
680  }
681  draw_line2( x, y, patt.h_num );
682  }
683 
684  for( j = 0; j < patt.h_num; j++ ) {
685  for( k = 0; k < patt.v_num; k++ ) {
686  x[k] = patt.point[i][k*patt.h_num+j].x_coord;
687  y[k] = patt.point[i][k*patt.h_num+j].y_coord;
688  }
689  draw_line2( x, y, patt.v_num );
690  }
691 
692  for( j = 3 - patt.v_num; j < patt.h_num - 2; j++ ) {
693  p = 0;
694  for( k = 0; k < patt.v_num; k++ ) {
695  l = j+k;
696  if( l < 0 || l >= patt.h_num ) continue;
697  x[p] = patt.point[i][k*patt.h_num+l].x_coord;
698  y[p] = patt.point[i][k*patt.h_num+l].y_coord;
699  p++;
700  }
701  draw_line2( x, y, p );
702  }
703 
704  for( j = 2; j < patt.h_num + patt.v_num - 3; j++ ) {
705  p = 0;
706  for( k = 0; k < patt.v_num; k++ ) {
707  l = j-k;
708  if( l < 0 || l >= patt.h_num ) continue;
709  x[p] = patt.point[i][k*patt.h_num+l].x_coord;
710  y[p] = patt.point[i][k*patt.h_num+l].y_coord;
711  p++;
712  }
713  draw_line2( x, y, p );
714  }
715 
716  free( x );
717  free( y );
718 }
719 
720 static void draw_line2( ARFloat *x, ARFloat *y, int num )
721 {
722  ARToolKitPlus::ARMat *input, *evec;
723  ARToolKitPlus::ARVec *ev, *mean;
724  ARFloat a, b, c;
725  int i;
726 
728  mean = ARToolKitPlus::Vector::alloc( 2 );
729  evec = ARToolKitPlus::Matrix::alloc( 2, 2 );
730 
731  input = ARToolKitPlus::Matrix::alloc( num, 2 );
732  for( i = 0; i < num; i++ ) {
733  ARToolKitPlus::Tracker::arParamObserv2Ideal( dist_factor, x[i], y[i],
734  &(input->m[i*2+0]), &(input->m[i*2+1]) );
735  }
736  if( ARToolKitPlus::Tracker::arMatrixPCA(input, evec, ev, mean) < 0 ) exit(0);
737  a = evec->m[1];
738  b = -evec->m[0];
739  c = -(a*mean->v[0] + b*mean->v[1]);
740 
745 
746  draw_warp_line(a, b, c);
747 }
748 
749 static void draw_warp_line( ARFloat a, ARFloat b , ARFloat c )
750 {
751  ARFloat x, y;
752  ARFloat x1, y1;
753  int i;
754 
755  glLineWidth( 1.0 );
756  glBegin(GL_LINE_STRIP);
757  if( a*a >= b*b ) {
758  for( i = -20; i <= ysize+20; i+=10 ) {
759  x = -(b*i + c)/a;
760  y = i;
761 
762  ARToolKitPlus::Tracker::arParamIdeal2Observ( dist_factor, x, y, &x1, &y1 );
763  glVertex2f( (float)x1, (float)(ysize-1-y1) );
764  }
765  }
766  else {
767  for( i = -20; i <= xsize+20; i+=10 ) {
768  x = i;
769  y = -(a*i + c)/b;
770 
771  ARToolKitPlus::Tracker::arParamIdeal2Observ( dist_factor, x, y, &x1, &y1 );
772  glVertex2f( (float)x1, (float)(ysize-1-y1) );
773  }
774  }
775  glEnd();
776 }
777 
778 static void print_comment( int status )
779 {
780  printf("\n-----------\n");
781  switch( status ) {
782  case 0:
783  printf("Mouse Button\n");
784  printf(" Left : Grab image.\n");
785  printf(" Right : Quit.\n");
786  break;
787  case 1:
788  printf("Mouse Button\n");
789  printf(" Left : Rubber-bounding of feature. (%d x %d)\n", patt.h_num, patt.v_num);
790  printf(" Right : Cansel rubber-bounding & Retry grabbing.\n");
791  break;
792  case 2:
793  printf("Mouse Button\n");
794  printf(" Left : Save feature position.\n");
795  printf(" Right : Discard & Retry grabbing.\n");
796  break;
797  case 4:
798  printf("Mouse Button\n");
799  printf(" Left : Grab next image.\n");
800  printf(" Right : Calc parameter.\n");
801  break;
802  case 5:
803  printf("Mouse Button\n");
804  printf(" Left : Check fittness.\n");
805  printf(" Right :\n");
806  printf(" %d/%d.\n", check_num+1, patt.loop_num);
807  break;
808  case 6:
809  printf("Mouse Button\n");
810  printf(" Left :\n");
811  printf(" Right : Calc parameter.\n");
812  printf(" %d/%d.\n", check_num+1, patt.loop_num);
813  break;
814  }
815  printf("-----------\n");
816 }
817 
818 static void dispClipImage( int sx, int sy, int xsize, int ysize, ARToolKitPlus::ARUint8 *clipImage )
819 {
820  glPixelZoom( (GLfloat)1.0, (GLfloat)-1.0);
821  glRasterPos3i( sx, sy, 0 );
822 #ifdef AR_PIX_FORMAT_ABGR
823  glDrawPixels( xsize, ysize, GL_ABGR, GL_UNSIGNED_BYTE, clipImage );
824 #endif
825 #ifdef AR_PIX_FORMAT_BGRA
826  glDrawPixels( xsize, ysize, GL_BGRA, GL_UNSIGNED_BYTE, clipImage );
827 #endif
828 #ifdef AR_PIX_FORMAT_BGR
829  glDrawPixels( xsize, ysize, GL_BGR, GL_UNSIGNED_BYTE, clipImage );
830 #endif
831 }
832 
833 
834 
static void print_comment(int status)
#define GL_ABGR
ARFloat * v
Definition: vector.h:55
bool param(const std::string &param_name, T &param_val, const T &default_val)
ARFloat * m
Definition: matrix.h:65
#define GL_BGR
static int free(ARVec *v)
static void cleanup(void)
#define THRESH
Definition: calib_camera.h:7
ARToolKitPlus::Tracker * theTracker
static void motionEvent(int x, int y)
#define GL_BGRA
static int free(ARMat *m)
#define V_NUM
Definition: calib_camera.h:5
#define H_NUM
Definition: calib_camera.h:4
ARFloat x_coord
Definition: calib_camera.h:10
#define LOOP_MAX
Definition: calib_camera.h:6
static bool usesSinglePrecision()
static void dispImage(void)
TFSIMD_FORCE_INLINE const tfScalar & y() const
Tracker is the vision core of ARToolKit.
Definition: Tracker.h:70
static void mouseEvent(int button, int state, int x, int y)
unsigned char * clipImage
ARFloat mat[3][4]
Definition: param.h:52
static void dispClipImage(int sx, int sy, int xsize, int ysize, ARToolKitPlus::ARUint8 *clipImage)
static void save_param(void)
static ARVec * alloc(int clm)
ARFloat y_coord
Definition: calib_camera.h:11
long * GetBuffer()
Definition: arFileGrabber.h:22
CALIB_PATT_T patt
int h_num
Definition: calib_camera.h:18
ARFloat dist_factor[4]
unsigned char ARUint8
Definition: ar.h:66
ARFileGrabber camera("dump_%02d.raw", CAM_W, CAM_H, 4)
TFSIMD_FORCE_INLINE const tfScalar & x() const
static void keyEvent(unsigned char key, int x, int y)
static void draw_warp_line(ARFloat a, ARFloat b, ARFloat c)
static void draw_line(void)
TFSIMD_FORCE_INLINE const tfScalar & z() const
ARFloat dist_factor[4]
Definition: param.h:53
CALIB_COORD_T * point[LOOP_MAX]
Definition: calib_camera.h:17
#define NULL
Definition: PocketKnife.h:38
static void dispImage2(unsigned char *pimage)
int v_num
Definition: calib_camera.h:19
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
int calc_inp(CALIB_PATT_T *patt, ARFloat dist_factor[4], int xsize, int ysize, ARFloat mat[3][4])
Definition: calib_inp.cpp:18
unsigned char * savedImage[LOOP_MAX]
Definition: calib_camera.h:15
float ARFloat
Definition: config.h:60
int loop_num
Definition: calib_camera.h:20
static void draw_line2(ARFloat *x, ARFloat *y, int num)
CALIB_COORD_T * world_coord
Definition: calib_camera.h:16
static void init(void)
#define max(a, b)
Definition: rpp_quintic.cpp:44
ARFloat mat[3][4]
static ARMat * alloc(int row, int clm)
void calc_distortion(CALIB_PATT_T *patt, int xsize, int ysize, ARFloat dist_factor[3])


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