mk_patt.c
Go to the documentation of this file.
00001 /*
00002  * 
00003  * This file is part of ARToolKit.
00004  * 
00005  * ARToolKit is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * ARToolKit is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with ARToolKit; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  * 
00019  */
00020 
00021 // ============================================================================
00022 //      Includes
00023 // ============================================================================
00024 
00025 #ifdef _WIN32
00026 #  include <windows.h>
00027 #endif
00028 #include <math.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #ifndef __APPLE__
00033 #  include <GL/glut.h>
00034 #  ifdef GL_VERSION_1_2
00035 #    include <GL/glext.h>
00036 #  endif
00037 #else
00038 #  include <GLUT/glut.h>
00039 #  include <OpenGL/glext.h>
00040 #endif
00041 #include <AR/config.h>
00042 #include <AR/video.h>
00043 #include <AR/param.h>
00044 #include <AR/ar.h>
00045 #include <AR/gsub_lite.h>
00046 
00047 // ============================================================================
00048 //      Constants
00049 // ============================================================================
00050 
00051 // ============================================================================
00052 //      Global variables
00053 // ============================================================================
00054 
00055 /* set up the video format globals */
00056 
00057 #if defined(__sgi)
00058 char            *vconf = "-size=FULL";
00059 #elif defined(__linux)
00060 #  if defined(AR_INPUT_GSTREAMER)
00061 char                    *vconf = "videotestsrc";
00062 #  elif defined(AR_INPUT_V4L)
00063 char            *vconf = "-width=640 -height=480";
00064 #  elif defined(AR_INPUT_1394CAM)
00065 char            *vconf = "-mode=640x480_YUV411";
00066 #  elif defined(AR_INPUT_DV)
00067 char            *vconf = "";
00068 #  endif
00069 #elif defined(_WIN32)
00070 char                    *vconf = "Data\\WDM_camera_flipV.xml";
00071 #elif defined(__APPLE__)
00072 char                    *vconf = "-width=640 -height=480";
00073 #else
00074 char                    *vconf = "";
00075 #endif
00076 
00077 // Image acquisition.
00078 static ARUint8          *gARTImage = NULL;
00079 static ARUint8          *gARTsaveImage = NULL;
00080 
00081 // Marker detection.
00082 static int                      gARTThreshhold = 100;
00083 static ARMarkerInfo* gTarget  = NULL;
00084 
00085 
00086 // Drawing.
00087 static ARParam          gARTCparam;
00088 static ARGL_CONTEXT_SETTINGS_REF gArglSettings = NULL;
00089 
00090 // ============================================================================
00091 //      Functions
00092 // ============================================================================
00093 
00094 void lineSeg(double x1, double y1, double x2, double y2, ARGL_CONTEXT_SETTINGS_REF contextSettings, ARParam cparam, double zoom)
00095 {
00096         int enable;
00097     float   ox, oy;
00098     double  xx1, yy1, xx2, yy2;
00099         
00100         if (!contextSettings) return;
00101         arglDistortionCompensationGet(contextSettings, &enable);
00102     if (arglDrawModeGet(contextSettings) == AR_DRAW_BY_TEXTURE_MAPPING && enable) {
00103         xx1 = x1;  yy1 = y1;
00104         xx2 = x2;  yy2 = y2;
00105     } else {
00106         arParamIdeal2Observ(cparam.dist_factor, x1, y1, &xx1, &yy1);
00107         arParamIdeal2Observ(cparam.dist_factor, x2, y2, &xx2, &yy2);
00108     }
00109         
00110     xx1 *= zoom; yy1 *= zoom;
00111     xx2 *= zoom; yy2 *= zoom;
00112         
00113         ox = 0;
00114         oy = cparam.ysize - 1;
00115         glBegin(GL_LINES);
00116         glVertex2f(ox + xx1, oy - yy1);
00117         glVertex2f(ox + xx2, oy - yy2);
00118         glEnd();
00119     glFlush();
00120 }
00121 
00122 static int setupCamera(ARParam *cparam)
00123 {
00124     ARParam  wparam;
00125     char     name1[256], name2[256];
00126         int                             xsize, ysize;
00127 
00128     printf("Enter camera parameter filename");
00129     printf("(Data/camera_para.dat): ");
00130     if (fgets(name1, 256, stdin) == NULL) exit(0);
00131     if (sscanf(name1, "%s", name2) != 1) {
00132         strcpy(name2, "Data/camera_para.dat");
00133     }
00134 
00135         // Load the camera parameters.
00136         if (arParamLoad(name2, 1, &wparam) < 0 ) {
00137         printf("Parameter load error !!\n");
00138         return (FALSE);
00139     }
00140         
00141     // Open the video path.
00142     if (arVideoOpen(vconf) < 0) {
00143         fprintf(stderr, "setupCamera(): Unable to open connection to camera.\n");
00144         return (FALSE);
00145         }
00146         
00147     // Find the size of the window.
00148     if (arVideoInqSize(&xsize, &ysize) < 0) return (FALSE);
00149     fprintf(stdout, "Camera image size (x,y) = (%d,%d)\n", xsize, ysize);
00150         
00151         // Resize for the window and init.
00152     arParamChangeSize(&wparam, xsize, ysize, cparam);
00153     fprintf(stdout, "*** Camera Parameter ***\n");
00154     arParamDisp(cparam);
00155         
00156     arInitCparam(cparam);
00157         
00158         if (arVideoCapStart() != 0) {
00159         fprintf(stderr, "setupCamera(): Unable to begin camera data capture.\n");
00160                 return (FALSE);         
00161         }
00162         
00163         return (TRUE);
00164 }
00165 
00166 static void Quit(void)
00167 {
00168         free(gARTsaveImage); gARTsaveImage = NULL;
00169         arglCleanup(gArglSettings);
00170         arVideoCapStop();
00171         arVideoClose();
00172         exit(0);
00173 }
00174 
00175 static void Keyboard(unsigned char key, int x, int y)
00176 {
00177         switch (key) {
00178                 case 0x1B:                                              // Quit.
00179                 case 'Q':
00180                 case 'q':
00181                         Quit();
00182                         break;
00183                 case 'T':
00184                 case 't':
00185                 printf("Enter new threshold value (default = 100): ");
00186                         scanf("%d", &gARTThreshhold); while (getchar()!='\n');
00187                                 printf("\n");
00188                         break;
00189                 case '?':
00190                 case '/':
00191                         printf("Keys:\n");
00192                         printf(" q or [esc]    Quit demo.\n");
00193                         printf(" t             Enter new binarization threshold value.\n");
00194                         printf(" ? or /        Show this help.\n");
00195                         printf("\nAdditionally, the ARVideo library supplied the following help text:\n");
00196                         arVideoDispOption();
00197                         break;
00198                 default:
00199                         break;
00200     }
00201 }
00202 
00203 static void Mouse(int button, int state, int x, int y)
00204 {
00205     char   name1[256], name2[256];
00206         
00207         if (state == GLUT_DOWN) {
00208                 if (button == GLUT_RIGHT_BUTTON) {
00209                         Quit();
00210                 } else if (button == GLUT_MIDDLE_BUTTON) {
00211                         printf("Enter new threshold value (default = 100): ");
00212                         scanf("%d", &gARTThreshhold); while (getchar() != '\n');
00213                         printf("\n");
00214                 } else if (button == GLUT_LEFT_BUTTON && gARTsaveImage && gTarget) {
00215                         printf("Enter filename: ");
00216                         if (fgets(name1, 256, stdin) == NULL) return;
00217                         if (sscanf(name1, "%s", name2) != 1 ) return;
00218                         if (arSavePatt(gARTsaveImage, gTarget, name2) < 0) {
00219                                 printf("ERROR!!\n");
00220                         } else {
00221                                 printf("  Saved\n");
00222                         }
00223                 }
00224         }
00225 }
00226 
00227 static void Idle(void)
00228 {
00229         static int ms_prev;
00230         int ms;
00231         float s_elapsed;
00232         ARUint8 *image;
00233     int             areamax;
00234         ARMarkerInfo    *marker_info;                                   // Pointer to array holding the details of detected markers.
00235     int             marker_num;                                         // Count of number of markers detected.
00236     int             i;
00237         
00238         // Find out how long since Idle() last ran.
00239         ms = glutGet(GLUT_ELAPSED_TIME);
00240         s_elapsed = (float)(ms - ms_prev) * 0.001;
00241         if (s_elapsed < 0.01f) return; // Don't update more often than 100 Hz.
00242         ms_prev = ms;
00243         
00244         // Grab a video frame.
00245         if ((image = arVideoGetImage()) != NULL) {
00246                 gARTImage = image;
00247                 
00248                 if (arDetectMarker(gARTImage, gARTThreshhold, &marker_info, &marker_num) < 0) {
00249                         Quit();
00250                 }
00251                 
00252                 areamax = 0;
00253                 gTarget = NULL;
00254                 for (i = 0; i < marker_num; i++) {
00255                         if (marker_info[i].area > areamax) {
00256                                 areamax = marker_info[i].area;
00257                                 gTarget = &(marker_info[i]);
00258                         }
00259                 }
00260                 memcpy(gARTsaveImage, gARTImage,  gARTCparam.xsize * gARTCparam.ysize * AR_PIX_SIZE_DEFAULT);
00261                 
00262                 // Tell GLUT the display has changed.
00263                 glutPostRedisplay();
00264         }
00265 }
00266 
00267 //
00268 //      This function is called on events when the visibility of the
00269 //      GLUT window changes (including when it first becomes visible).
00270 //
00271 static void Visibility(int visible)
00272 {
00273         if (visible == GLUT_VISIBLE) {
00274                 glutIdleFunc(Idle);
00275         } else {
00276                 glutIdleFunc(NULL);
00277         }
00278 }
00279 
00280 //
00281 //      This function is called when the
00282 //      GLUT window is resized.
00283 //
00284 static void Reshape(int w, int h)
00285 {
00286         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00287         glViewport(0, 0, (GLsizei) w, (GLsizei) h);
00288         
00289         glMatrixMode(GL_PROJECTION);
00290         glLoadIdentity();
00291         glMatrixMode(GL_MODELVIEW);
00292         glLoadIdentity();
00293         
00294         // Call through to anyone else who needs to know about window sizing here.
00295 }
00296 
00297 static void beginOrtho2D(int xsize, int ysize) {
00298         glMatrixMode(GL_PROJECTION);
00299         glPushMatrix();
00300         glLoadIdentity();
00301         gluOrtho2D(0.0, xsize, 0.0, ysize);
00302         glMatrixMode(GL_MODELVIEW);
00303         glPushMatrix();
00304         glLoadIdentity();       
00305 }
00306 
00307 static void endOrtho2D(void) {
00308         glMatrixMode(GL_PROJECTION);
00309         glPopMatrix();
00310         glMatrixMode(GL_MODELVIEW);
00311         glPopMatrix();
00312 }
00313 
00314 //
00315 // This function is called when the window needs redrawing.
00316 //
00317 static void Display(void)
00318 {
00319         // Select correct buffer for this context.
00320         glDrawBuffer(GL_BACK);
00321         glClear(GL_COLOR_BUFFER_BIT); // Clear the buffers for new frame.
00322         
00323         arglDispImage(gARTImage, &gARTCparam, 1.0, gArglSettings);      // zoom = 1.0.
00324         arVideoCapNext();
00325         gARTImage = NULL; // Image data is no longer valid after calling arVideoCapNext().
00326 
00327         if (gTarget != NULL) {
00328                 glDisable(GL_DEPTH_TEST);
00329                 glDisable(GL_LIGHTING);
00330                 glDisable(GL_TEXTURE_2D);
00331                 beginOrtho2D(gARTCparam.xsize, gARTCparam.ysize);
00332         glLineWidth(2.0f);
00333         glColor3d(0.0, 1.0, 0.0);
00334         lineSeg(gTarget->vertex[0][0], gTarget->vertex[0][1],
00335                                 gTarget->vertex[1][0], gTarget->vertex[1][1], gArglSettings, gARTCparam, 1.0);
00336         lineSeg(gTarget->vertex[3][0], gTarget->vertex[3][1],
00337                                 gTarget->vertex[0][0], gTarget->vertex[0][1], gArglSettings, gARTCparam, 1.0);
00338         glColor3d(1.0, 0.0, 0.0);
00339         lineSeg(gTarget->vertex[1][0], gTarget->vertex[1][1],
00340                                 gTarget->vertex[2][0], gTarget->vertex[2][1], gArglSettings, gARTCparam, 1.0);
00341         lineSeg(gTarget->vertex[2][0], gTarget->vertex[2][1],
00342                                 gTarget->vertex[3][0], gTarget->vertex[3][1], gArglSettings, gARTCparam, 1.0);
00343                 endOrtho2D();
00344     }
00345 
00346         glutSwapBuffers();
00347 }
00348 
00349 int main(int argc, char *argv[])
00350 {
00351         // ----------------------------------------------------------------------------
00352         // Library inits.
00353         //
00354         
00355         glutInit(&argc, argv);
00356         
00357         // ----------------------------------------------------------------------------
00358         // Hardware setup.
00359         //
00360         
00361         if (!setupCamera(&gARTCparam)) {
00362                 fprintf(stderr, "main(): Unable to set up AR camera.\n");
00363                 exit(-1);
00364         }
00365         
00366         // ----------------------------------------------------------------------------
00367         // Library setup.
00368         //
00369         
00370         // Set up GL context(s) for OpenGL to draw into.
00371     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
00372         glutInitWindowSize(gARTCparam.xsize, gARTCparam.ysize);
00373         glutCreateWindow(argv[0]);
00374         
00375         // Setup argl library for current context.
00376         if ((gArglSettings = arglSetupForCurrentContext()) == NULL) {
00377                 fprintf(stderr, "main(): arglSetupForCurrentContext() returned error.\n");
00378                 exit(-1);
00379         }
00380         
00381         arMalloc(gARTsaveImage, ARUint8, gARTCparam.xsize * gARTCparam.ysize * AR_PIX_SIZE_DEFAULT);
00382         
00383         // Register GLUT event-handling callbacks.
00384         // NB: Idle() is registered by Visibility.
00385         glutDisplayFunc(Display);
00386         glutReshapeFunc(Reshape);
00387         glutVisibilityFunc(Visibility);
00388         glutKeyboardFunc(Keyboard);
00389     glutMouseFunc(Mouse);
00390         
00391         glutMainLoop();
00392         
00393         return (0);
00394 }
00395 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


ar_recog
Author(s): Graylin Trevor Jay and Christopher Crick
autogenerated on Fri Jan 25 2013 12:15:00