Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/multi/SRC/CalibradorAR.cpp
blob: ded03cd3d00ae18824f97d3951e14153288d325a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * CalibradorAR.cpp
 *
 *  Created on: 8/09/2010
 *      Author: smarichal
 */

#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>
#include <AR/arMulti.h>
#include "Utilidades.h"
#include "stdio.h"
#include "stdlib.h"

#include <math.h>


			/*************************************************************************************************************************
			 * 									CONFIGURACIONES GENERALES DE LA APLICACION
			 *************************************************************************************************************************/
int mostrarVideo = 0;
int mostrarRadar = 1;

			/********************************************************************************************************************************
			 * 								CONFIGURACION DE ARTOOLKIT Y MARCADORES
			 ********************************************************************************************************************************/

#define MAX_MARCADORES 30
/* set up the video format globals */

#ifdef _WIN32
char			*vconf = "Data\\WDM_camera_flipV.xml";
#else
char			*vconf = "";
#endif

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

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

char                *config_name = "Data/prototipo1/prototipo1.dat";
ARMultiMarkerInfoT  *config;



				/******************************************************************************************************************************************
				 * 												VARIABLES DEL CALIBRADOR
				 *****************************************************************************************************************************************/
int cantidad_iteraciones = 20;
				/***************************************************************************************************************************************
				 * 												DECALRACIONES DE FUNCIONES AUXILIARES Y VARIABLES AUXILIAREAS
				 ****************************************************************************************************************************************/
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();

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

    arVideoCapStart();
    argMainLoop( NULL, keyEvent, mainLoop );
	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);
    }


    if( key == 'c'){
    	ArtoolkitData* data = new ArtoolkitData();
		data->config = config;
		data->thresh = thresh;
		data->usarAlgoritmoLite = 1;
		printf("Antes de calibrar\n");
		ARMultiCalibracionData* resultCalibracion = arCalibrarThresholdMultiMarker(data,1000,cantidad_iteraciones,1);
		printf("Resultados de la calibracion, nuevo t = %d\n",resultCalibracion->threshCalculado);
    }

    if( key == 'x'){
        	ArtoolkitData* data = new ArtoolkitData();
    		data->config = config;
    		data->thresh = thresh;
    		data->usarAlgoritmoLite = 0;
    		printf("Antes de calibrar\n");
    		ARMultiCalibracionData* resultCalibracion = arCalibrarThresholdMultiMarker(data,1000,cantidad_iteraciones,1);
    		printf("Resultados de la calibracion, nuevo t = %d\n",resultCalibracion->threshCalculado);
	}


    if( key == 'v' ) {
		mostrarVideo = 1 - mostrarVideo;
		if(!mostrarVideo){
			argCleanWindow(0,0);
		}
    }

     if( key == 'r' ) {
    	printf("Mostrar Radar\n");
		mostrarRadar = 1 - mostrarRadar;
		if(!mostrarRadar){
			argCleanWindow(2,1);
		}
	}

    if( key == 't' ) {
        printf("count = %d\n",count);
        printf("timer = %f\n",arUtilTimer());
        printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
        printf("Enter new threshold value (current = %d): ", thresh);
        scanf("%d",&thresh); while( getchar()!='\n' );
        printf("\n");
        count = 0;
    }

    /* turn on and off the debug mode with right mouse */
    if( key == 'd' ) {
        printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
        arDebug = 1 - arDebug;
        if( arDebug == 0 ) {
            glClearColor( 0.0, 0.0, 0.0, 0.0 );
            glClear(GL_COLOR_BUFFER_BIT);
            argSwapBuffers();
            glClear(GL_COLOR_BUFFER_BIT);
            argSwapBuffers();
        }
        count = 0;
    }

}


/* main loop */
static void mainLoop(void)
{
	ARUint8         *dataPtr;
    ARMarkerInfo    *marker_info;
    int             marker_num;
    double          err;
    int             i;

    /* grab a video frame */
    arUtilSleep(90000);
    if( (dataPtr = (ARUint8 *)arVideoGetImage()) == NULL ) {
	printf("sleep\n");
	arUtilSleep(2);
        return;
    }
    if( count == 0 ) arUtilTimerReset();
    count++;


		/* detect the markers in the video frame */
		if( arDetectMarkerLite(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
			cleanup();
			exit(0);
		}



		//argSwapBuffers();
		if(mostrarVideo){
			argDrawMode2D();
			if( !arDebug ) {
				argDispImage( dataPtr, 0,0 );
			}
			else {
				argDispImage( dataPtr, 1, 1 );
				if( arImageProcMode == AR_IMAGE_PROC_IN_HALF )
					argDispHalfImage( arImage, 0, 0 );
				else
					argDispImage( arImage, 0, 0);

				glColor3f( 1.0, 0.0, 0.0 );
				glLineWidth(6.0);
				for( i = 0; i < marker_num; i++ ) {
					argDrawSquare( marker_info[i].vertex, 0, 0 );
				}
				glLineWidth( 1.0 );
			}
		}

		if(mostrarRadar){
			argDrawMode2D();
			argCleanWindow(2,1);
			dibujarLimitesPantalla(xsize, ysize);
			for( i = 0; i < marker_num; i++ ) {
				dibujarMiniMarcador(0,0,marker_info[i].vertex,1);
			}
		}
		arVideoCapNext();

		if( (err=arMultiGetTransMat(marker_info, marker_num, config)) < 0 ) {
			argSwapBuffers();
			return;
		}

		if(err > 100.0 ) {
		  printf("saliendo 2\n");
		  argSwapBuffers();
		  return;
		}

		if(mostrarVideo){
			argDrawMode3D();
			argDraw3dCamera( 0, 0 );
			glClearDepth( 1.0 );
			glClear(GL_DEPTH_BUFFER_BIT);
			argSwapBuffers();
		}

	//	printf("Marcadores detectados = %d\n",marker_num);



}


static void init( void )
{
    ARParam  wparam;

    /* open the video path */
    if( arVideoOpen( vconf ) < 0 ) exit(0);
    /* find the size of the window */
    if( arVideoInqSize(&xsize, &ysize) < 0 ) exit(0);
    printf("Image size (x,y) = (%d,%d)\n", xsize, ysize);

    /* set the initial camera parameters */
    if( arParamLoad(cparam_name, 1, &wparam) < 0 ) {
        printf("Camera parameter load error !!\n");
        exit(0);
    }
    arParamChangeSize( &wparam, xsize, ysize, &cparam );
    arInitCparam( &cparam );
    printf("*** Camera Parameter ***\n");
    arParamDisp( &cparam );

    if( (config = arMultiReadConfigFile(config_name)) == NULL ) {
        printf("config data load error !!\n");
        exit(0);
    }

    /* open the graphics window */
    argInit( &cparam, 1.0, 0, 2, 1, 0 );
    arFittingMode   = AR_FITTING_TO_IDEAL;
    arImageProcMode = AR_IMAGE_PROC_IN_HALF;
    argDrawMode     = AR_DRAW_BY_TEXTURE_MAPPING;
    argTexmapMode   = AR_DRAW_TEXTURE_HALF_IMAGE;


}

/* cleanup function called when program exits */
static void cleanup(void)
{
    arVideoCapStop();
    arVideoClose();
    argCleanup();
}