16 #include "ARFileGrabber.h" 22 #define GL_ABGR GL_ABGR_EXT 25 #define GL_BGRA GL_BGRA_EXT 28 #define GL_BGR GL_BGR_EXT 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 );
57 static void init(
void);
58 static void mouseEvent(
int button,
int state,
int x,
int y);
60 static void keyEvent(
unsigned char key,
int x,
int y);
62 static void dispImage2(
unsigned char *pimage );
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") 101 for( j = 0; j < 3; j++ ) {
102 for( i = 0; i < 4; i++ ) {
103 param.
mat[j][i] =
mat[j][i];
107 ARToolKitPlus::Param::display( ¶m );
109 printf(
"Filename: ");
110 scanf(
"%s", name );
while( getchar()!=
'\n' );
111 ARToolKitPlus::Tracker::arParamSave( name, 1, ¶m );
122 printf(
"ERROR: CameraCalib needs one of: PIXEL_FORMAT_ABGR, PIXEL_FORMAT_BGRA or PIXEL_FORMAT_BGR\n");
128 printf(
"ERROR: CameraCalib needs double precision\n");
167 if( patt.
h_num < 3 || patt.
v_num < 3 ) exit(0);
169 printf(
"Input the length between each markers: ");
170 scanf(
"%lf", &length);
while( getchar()!=
'\n' );
172 for( j = 0; j < patt.
v_num; j++ ) {
173 for( i = 0; i < patt.
h_num; i++ ) {
181 printf(
"Image size (x,y) = (%d,%d)\n",
xsize,
ysize);
183 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
185 glutInitWindowPosition(100,100);
186 win = glutCreateWindow(
"Calib distotion param");
188 glMatrixMode(GL_MODELVIEW);
190 glMatrixMode(GL_PROJECTION);
192 glOrtho(-0.5,
xsize-0.5, -0.5,
ysize-0.5, -1.0, 1.0);
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 );
217 while( tex1Xsize2 <
xsize - tex1Xsize1 ) tex1Xsize2 *= 2;
221 while( tex1Xsize1 <
xsize ) tex1Xsize1 *= 2;
224 while( tex1Ysize <
ysize ) tex1Ysize *= 2;
230 unsigned char *p, *p1;
231 int ssx, ssy, eex, eey;
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; }
243 for( j = ssy; j <= eey; j++ ) {
245 for( i = ssx; i <= eex; i++ ) {
246 #ifdef AR_PIX_FORMAT_BGRA 247 k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
250 *(p1+0) = *(p1+1) = *(p1+2) = k;
252 #ifdef AR_PIX_FORMAT_ABGR 253 k = (255*3 - (*(p+1) + *(p+2) + *(p+3))) / 3;
256 *(p1+1) = *(p1+2) = *(p1+3) = k;
258 #ifdef AR_PIX_FORMAT_BGR 259 k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
262 *(p1+0) = *(p1+1) = *(p1+2) = k;
273 unsigned char *p, *p1;
274 int ssx, ssy, eex, eey;
277 if( button == GLUT_RIGHT_BUTTON && state == GLUT_UP ) {
281 printf(
"--------------\n");
286 printf(
"--------------\n");
292 glutDestroyWindow(
win );
297 if( patt.
loop_num == 0 ) {printf(
"error!!\n"); exit(0);}
309 if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {
316 #ifdef AR_PIX_FORMAT_BGRA 317 k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
320 *(p1+0) = *(p1+1) = *(p1+2) = k;
322 #ifdef AR_PIX_FORMAT_ABGR 323 k = (255*3 - (*(p+1) + *(p+2) + *(p+3))) / 3;
326 *(p1+1) = *(p1+2) = *(p1+3) = k;
328 #ifdef AR_PIX_FORMAT_BGR 329 k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
332 *(p1+0) = *(p1+1) = *(p1+2) = k;
337 if( button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN )
340 if( button == GLUT_LEFT_BUTTON && state == GLUT_UP ) {
356 for(i=0;i<
xsize*
ysize*PIX_SIZE;i++) *(p1++) = *(p++);
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,
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; }
395 for( j = 0; j < (eey-ssy+1); j++ ) {
396 for( i = 0; i < (eex-ssx+1); i++ ) {
420 printf(
"Calibration failed.\n");
425 glutDestroyWindow(
win );
430 printf(
"\nLeft Mouse Button: Next Step.\n");
439 static void keyEvent(
unsigned char key,
int x,
int y)
442 printf(
"Enter new threshold value (now = %d): ",
thresh);
443 scanf(
"%d",&
thresh);
while( getchar()!=
'\n' );
450 unsigned char *dataPtr;
452 int ssx, eex, ssy, eey;
472 glColor3f( 1.0, 0.0, 0.0 );
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)) );
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; }
488 glColor3f( 0.0, 0.0, 1.0 );
489 glBegin(GL_LINE_LOOP);
501 for( i = 0; i < patt.
h_num*patt.
v_num; i++ ) {
504 glColor3f( 1.0, 0.0, 0.0 );
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)) );
531 glPixelZoom( 1.0, -1.0);
532 glRasterPos3i( sx, sy, 0 );
534 #ifdef AR_PIX_FORMAT_ABGR 537 #ifdef AR_PIX_FORMAT_BGRA 540 #ifdef AR_PIX_FORMAT_BGR 544 glDisable( GL_DEPTH_TEST );
545 if(
xsize > tex1Xsize1 ) dispImageTex1( pimage );
546 else dispImageTex2( pimage );
551 static void dispImageTex1(
unsigned char *pimage )
556 glEnable( GL_TEXTURE_2D );
557 glMatrixMode(GL_TEXTURE);
559 glMatrixMode(GL_MODELVIEW);
561 glPixelStorei( GL_UNPACK_ROW_LENGTH,
xsize );
562 glPixelStorei( GL_UNPACK_IMAGE_HEIGHT,
ysize );
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 );
568 #ifdef AR_PIX_FORMAT_BGRA 569 glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0,
GL_BGRA, GL_UNSIGNED_BYTE, pimage );
571 #ifdef AR_PIX_FORMAT_BGR 572 glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0,
GL_BGR, GL_UNSIGNED_BYTE, pimage );
578 ex = sx + tex1Xsize1;
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 );
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 );
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 );
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 );
602 ex = sx + tex1Xsize2;
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 );
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 );
618 static void dispImageTex2(
unsigned char *pimage )
623 glEnable( GL_TEXTURE_2D );
624 glMatrixMode(GL_TEXTURE);
626 glMatrixMode(GL_MODELVIEW);
628 glPixelStorei( GL_UNPACK_ROW_LENGTH,
xsize );
629 glPixelStorei( GL_UNPACK_IMAGE_HEIGHT,
ysize );
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 );
635 #ifdef AR_PIX_FORMAT_BGRA 636 glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0,
GL_BGRA, GL_UNSIGNED_BYTE, pimage );
638 #ifdef AR_PIX_FORMAT_BGR 639 glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1Xsize1, tex1Ysize, 0,
GL_BGR, GL_UNSIGNED_BYTE, pimage );
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 );
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 );
672 if( x ==
NULL || y ==
NULL ) exit(0);
676 for( j = 0; j < patt.
v_num; j++ ) {
677 for( k = 0; k < patt.
h_num; k++ ) {
684 for( j = 0; j < patt.
h_num; j++ ) {
685 for( k = 0; k < patt.
v_num; k++ ) {
692 for( j = 3 - patt.
v_num; j < patt.
h_num - 2; j++ ) {
694 for( k = 0; k < patt.
v_num; k++ ) {
696 if( l < 0 || l >= patt.
h_num )
continue;
704 for( j = 2; j < patt.
h_num + patt.
v_num - 3; j++ ) {
706 for( k = 0; k < patt.
v_num; k++ ) {
708 if( l < 0 || l >= patt.
h_num )
continue;
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]) );
736 if( ARToolKitPlus::Tracker::arMatrixPCA(input, evec, ev, mean) < 0 ) exit(0);
739 c = -(a*mean->
v[0] + b*mean->
v[1]);
756 glBegin(GL_LINE_STRIP);
758 for( i = -20; i <=
ysize+20; i+=10 ) {
762 ARToolKitPlus::Tracker::arParamIdeal2Observ(
dist_factor, x, y, &x1, &y1 );
763 glVertex2f( (
float)x1, (
float)(
ysize-1-y1) );
767 for( i = -20; i <=
xsize+20; i+=10 ) {
771 ARToolKitPlus::Tracker::arParamIdeal2Observ(
dist_factor, x, y, &x1, &y1 );
772 glVertex2f( (
float)x1, (
float)(
ysize-1-y1) );
780 printf(
"\n-----------\n");
783 printf(
"Mouse Button\n");
784 printf(
" Left : Grab image.\n");
785 printf(
" Right : Quit.\n");
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");
793 printf(
"Mouse Button\n");
794 printf(
" Left : Save feature position.\n");
795 printf(
" Right : Discard & Retry grabbing.\n");
798 printf(
"Mouse Button\n");
799 printf(
" Left : Grab next image.\n");
800 printf(
" Right : Calc parameter.\n");
803 printf(
"Mouse Button\n");
804 printf(
" Left : Check fittness.\n");
805 printf(
" Right :\n");
809 printf(
"Mouse Button\n");
811 printf(
" Right : Calc parameter.\n");
815 printf(
"-----------\n");
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 );
825 #ifdef AR_PIX_FORMAT_BGRA 826 glDrawPixels( xsize, ysize,
GL_BGRA, GL_UNSIGNED_BYTE, clipImage );
828 #ifdef AR_PIX_FORMAT_BGR 829 glDrawPixels( xsize, ysize,
GL_BGR, GL_UNSIGNED_BYTE, clipImage );
static void print_comment(int status)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
static void cleanup(void)
ARToolKitPlus::Tracker * theTracker
static void motionEvent(int x, int y)
static void dispImage(void)
TFSIMD_FORCE_INLINE const tfScalar & y() const
static void mouseEvent(int button, int state, int x, int y)
unsigned char * clipImage
static void dispClipImage(int sx, int sy, int xsize, int ysize, ARToolKitPlus::ARUint8 *clipImage)
static void save_param(void)
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
CALIB_COORD_T * point[LOOP_MAX]
static void dispImage2(unsigned char *pimage)
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])
unsigned char * savedImage[LOOP_MAX]
static void draw_line2(ARFloat *x, ARFloat *y, int num)
CALIB_COORD_T * world_coord
void calc_distortion(CALIB_PATT_T *patt, int xsize, int ysize, ARFloat dist_factor[3])