00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifdef _WIN32
00010 #include <windows.h>
00011 #endif
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #ifndef __APPLE__
00016 #include <GL/gl.h>
00017 #include <GL/glut.h>
00018 #else
00019 #include <OpenGL/gl.h>
00020 #include <GLUT/glut.h>
00021 #endif
00022 #include <AR/gsub.h>
00023 #include <AR/video.h>
00024 #include <AR/param.h>
00025 #include <AR/ar.h>
00026 #include "object.h"
00027 #include "draw_object.h"
00028
00029
00030
00031
00032
00033 #ifdef _WIN32
00034 char *vconf = "Data\\WDM_camera_flipV.xml";
00035 #else
00036 char *vconf = "";
00037 #endif
00038
00039 int xsize;
00040 int ysize;
00041 int thresh = 100;
00042
00043
00044 ARParam cparam;
00045
00046 ObjectData_T *object;
00047 int objectnum;
00048 double object_center[2] = {0.0, 0.0};
00049
00050 static int count = 0;
00051
00052
00053
00054 static void usage( char *com );
00055 static int init( int argc, char *argv[] );
00056 static void cleanup(void);
00057 static void keyEvent( unsigned char key, int x, int y);
00058 static void mainLoop(void);
00059 static void dispMode(void);
00060
00061 static void usage( char *com )
00062 {
00063 printf("Usage: %s [options]\n", com);
00064 printf(" Options:\n");
00065 printf(" -c <camera parameter filename>\n");
00066 printf(" -o <object data filename>\n");
00067 printf("\n");
00068 exit(0);
00069 }
00070
00071 int main(int argc, char **argv)
00072 {
00073 glutInit(&argc, argv);
00074 if( init( argc, argv ) < 0 ) exit(0);
00075
00076 arVideoCapStart();
00077 argMainLoop( NULL, keyEvent, mainLoop );
00078 return (0);
00079 }
00080
00081 static void keyEvent( unsigned char key, int x, int y)
00082 {
00083
00084 if( key == 0x1b ) {
00085 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00086
00087 cleanup();
00088 exit(0);
00089 }
00090
00091 if( key == 'p' ) {
00092 arMatchingPCAMode = 1 - arMatchingPCAMode;
00093 count = 0;
00094 dispMode();
00095 }
00096
00097
00098 if( key == 't' ) {
00099 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00100 printf("Enter new threshold value (current = %d): ", thresh);
00101 scanf("%d",&thresh); while( getchar()!='\n' );
00102 printf("\n");
00103 dispMode();
00104 count = 0;
00105 }
00106
00107
00108 if( key == 'd' ) {
00109 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00110 arDebug = 1 - arDebug;
00111 if( arDebug == 0 ) {
00112 glClearColor( 0.0, 0.0, 0.0, 0.0 );
00113 glClear(GL_COLOR_BUFFER_BIT);
00114 argSwapBuffers();
00115 glClear(GL_COLOR_BUFFER_BIT);
00116 argSwapBuffers();
00117 }
00118 count = 0;
00119 dispMode();
00120 }
00121
00122 if( key == 'z' ) {
00123 if( arFittingMode == AR_FITTING_TO_IDEAL ) {
00124 arFittingMode = AR_FITTING_TO_INPUT;
00125 }
00126 else {
00127 arFittingMode = AR_FITTING_TO_IDEAL;
00128 }
00129 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00130 count = 0;
00131 dispMode();
00132 }
00133
00134 if( key == 'x' ) {
00135 if( arImageProcMode == AR_IMAGE_PROC_IN_FULL ) {
00136 arImageProcMode = AR_IMAGE_PROC_IN_HALF;
00137 }
00138 else {
00139 arImageProcMode = AR_IMAGE_PROC_IN_FULL;
00140 }
00141 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00142 count = 0;
00143 dispMode();
00144 }
00145
00146 if( key == 'c' ) {
00147 if( argDrawMode == AR_DRAW_BY_GL_DRAW_PIXELS ) {
00148 argDrawMode = AR_DRAW_BY_TEXTURE_MAPPING;
00149 argTexmapMode = AR_DRAW_TEXTURE_FULL_IMAGE;
00150 }
00151 else if( argTexmapMode == AR_DRAW_TEXTURE_FULL_IMAGE ) {
00152 argTexmapMode = AR_DRAW_TEXTURE_HALF_IMAGE;
00153 }
00154 else {
00155 argDrawMode = AR_DRAW_BY_GL_DRAW_PIXELS;
00156 }
00157 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00158 count = 0;
00159 dispMode();
00160 }
00161
00162 if( key == 'm' ) {
00163 if( arTemplateMatchingMode == AR_TEMPLATE_MATCHING_COLOR ) {
00164 arTemplateMatchingMode = AR_TEMPLATE_MATCHING_BW;
00165 }
00166 else {
00167 arTemplateMatchingMode = AR_TEMPLATE_MATCHING_COLOR;
00168 }
00169 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00170 count = 0;
00171 dispMode();
00172 }
00173 }
00174
00175
00176 static void mainLoop(void)
00177 {
00178 ARUint8 *dataPtr;
00179 ARMarkerInfo *marker_info;
00180 int marker_num;
00181 int i, j, k;
00182
00183 if( count == 0 ) arUtilTimerReset();
00184
00185
00186 while( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) arUtilSleep(2);
00187
00188 argDrawMode2D();
00189 argDispImage( dataPtr, 0, 0 );
00190
00191
00192 if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
00193 cleanup();
00194 exit(0);
00195 }
00196
00197
00198
00199 if( arDebug ) {
00200 argDispImage( dataPtr, 1, 1 );
00201 if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
00202 argDispHalfImage( arImage, 2, 1 );
00203 else
00204 argDispImage( arImage, 2, 1);
00205
00206 glColor3f( 1.0, 0.0, 0.0 );
00207 glLineWidth( 3.0 );
00208 for( i = 0; i < marker_num; i++ ) {
00209 if( marker_info[i].id < 0 ) continue;
00210 argDrawSquare( marker_info[i].vertex, 2, 1 );
00211 }
00212 glLineWidth( 1.0 );
00213 }
00214 arVideoCapNext();
00215
00216
00217 for( i = 0; i < objectnum; i++ ) {
00218 object[i].visible = 0;
00219 k = -1;
00220 for( j = 0; j < marker_num; j++ ) {
00221 if( object[i].id == marker_info[j].id ) {
00222 if( k == -1 ) k = j;
00223 else {
00224 if( marker_info[k].cf < marker_info[j].cf ) k = j;
00225 }
00226 }
00227 }
00228 if( k == -1 ) continue;
00229
00230
00231 arGetTransMat(&marker_info[k], object_center, object[i].marker_width, object[i].trans);
00232 object[i].visible = 1;
00233 }
00234
00235
00236 glClearDepth( 1.0 );
00237 glClear(GL_DEPTH_BUFFER_BIT);
00238 draw( object, objectnum );
00239
00240 count++;
00241 if( count == 30 ) {
00242 printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
00243 count = 0;
00244 }
00245
00246 argSwapBuffers();
00247 }
00248
00249
00250 static int init( int argc, char *argv[] )
00251 {
00252 char cparaname[256];
00253 char odataname[256];
00254 ARParam wparam;
00255 int i;
00256
00257
00258
00259 strcpy( cparaname, "Data/camera_para.dat" );
00260 strcpy( odataname, "Data/object_data" );
00261
00262
00263 for( i = 1; i < argc; i++ ) {
00264 if( strcmp(argv[i],"-c") == 0 ) {
00265 if( i < argc-1 && argv[i+1][0] != '-' ) {
00266 strcpy( cparaname, argv[i+1] );
00267 i++;
00268 }
00269 else usage( argv[0] );
00270 }
00271 else if( strcmp(argv[i],"-o") == 0 ) {
00272 if( i < argc-1 && argv[i+1][0] != '-' ) {
00273 strcpy( odataname, argv[i+1] );
00274 i++;
00275 }
00276 else usage( argv[0] );
00277 }
00278 else usage( argv[0] );
00279 }
00280
00281
00282 if( (object=read_objectdata(odataname,&objectnum)) == NULL ) exit(0);
00283
00284
00285 if( arVideoOpen( vconf ) < 0 ) exit(0);
00286
00287 if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
00288 printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);
00289
00290
00291 if( arParamLoad(cparaname, 1, &wparam) < 0 ) {
00292 printf("Camera parameter load error !!\n");
00293 exit(0);
00294 }
00295 arParamChangeSize( &wparam, xsize, ysize, &cparam );
00296
00297 arInitCparam( &cparam );
00298 printf("*** Camera Parameter ***\n");
00299 arParamDisp( &cparam );
00300
00301
00302 argInit( &cparam, 1.0, 0, 2, 1, 0 );
00303
00304 dispMode();
00305
00306 return 0;
00307 }
00308
00309 static void dispMode( void )
00310 {
00311 if( arFittingMode == AR_FITTING_TO_INPUT ) {
00312 printf("FittingMode (Z): INPUT IMAGE\n");
00313 }
00314 else {
00315 printf("FittingMode (Z): COMPENSATED IMAGE\n");
00316 }
00317
00318 if( arImageProcMode == AR_IMAGE_PROC_IN_FULL ) {
00319 printf("ProcMode (X) : FULL IMAGE\n");
00320 }
00321 else {
00322 printf("ProcMode (X) : HALF IMAGE\n");
00323 }
00324
00325 if( argDrawMode == AR_DRAW_BY_GL_DRAW_PIXELS ) {
00326 printf("DrawMode (C) : GL_DRAW_PIXELS\n");
00327 }
00328 else if( argTexmapMode == AR_DRAW_TEXTURE_FULL_IMAGE ) {
00329 printf("DrawMode (C) : TEXTURE MAPPING (FULL RESOLUTION)\n");
00330 }
00331 else {
00332 printf("DrawMode (C) : TEXTURE MAPPING (HALF RESOLUTION)\n");
00333 }
00334
00335 if( arTemplateMatchingMode == AR_TEMPLATE_MATCHING_COLOR ) {
00336 printf("TemplateMatchingMode (M) : Color Template\n");
00337 }
00338 else {
00339 printf("TemplateMatchingMode (M) : BW Template\n");
00340 }
00341
00342 if( arMatchingPCAMode == AR_MATCHING_WITHOUT_PCA ) {
00343 printf("MatchingPCAMode (P) : Without PCA\n");
00344 }
00345 else {
00346 printf("MatchingPCAMode (P) : With PCA\n");
00347 }
00348 }
00349
00350
00351 static void cleanup(void)
00352 {
00353 arVideoCapStop();
00354 arVideoClose();
00355 argCleanup();
00356 }