[ARToolkit] The first instance simpletest

This first instance of ARToolkit, simpletext, is used to familiarize yourself with ARToolkit-related knowledge. Demonstrate in detail how to develop an ARToolKit application.

 ARToolKit application can be mainly divided into six steps  

Step 1. Application initialization. The init() function implements the initializer.


Step 2. Grab a frame of input video. Implemented by arVideoGetImage() function

Step 3. Probe the ID card. Implemented by the arDetectMarker() function

Step 4. Calculate the transition matrix of the camera. Implemented by arGetTransMat() function

Step 5. Draw the virtual object. Implemented by the draw() function

Step 6. Turn off video capture. Implemented by the cleanup() function


Among them, steps 2 to 5 are located in the mainloop function, and step 1 is located in the init() function alone. Step 6 is in the cleanup() function

In this simpletest program, the most important functions are main , init , mainloop , draw and cleanup. In this section we will explain these function calls in detail. Let everyone familiar with the program coding of ARToolkit

This program can only be recognized by the Hiro identification card. As for only being recognized by Hiro, it cannot be recognized by other identification cards. I will talk about it in my other article, the production of identification cards.

Paste the program code below:

//Limitations of this program: it only uses the template Hiro
//The program simpletest uses template matching to identify the word Hiro in the identification box
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef __APPLE__
#include <GL/gl.h>
#include <GL/glut.h>
#else
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#endif
#include <AR/gsub.h>
#include <AR/video.h>
#include <AR/param.h>
#include <AR/ar.h>


//Key parameters need to be initialized, these parameters may be used for template Pattern matching template information, and virtual objects corresponding to these template locks
//initialize the camera feature parameters of the video camera
//
// Camera configuration.
//
#ifdef _WIN32
char *vconf = "Data\\WDM_camera_flipV.xml";
#else
char *vconf = "";//For each platform, a default string is defined, which generally opens the first available video stream in your application structure.
#endif


int xsize, ysize;
int thresh = 100;
int count = 0;


char *cparam_name = "Data/camera_para.dat";
ARParam cparam;


char *patt_name = "Data/patt.hiro";
int patt_id;
double patt_width = 80.0;
double patt_center[2] = {0.0, 0.0};
double patt_trans[3][4];


static void init(void);
static void cleanup(void);
static void keyEvent( unsigned char key, int x, int y );
static void mainLoop(void);
static void draw( void );


int main(int argc, char **argv)
{
glutInit(&argc, argv);
init();//Initialize


    arVideoCapStart();//Call the video start function to enter the real-time state
    argMainLoop( NULL, keyEvent, mainLoop );//Start the main program loop, keyboard events and function keyEvent Combined use, combined with mainLoop through the main image display loop, argMainLoop is defined in gsub.c
return (0);
}


static void keyEvent( unsigned char key, int x, int y)
{
    /* quit if the ESC key is pressed */
    if( key == 0x1b ) {
        printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
        cleanup();
        exit(0);
    }
}


/* main loop */
//Most of the ARTookit application is completed in this routine, which includes the second to fifth steps of the development principle (2 capture a frame of input video, 3 detect the identification card, 4 calculate The camera's transition matrix, 5 draws virtual objects)
static void mainLoop(void)
{
    ARUint8 *dataPtr;
    ARMarkerInfo *marker_info;
    int marker_num;
    int j, k;


    /* grab a vide frame */
//Grab a frame of input video frame
/*The video image is immediately output to the display screen. This image can be an undistorted image, or it can be distorted and corrected based on camera distortion information. Distorting to correct the image can produce a more normal image, but may result in a significantly slower video frame rate */
    if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
        arUtilSleep(2);
        return;
    }
    if ( count == 0 ) arUtilTimerReset();
    count++;
//this image has been distorted
    argDrawMode2D();
    argDispImage( dataPtr, 0,0 );


    /* detect the markers in the video frame */
//search the entire image to Find squares for logo templates with correct meaning
    if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {//marker_num is used to store the number of markers, marker_info is a pointer to a list of marker structures, this structure contains coordinate information to identify credible degree, as well as the identification information and objects corresponding to each logo. Details of marker_info are in the API documentation.
        cleanup();
        exit(0);
    }
//At this point, the video image has been displayed and analyzed. So we don't need to use it anymore: we can use the frame grabber to start a new frame capture operation at the same time as using the new function, to complete the work, we just need to call the function arVideoCapNext
    arVideoCapNext(); // use the frame grabber Start a new frame capture operation
// (Note: when we call this function, using the previous video image buffer will lead to bad results, make sure you have dealt with the video image buffer)
    /* check for object visibility */
/ / The reliability information of all detected markers is compared, and finally the correct marker identification information is determined as the most reliable marker identification information:
    k = -1;
    for( j = 0; j < marker_num; j++ ) {
        if( patt_id == marker_info[j].id ) {
            if( k == -1 ) k = j;
            else if( marker_info[k].cf < marker_info[j].cf ) k = j;
        }
    }
//如果没有标识被找到(k==-1),应用程序会做一个简单的优化步骤,我们可以交换缓冲器而不需要调用函数draw,然后返回
    if( k == -1 ) {
        argSwapBuffers();
        return;
    }


    /* get the transformation between the marker and the real camera */
//计算摄像头的转移矩阵,标识卡和摄像机之间的转移信息通过使用函数arGetTransMat来获取
    arGetTransMat(&marker_info[k], patt_center, patt_width, patt_trans);//相对于标识物体 i 的真实的摄像机的位置和姿态包含在一个 3*4 的矩阵 patt_trans 中。


//使用画图函数,虚拟物体可以被叠加在标示卡上
    draw();//画上虚拟物体


    argSwapBuffers();
}
//初始化视频捕捉,读入ARTookit应用的初始参数信息
static void init( void )//应用程序初始化
{
//读取标识卡信息,摄像机参数信息,设置图像窗口
    ARParam wparam;     /* open the video path */ //The video channel is opened     if( arVideoOpen( vconf ) < 0 ) exit(0);     /* find the size of the window */ // Determine the video image size     if( arVideoInqSize (&xsize, &ysize) < 0 ) exit(0);     printf("Image size (x,y) = (%d,%d)\n", xsize, ysize); //The information is read from the file Take, the names of these files can be specified on the command line, or use the default names of hardware-encoded files     /* set the initial camera parameters */ //The parameter information of the camera passes the default camera parameter file name Data/camer_para.dat is read in     if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {         printf("Camera parameter load error !!\n");         exit(0);     } //These parameters are converted according to the existing image size, Because the parameters of the camera change according to the size of the image, even with the same camera     arParamChangeSize( &wparam, xsize, ysize,&cparam );























//The parameters of the camera are read into its program settings, and the parameters of the camera are output and displayed on the screen
    arInitCparam( &cparam );
    printf("*** Camera Parameter ***\n");
    arParamDisp( &cparam );
// where patt_id is the identification information of a recognized template (tells us which template it is)
    if( (patt_id=arLoadPatt(patt_name)) < 0 ) {
        printf("pattern load error !!\n");
        exit(0 );
    }


    /* open the graphics window */
//Open the window
    argInit( &cparam, 1.0, 0, 0, 0, 0 );//The second parameter of argInit defines a zoom information that adapts to the value of the video image format Set to 1.0, and a value of 2.0 doubles the size (eg, input 320*240 image, output in VGA AR format). 
}


/* cleanup function called when program exits */
//Close video capture
//The function is to stop video processing and close the video path and release it, so that other applications can use
static void cleanup(void)
{
    arVideoCapStop();
    arVideoClose();
    argCleanup();
}
//Draw a virtual object
static void draw( void )
{
//Function draw is divided into display environment initialization, setting matrix, and displaying objects. We can use ARToolkit to display a 3D object and Set the minimum OpenGL state to initialize a 3d display:
    double gl_para[16];
    GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
    GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
    GLfloat mat_flash_shiny[ ] = {50.0};
    GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
    GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
    GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1 };
   
    argDrawMode3D();
    argDraw3dCamera( 0, 0 );
    glClearDepth( 1.0 );
    glClear(GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);     /* load the camera transformation matrix */     argConvGlpara(patt_trans, gl_para);//We need to convert this transition matrix (3*4 matrix) into a format suitable for OpenGL (16 vector of values), which can be done with the function argConvGlpara. /* These sixteen values ​​are the position and attitude information of the camera in the real world, so the position of the camera in the virtual world can be set by using this information, so any graphic object can be accurately placed on the corresponding real identification card*/     glMatrixMode(GL_MODELVIEW);     glLoadMatrixd( gl_para );//The position of the camera in the virtual world is set by the function glLoadMatrixd(gl_para). The end of the code is the display of the 3D object //display a blue cube under the white beam     glEnable(GL_LIGHTING);     glEnable(GL_LIGHT0);     glLightfv(GL_LIGHT0, GL_POSITION, light_position);     glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);     glLightfv(GL_LIGHT0 , GL_DIFFUSE, lightZeroColor);
 















    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);     glMatrixMode(GL_MODELVIEW);     glTranslatef( 0.0, 0.0, 25.0 );     glutSolidCube(50.0); //reset Some OpenGL parameters are default values:     glDisable( GL_LIGHTING );     glDisable( GL_DEPTH_TEST ); //The above steps appear and run through the main display function. When this program is running, mouse events are controlled by mouse event functions, and keyboard events Controlled by keyboard functions










}

There are some important functions or variables that need to be mentioned here, such as:

1. The vconf parameter defined at the beginning, the function of this parameter is to open the first available video stream of the application structure. For each platform, a default string is defined.

2. The parameter information is also initialized in ARToolkit, but these parameters are the video camera and template information and the virtual objects that appear

3. patt_id is the identification information of a template that has been recognized (tell us which template it is, equivalent to a human ID card)

4. The second parameter of the function arginit defines a scaling function, the value is set to 1.0 when adapting to the video image format, and the double size when the value is set to 2.0

5. Among them, when the function arVideoGetImage captures the input video frame, the captured image can be an undistorted image, or an image that has been distorted and corrected according to the distortion information of the camera. Warping to correct the image produces a more normal image, but may result in a significantly slower video frame rate, where warping and correcting produces a better image




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325616477&siteId=291194637