Beispiel #1
0
static int setupCamera(const char *cparam_name, char *vconf, ARParamLT **cparamLT_p)
{	
    ARParam			cparam;
	int				xsize, ysize;
    AR_PIXEL_FORMAT pixFormat;

    // Open the video path.
    if (arVideoOpen(vconf) < 0) {
    	ARLOGe("setupCamera(): Unable to open connection to camera.\n");
    	return (FALSE);
	}
	
    // Find the size of the window.
    if (arVideoGetSize(&xsize, &ysize) < 0) {
        ARLOGe("setupCamera(): Unable to determine camera frame size.\n");
        arVideoClose();
        return (FALSE);
    }
    ARLOGi("Camera image size (x,y) = (%d,%d)\n", xsize, ysize);
	
	// Get the format in which the camera is returning pixels.
	pixFormat = arVideoGetPixelFormat();
	if (pixFormat == AR_PIXEL_FORMAT_INVALID) {
    	ARLOGe("setupCamera(): Camera is using unsupported pixel format.\n");
        arVideoClose();
		return (FALSE);
	}
	
	// Load the camera parameters, resize for the window and init.
    if (arParamLoad(cparam_name, 1, &cparam) < 0) {
		ARLOGe("setupCamera(): Error loading parameter file %s for camera.\n", cparam_name);
        arVideoClose();
        return (FALSE);
    }
    if (cparam.xsize != xsize || cparam.ysize != ysize) {
        ARLOGw("*** Camera Parameter resized from %d, %d. ***\n", cparam.xsize, cparam.ysize);
        arParamChangeSize(&cparam, xsize, ysize, &cparam);
    }
#ifdef DEBUG
    ARLOG("*** Camera Parameter ***\n");
    arParamDisp(&cparam);
#endif
    if ((*cparamLT_p = arParamLTCreate(&cparam, AR_PARAM_LT_DEFAULT_OFFSET)) == NULL) {
        ARLOGe("setupCamera(): Error: arParamLTCreate.\n");
        arVideoClose();
        return (FALSE);
    }
	
	return (TRUE);
}
Beispiel #2
0
static void printMode()
{
    int len, thresh, line, mode, xsize, ysize;
    AR_LABELING_THRESH_MODE threshMode;
    ARdouble tempF;
    char text[256], *text_p;
	float hMargin = 2.0f;
	float vMargin = 2.0f;
    
    glColor4ub(255, 255, 255, 255);
    line = 1;
    
    // Image size and processing mode.
    arVideoGetSize(&xsize, &ysize);
    arGetImageProcMode(gARHandle, &mode);
	if (mode == AR_IMAGE_PROC_FRAME_IMAGE) text_p = "full frame";
	else text_p = "even field only";
    snprintf(text, sizeof(text), "Processing %dx%d video frames %s", xsize, ysize, text_p);
    print(text, hMargin, (line - 1)*(FONT_SIZE + FONT_LINE_SPACING) + vMargin, 0, 1);
    line++;
    
    // Threshold mode, and threshold, if applicable.
    arGetLabelingThreshMode(gARHandle, &threshMode);
    switch (threshMode) {
        case AR_LABELING_THRESH_MODE_MANUAL: text_p = "MANUAL"; break;
        case AR_LABELING_THRESH_MODE_AUTO_MEDIAN: text_p = "AUTO_MEDIAN"; break;
        case AR_LABELING_THRESH_MODE_AUTO_OTSU: text_p = "AUTO_OTSU"; break;
        case AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE: text_p = "AUTO_ADAPTIVE"; break;
        case AR_LABELING_THRESH_MODE_AUTO_BRACKETING: text_p = "AUTO_BRACKETING"; break;
        default: text_p = "UNKNOWN"; break;
    }
    snprintf(text, sizeof(text), "Threshold mode: %s", text_p);
    if (threshMode != AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE) {
        arGetLabelingThresh(gARHandle, &thresh);
        len = (int)strlen(text);
        snprintf(text + len, sizeof(text) - len, ", thresh=%d", thresh);
    }
    print(text, hMargin, (line - 1)*(FONT_SIZE + FONT_LINE_SPACING) + vMargin, 0, 1);
    line++;
    
    // Border size, image processing mode, pattern detection mode.
    arGetBorderSize(gARHandle, &tempF);
    snprintf(text, sizeof(text), "Border: %0.1f%%", tempF*100.0);
    arGetPatternDetectionMode(gARHandle, &mode);
    switch (mode) {
        case AR_TEMPLATE_MATCHING_COLOR: text_p = "Colour template (pattern)"; break;
        case AR_TEMPLATE_MATCHING_MONO: text_p = "Mono template (pattern)"; break;
        case AR_MATRIX_CODE_DETECTION: text_p = "Matrix (barcode)"; break;
        case AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX: text_p = "Colour template + Matrix (2 pass, pattern + barcode)"; break;
        case AR_TEMPLATE_MATCHING_MONO_AND_MATRIX: text_p = "Mono template + Matrix (2 pass, pattern + barcode "; break;
        default: text_p = "UNKNOWN"; break;
    }
    len = (int)strlen(text);
    snprintf(text + len, sizeof(text) - len, ", Pattern detection mode: %s", text_p);
    print(text, hMargin,  (line - 1)*(FONT_SIZE + FONT_LINE_SPACING) + vMargin, 0, 1);
    line++;
    
    // Window size.
    snprintf(text, sizeof(text), "Drawing into %dx%d window", gWindowW, gWindowH);
    print(text, hMargin,  (line - 1)*(FONT_SIZE + FONT_LINE_SPACING) + vMargin, 0, 1);
    line++;
    
}
Beispiel #3
0
static void   init(int argc, char *argv[])
{
    ARParam         cparam;
    ARGViewport     viewport;
    ARPattHandle    *arPattHandle;
    char            vconf[512];
    AR_PIXEL_FORMAT pixFormat;
    ARUint32        id0, id1;
    int             i;

    if (argc == 1)
        vconf[0] = '\0';
    else
    {
        strcpy(vconf, argv[1]);

        for (i = 2; i < argc; i++)
        {
            strcat(vconf, " "); strcat(vconf, argv[i]);
        }
    }

    /* open the video path */
    if (arVideoOpen(vconf) < 0)
        exit(0);

    if (arVideoGetSize(&xsize, &ysize) < 0)
        exit(0);

    ARLOG("Image size (x,y) = (%d,%d)\n", xsize, ysize);
    if ((pixFormat = arVideoGetPixelFormat()) < 0)
        exit(0);

    if (arVideoGetId(&id0, &id1) == 0)
    {
        ARLOG("Camera ID = (%08x, %08x)\n", id1, id0);
        sprintf(vconf, VPARA_NAME, id1, id0);
        if (arVideoLoadParam(vconf) < 0)
        {
            ARLOGe("No camera setting data!!\n");
        }
    }

    /* set the initial camera parameters */
    if (arParamLoad(CPARA_NAME, 1, &cparam) < 0)
    {
        ARLOGe("Camera parameter load error !!\n");
        exit(0);
    }

    arParamChangeSize(&cparam, xsize, ysize, &cparam);
    ARLOG("*** Camera Parameter ***\n");
    arParamDisp(&cparam);
    if ((gCparamLT = arParamLTCreate(&cparam, AR_PARAM_LT_DEFAULT_OFFSET)) == NULL)
    {
        ARLOGe("Error: arParamLTCreate.\n");
        exit(-1);
    }

    if ((arHandle = arCreateHandle(gCparamLT)) == NULL)
    {
        ARLOGe("Error: arCreateHandle.\n");
        exit(0);
    }

    if (arSetPixelFormat(arHandle, pixFormat) < 0)
    {
        ARLOGe("Error: arSetPixelFormat.\n");
        exit(0);
    }

    if ((ar3DHandle = ar3DCreateHandle(&cparam)) == NULL)
    {
        ARLOGe("Error: ar3DCreateHandle.\n");
        exit(0);
    }

    if ((arPattHandle = arPattCreateHandle()) == NULL)
    {
        ARLOGe("Error: arPattCreateHandle.\n");
        exit(0);
    }

    if ((patt_id = arPattLoad(arPattHandle, PATT_NAME)) < 0)
    {
        ARLOGe("pattern load error !!\n");
        exit(0);
    }

    arPattAttach(arHandle, arPattHandle);

    /* open the graphics window */
    w1             = argCreateWindow(xsize, ysize);
    viewport.sx    = 0;
    viewport.sy    = 0;
    viewport.xsize = xsize;
    viewport.ysize = ysize;
    if ((vp1 = argCreateViewport(&viewport)) == NULL)
        exit(0);

    argViewportSetCparam(vp1, &cparam);
    argViewportSetPixFormat(vp1, pixFormat);
    argViewportSetDispMethod(vp1, AR_GL_DISP_METHOD_TEXTURE_MAPPING_FRAME);
    argViewportSetDispMode(vp1, AR_GL_DISP_MODE_FIT_TO_VIEWPORT);
    argViewportSetDistortionMode(vp1, AR_GL_DISTORTION_COMPENSATE_ENABLE);

    w2             = argCreateWindow(xsize, ysize);
    viewport.sx    = 0;
    viewport.sy    = 0;
    viewport.xsize = xsize;
    viewport.ysize = ysize;
    if ((vp2 = argCreateViewport(&viewport)) == NULL)
        exit(0);

    argViewportSetCparam(vp2, &cparam);
    argViewportSetPixFormat(vp2, pixFormat);
    argViewportSetDispMethod(vp2, AR_GL_DISP_METHOD_TEXTURE_MAPPING_FRAME);
    argViewportSetDispMode(vp2, AR_GL_DISP_MODE_FIT_TO_VIEWPORT);
    argViewportSetDistortionMode(vp2, AR_GL_DISTORTION_COMPENSATE_DISABLE);

    return;
}
Beispiel #4
0
static void printMode()
{
    int len, thresh, line, mode, xsize, ysize;
    AR_LABELING_THRESH_MODE threshMode;
    ARdouble tempF;
    char text[256], *text_p;

    glColor3ub(255, 255, 255);
    line = 1;
    
    // Image size and processing mode.
    arVideoGetSize(&xsize, &ysize);
    arGetImageProcMode(gARHandle, &mode);
	if (mode == AR_IMAGE_PROC_FRAME_IMAGE) text_p = "full frame";
	else text_p = "even field only";
    snprintf(text, sizeof(text), "Processing %dx%d video frames %s", xsize, ysize, text_p);
    print(text, 2.0f,  (line - 1)*12.0f + 2.0f, 0, 1);
    line++;
    
    // Threshold mode, and threshold, if applicable.
    arGetLabelingThreshMode(gARHandle, &threshMode);
    switch (threshMode) {
        case AR_LABELING_THRESH_MODE_MANUAL: text_p = "MANUAL"; break;
        case AR_LABELING_THRESH_MODE_AUTO_MEDIAN: text_p = "AUTO_MEDIAN"; break;
        case AR_LABELING_THRESH_MODE_AUTO_OTSU: text_p = "AUTO_OTSU"; break;
        case AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE: text_p = "AUTO_ADAPTIVE"; break;
        case AR_LABELING_THRESH_MODE_AUTO_BRACKETING: text_p = "AUTO_BRACKETING"; break;
        default: text_p = "UNKNOWN"; break;
    }
    snprintf(text, sizeof(text), "Threshold mode: %s", text_p);
    if (threshMode != AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE) {
        arGetLabelingThresh(gARHandle, &thresh);
        len = (int)strlen(text);
        snprintf(text + len, sizeof(text) - len, ", thresh=%d", thresh);
    }
    print(text, 2.0f,  (line - 1)*12.0f + 2.0f, 0, 1);
    line++;
    
    // Border size, image processing mode, pattern detection mode.
    arGetBorderSize(gARHandle, &tempF);
    snprintf(text, sizeof(text), "Border: %0.1f%%", tempF*100.0);
    arGetPatternDetectionMode(gARHandle, &mode);
    switch (mode) {
        case AR_TEMPLATE_MATCHING_COLOR: text_p = "Colour template (pattern)"; break;
        case AR_TEMPLATE_MATCHING_MONO: text_p = "Mono template (pattern)"; break;
        case AR_MATRIX_CODE_DETECTION: text_p = "Matrix (barcode)"; break;
        case AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX: text_p = "Colour template + Matrix (2 pass, pattern + barcode)"; break;
        case AR_TEMPLATE_MATCHING_MONO_AND_MATRIX: text_p = "Mono template + Matrix (2 pass, pattern + barcode "; break;
        default: text_p = "UNKNOWN"; break;
    }
    len = (int)strlen(text);
    snprintf(text + len, sizeof(text) - len, ", Pattern detection mode: %s", text_p);
    print(text, 2.0f,  (line - 1)*12.0f + 2.0f, 0, 1);
    line++;
    
    // Draw mode.
    if (arglDrawModeGet(gArglSettings) == AR_DRAW_BY_GL_DRAW_PIXELS) text_p = "GL_DRAW_PIXELS";
    else {
        if (arglTexmapModeGet(gArglSettings) == AR_DRAW_TEXTURE_FULL_IMAGE) text_p = "texture mapping";
        else text_p = "texture mapping (even field only)";
    }
    snprintf(text, sizeof(text), "Drawing using %s into %dx%d window", text_p, windowWidth, windowHeight);
    print(text, 2.0f,  (line - 1)*12.0f + 2.0f, 0, 1);
    line++;
    
}
Beispiel #5
0
static void init( int argc, char *argv[] )
{
    char         vconf[256];
    ARGViewport  viewport;
    ARUint32     euid0, euid1;
    double       length;
    int          i, j;

	// Allow user to choose file version for backwards compatibility.
    ARLOG("A calibrated camera parameter file for ARToolKit will be produced.\n");
    ARLOG("The file can be generated to be backwards compatible with previous ARToolKit versions.\n");
    ARLOG("    1: ARToolKit v1.0 and later\n");
    ARLOG("    2: ARToolKit v4.0 and later\n");
    ARLOG("    3: ARToolKit v4.1 and later\n");
	printf("Enter the number (1 to %d) of the camera parameter file version: ", AR_DIST_FUNCTION_VERSION_MAX); 
	scanf("%d", &dist_function_version); while (getchar() != '\n');
	
	patt.h_num    = H_NUM;
    patt.v_num    = V_NUM;
    patt.loop_num = 0;
    if( patt.h_num < 3 || patt.v_num < 3 ) exit(0);

    printf("Input the distance between each marker dot, in millimeters: ");
    scanf("%lf", &length); while( getchar()!='\n' );
    patt.world_coord = (CALIB_COORD_T *)malloc( sizeof(CALIB_COORD_T)*patt.h_num*patt.v_num );
    for( j = 0; j < patt.v_num; j++ ) {
        for( i = 0; i < patt.h_num; i++ ) {
            patt.world_coord[j*patt.h_num+i].x_coord =  length * i;
            patt.world_coord[j*patt.h_num+i].y_coord =  length * j;
        }
    }

    if( argc == 1 ) {
        strcpy(vconf, VCONF);
    }
    else {
        strcpy(vconf, argv[1]);
        for( i = 2; i < argc; i++ ) {
            strcat(vconf, " ");
            strcat(vconf, argv[i]);
        }
    }
    if( arVideoOpen(vconf) < 0  ) exit(0);
    if( arVideoGetSize(&xsize, &ysize) < 0 ) exit(0);
    if( (pixelFormat=arVideoGetPixelFormat()) < 0 ) exit(0);
    if( (pixelSize=arVideoGetPixelSize()) < 0 ) exit(0);
    ARLOG("Camera image size (x,y) = (%d,%d)\n", xsize, ysize);

    viewport.sx = 0;
    viewport.sy = 0;
    viewport.xsize = xsize/SCALE;
    viewport.ysize = ysize/SCALE;
    if( (vp=argCreateViewport(&viewport)) == NULL ) exit(0);
    argViewportSetImageSize( vp, xsize, ysize );
    argViewportSetPixFormat( vp, pixelFormat );
    argViewportSetDispMethod( vp, AR_GL_DISP_METHOD_GL_DRAW_PIXELS );
    argViewportSetDispMode( vp, AR_GL_DISP_MODE_FIT_TO_VIEWPORT );
    argViewportSetDistortionMode( vp, AR_GL_DISTORTION_COMPENSATE_DISABLE );

    clipImage = (unsigned char *)malloc( xsize*ysize*pixelSize );
    if( clipImage == NULL ) exit(0);

    save_filename[0] = '\0';
    if( arVideoGetId(&euid0, &euid1) == 0 ) {
        sprintf(save_filename, "cpara.%08x%08x", euid0, euid1);
    }
}
Beispiel #6
0
static void printMode()
{
    int                     len, thresh, line, mode, xsize, ysize, textPatternCount;
    AR_LABELING_THRESH_MODE threshMode;
    ARdouble                tempF;
    char                    text[256], *text_p;

    glColor3ub(255, 255, 255);
    line = 1;

    // Image size and processing mode.
    arVideoGetSize(&xsize, &ysize);
    arGetImageProcMode(gARHandle, &mode);
    if (mode == AR_IMAGE_PROC_FRAME_IMAGE)
        text_p = "full frame";
    else
        text_p = "even field only";

    snprintf(text, sizeof(text), "Processing %dx%d video frames %s", xsize, ysize, text_p);
    print(text, 2.0f,  (line - 1) * 12.0f + 2.0f, 0, 1);
    line++;

    // Threshold mode, and threshold, if applicable.
    arGetLabelingThreshMode(gARHandle, &threshMode);

    switch (threshMode)
    {
    case AR_LABELING_THRESH_MODE_MANUAL: text_p = "MANUAL"; break;

    case AR_LABELING_THRESH_MODE_AUTO_MEDIAN: text_p = "AUTO_MEDIAN"; break;

    case AR_LABELING_THRESH_MODE_AUTO_OTSU: text_p = "AUTO_OTSU"; break;

    case AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE: text_p = "AUTO_ADAPTIVE"; break;

    case AR_LABELING_THRESH_MODE_AUTO_BRACKETING: text_p = "AUTO_BRACKETING"; break;

    default: text_p = "UNKNOWN"; break;
    }

    snprintf(text, sizeof(text), "Threshold mode: %s", text_p);
    if (threshMode != AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE)
    {
        arGetLabelingThresh(gARHandle, &thresh);
        len = (int)strlen(text);
        snprintf(text + len, sizeof(text) - len, ", thresh=%d", thresh);
    }

    print(text, 2.0f,  (line - 1) * 12.0f + 2.0f, 0, 1);
    line++;

    // Border size, image processing mode, pattern detection mode.
    arGetBorderSize(gARHandle, &tempF);
    snprintf(text, sizeof(text), "Border: %0.2f%%", tempF * 100.0);
    arGetPatternDetectionMode(gARHandle, &mode);
    textPatternCount = 0;

    switch (mode)
    {
    case AR_TEMPLATE_MATCHING_COLOR: text_p = "Colour template (pattern)"; break;

    case AR_TEMPLATE_MATCHING_MONO: text_p = "Mono template (pattern)"; break;

    case AR_MATRIX_CODE_DETECTION: text_p = "Matrix (barcode)"; textPatternCount = -1; break;

    case AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX: text_p = "Colour template + Matrix (2 pass, pattern + barcode)"; break;

    case AR_TEMPLATE_MATCHING_MONO_AND_MATRIX: text_p = "Mono template + Matrix (2 pass, pattern + barcode "; break;

    default: text_p = "UNKNOWN"; textPatternCount = -1; break;
    }

    if (textPatternCount != -1)
        textPatternCount = gARPattHandle->patt_num;

    len = (int)strlen(text);
    if (textPatternCount != -1)
        snprintf(text + len, sizeof(text) - len, ", Pattern detection mode: %s, %d patterns loaded", text_p, textPatternCount);
    else
        snprintf(text + len, sizeof(text) - len, ", Pattern detection mode: %s", text_p);

    print(text, 2.0f,  (line - 1) * 12.0f + 2.0f, 0, 1);
    line++;

    // Robust mode.
    if (gMultiConfigCount)
    {
        snprintf(text, sizeof(text), "Robust multi-marker pose estimation %s", (gRobustFlag ? "ON" : "OFF"));
        if (gRobustFlag)
        {
            icpGetInlierProbability(gAR3DHandle->icpHandle, &tempF);
            len = (int)strlen(text);
            snprintf(text + len, sizeof(text) - len, ", inliner prob. %0.1f%%", tempF * 100.0f);
        }

        print(text, 2.0f,  (line - 1) * 12.0f + 2.0f, 0, 1);
        line++;
    }

    // Window size.
    snprintf(text, sizeof(text), "Drawing into %dx%d window", windowWidth, windowHeight);
    print(text, 2.0f,  (line - 1) * 12.0f + 2.0f, 0, 1);
    line++;
}
Beispiel #7
0
static int setupCamera(const char *cparam_name, char *vconf, ARParamLT **cparamLT_p, ARHandle **arhandle, AR3DHandle **ar3dhandle)
{
    ARParam			cparam;
    int				xsize, ysize;
    AR_PIXEL_FORMAT pixFormat;

    // Open the video path.
    if (arVideoOpen(vconf) < 0) {
        ARLOGe("setupCamera(): Unable to open connection to camera.\n");
        return (FALSE);
    }

    // Find the size of the window.
    if (arVideoGetSize(&xsize, &ysize) < 0) {
        ARLOGe("setupCamera(): Unable to determine camera frame size.\n");
        arVideoClose();
        return (FALSE);
    }
    ARLOGi("Camera image size (x,y) = (%d,%d)\n", xsize, ysize);

    // Get the format in which the camera is returning pixels.
    pixFormat = arVideoGetPixelFormat();
    if (pixFormat == AR_PIXEL_FORMAT_INVALID) {
        ARLOGe("setupCamera(): Camera is using unsupported pixel format.\n");
        arVideoClose();
        return (FALSE);
    }

    // Load the camera parameters, resize for the window and init.
    if (arParamLoad(cparam_name, 1, &cparam) < 0) {
        ARLOGe("setupCamera(): Error loading parameter file %s for camera.\n", cparam_name);
        arVideoClose();
        return (FALSE);
    }
    if (cparam.xsize != xsize || cparam.ysize != ysize) {
        ARLOGw("*** Camera Parameter resized from %d, %d. ***\n", cparam.xsize, cparam.ysize);
        arParamChangeSize(&cparam, xsize, ysize, &cparam);
    }
#ifdef DEBUG
    ARLOG("*** Camera Parameter ***\n");
    arParamDisp(&cparam);
#endif
    if ((*cparamLT_p = arParamLTCreate(&cparam, AR_PARAM_LT_DEFAULT_OFFSET)) == NULL) {
        ARLOGe("setupCamera(): Error: arParamLTCreate.\n");
        arVideoClose();
        return (FALSE);
    }

    if ((*arhandle = arCreateHandle(*cparamLT_p)) == NULL) {
        ARLOGe("setupCamera(): Error: arCreateHandle.\n");
        return (FALSE);
    }
    if (arSetPixelFormat(*arhandle, pixFormat) < 0) {
        ARLOGe("setupCamera(): Error: arSetPixelFormat.\n");
        return (FALSE);
    }
    if (arSetDebugMode(*arhandle, AR_DEBUG_DISABLE) < 0) {
        ARLOGe("setupCamera(): Error: arSetDebugMode.\n");
        return (FALSE);
    }
    if (arSetImageProcMode(*arhandle, AR_IMAGE_PROC_FRAME_IMAGE) < 0) { // Change to AR_IMAGE_PROC_FIELD_IMAGE if using a DVCam.
        ARLOGe("setupCamera(): Error: arSetImageProcMode.\n");
        return (FALSE);
    }
    if ((*ar3dhandle = ar3DCreateHandle(&(*cparamLT_p)->param)) == NULL) {
        ARLOGe("setupCamera(): Error: ar3DCreateHandle.\n");
        return (FALSE);
    }

    if (arVideoCapStart() != 0) {
        ARLOGe("setupCamera(): Unable to begin camera data capture.\n");
        return (FALSE);
    }

    return (TRUE);
}
static void init(int argc, char *argv[])
{
    ARGViewport viewport;
    char        *vconf = NULL;
    int         i;
    int         gotTwoPartOption;
    int         screenWidth, screenHeight, screenMargin;

    chessboardCornerNumX = 0;
    chessboardCornerNumY = 0;
    calibImageNum        = 0;
    patternWidth         = 0.0f;

    arMalloc(cwd, char, MAXPATHLEN);
    if (!getcwd(cwd, MAXPATHLEN))
        ARLOGe("Unable to read current working directory.\n");
    else
        ARLOG("Current working directory is '%s'\n", cwd);

    i = 1; // argv[0] is name of app, so start at 1.

    while (i < argc)
    {
        gotTwoPartOption = FALSE;
        // Look for two-part options first.
        if ((i + 1) < argc)
        {
            if (strcmp(argv[i], "--vconf") == 0)
            {
                i++;
                vconf            = argv[i];
                gotTwoPartOption = TRUE;
            }
        }

        if (!gotTwoPartOption)
        {
            // Look for single-part options.
            if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0)
            {
                usage(argv[0]);
            }
            else if (strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0)
            {
                ARLOG("%s version %s\n", argv[0], AR_HEADER_VERSION_STRING);
                exit(0);
            }
            else if (strncmp(argv[i], "-cornerx=", 9) == 0)
            {
                if (sscanf(&(argv[i][9]), "%d", &chessboardCornerNumX) != 1)
                    usage(argv[0]);

                if (chessboardCornerNumX <= 0)
                    usage(argv[0]);
            }
            else if (strncmp(argv[i], "-cornery=", 9) == 0)
            {
                if (sscanf(&(argv[i][9]), "%d", &chessboardCornerNumY) != 1)
                    usage(argv[0]);

                if (chessboardCornerNumY <= 0)
                    usage(argv[0]);
            }
            else if (strncmp(argv[i], "-imagenum=", 10) == 0)
            {
                if (sscanf(&(argv[i][10]), "%d", &calibImageNum) != 1)
                    usage(argv[0]);

                if (calibImageNum <= 0)
                    usage(argv[0]);
            }
            else if (strncmp(argv[i], "-pattwidth=", 11) == 0)
            {
                if (sscanf(&(argv[i][11]), "%f", &patternWidth) != 1)
                    usage(argv[0]);

                if (patternWidth <= 0)
                    usage(argv[0]);
            }
            else
            {
                ARLOGe("Error: invalid command line argument '%s'.\n", argv[i]);
                usage(argv[0]);
            }
        }

        i++;
    }

    if (chessboardCornerNumX == 0)
        chessboardCornerNumX = CHESSBOARD_CORNER_NUM_X;

    if (chessboardCornerNumY == 0)
        chessboardCornerNumY = CHESSBOARD_CORNER_NUM_Y;

    if (calibImageNum == 0)
        calibImageNum = CALIB_IMAGE_NUM;

    if (patternWidth == 0.0f)
        patternWidth = (float)CHESSBOARD_PATTERN_WIDTH;

    ARLOG("CHESSBOARD_CORNER_NUM_X = %d\n", chessboardCornerNumX);
    ARLOG("CHESSBOARD_CORNER_NUM_Y = %d\n", chessboardCornerNumY);
    ARLOG("CHESSBOARD_PATTERN_WIDTH = %f\n", patternWidth);
    ARLOG("CALIB_IMAGE_NUM = %d\n", calibImageNum);
    ARLOG("Video parameter: %s\n", vconf);

    if (arVideoOpen(vconf) < 0)
        exit(0);

    if (arVideoGetSize(&xsize, &ysize) < 0)
        exit(0);

    ARLOG("Image size (x,y) = (%d,%d)\n", xsize, ysize);
    if ((pixFormat = arVideoGetPixelFormat()) == AR_PIXEL_FORMAT_INVALID)
        exit(0);

    screenWidth  = glutGet(GLUT_SCREEN_WIDTH);
    screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
    if (screenWidth > 0 && screenHeight > 0)
    {
        screenMargin = (int)(MAX(screenWidth, screenHeight) * SCREEN_SIZE_MARGIN);
        if ((screenWidth - screenMargin) < xsize || (screenHeight - screenMargin) < ysize)
        {
            viewport.xsize = screenWidth - screenMargin;
            viewport.ysize = screenHeight - screenMargin;
            ARLOG("Scaling window to fit onto %dx%d screen (with %2.0f%% margin).\n", screenWidth, screenHeight, SCREEN_SIZE_MARGIN * 100.0);
        }
        else
        {
            viewport.xsize = xsize;
            viewport.ysize = ysize;
        }
    }
    else
    {
        viewport.xsize = xsize;
        viewport.ysize = ysize;
    }

    viewport.sx = 0;
    viewport.sy = 0;
    if ((vp = argCreateViewport(&viewport)) == NULL)
        exit(0);

    argViewportSetImageSize(vp, xsize, ysize);
    argViewportSetPixFormat(vp, pixFormat);
    argViewportSetDispMethod(vp, AR_GL_DISP_METHOD_TEXTURE_MAPPING_FRAME);
    argViewportSetDistortionMode(vp, AR_GL_DISTORTION_COMPENSATE_DISABLE);
    argViewportSetDispMode(vp, AR_GL_DISP_MODE_FIT_TO_VIEWPORT_KEEP_ASPECT_RATIO);

    // Set up the grayscale image.
    arIPI = arImageProcInit(xsize, ysize, pixFormat, 1); // 1 -> always copy, since we need OpenCV to be able to wrap the memory.
    if (!arIPI)
    {
        ARLOGe("Error initialising image processing.\n");
        exit(-1);
    }

    calibImage = cvCreateImageHeader(cvSize(xsize, ysize), IPL_DEPTH_8U, 1);
    cvSetData(calibImage, arIPI->image, xsize); // Last parameter is rowBytes.

    // Allocate space for results.
    arMalloc(corners, CvPoint2D32f, chessboardCornerNumX * chessboardCornerNumY);
    arMalloc(cornerSet, CvPoint2D32f, chessboardCornerNumX * chessboardCornerNumY * calibImageNum);
}