Download document (10.35 MB) - Escuela Superior de Informática ...

Download document (10.35 MB) - Escuela Superior de Informática ... Download document (10.35 MB) - Escuela Superior de Informática ...

11.07.2015 Views

164 Capítulo D. Código fuente43 /* Hacemos tantas copias de la imagen como44 procesamientos diferentes queramos hacer. */45 if( !frame_copy) {46 frame_copy =47 cvCreateImage( cvSize(frame->width,frame->height),48 IPL_DEPTH_8U, frame->nChannels );49 }5051 if( !imageAdquisition) {52 imageAdquisition =53 cvCreateImage( cvSize(frame->width,frame->height),54 IPL_DEPTH_8U, frame->nChannels );55 }5657 if( frame->origin == IPL_ORIGIN_TL ) {58 cvFlip( frame, frame_copy, 0 );59 cvFlip( frame, imageAdquisition, 0 );60 }61 else {62 cvFlip( frame, frame_copy, 0 );63 }6465 //Se aplica el algoritmo de L-K mejorado.66 calcularOpticalFlow(frame);67 c = cvWaitKey(25);6869 }//fin else frame70 }//fin for7172 cvReleaseCapture(&capture);73 cvReleaseImage(&frame_copy);74 cvReleaseMemStorage(&storage);75 caraInicialized = 0;76 free_memory();7778 }79 else {80 free_memory();81 }8283 if(ficheroMARCAS != NULL)84 fclose(ficheroMARCAS);85 if(ficheroGFACIALES != NULL)86 fclose(ficheroGFACIALES);8788 return 0;89 }9091 /**********************************************************************92 * Funcion: calcularOpticalFlow93 * Descripcion: Implementación del algoritmo de Lucas-Kanade.94 * Entradas: frame_optical_flow --> Puntero a Imagen a analizar.95 * Salidas: nada.96 *********************************************************************/97 void calcularOpticalFlow(IplImage *frame_optical_flow){9899 int i, k;// c;100101 if( !imageOF )102 {

D.2. Código fuente de los módulos más relevantes. 165103 cvReleaseImage( &grey );104 cvReleaseImage( &prev_grey );105 cvReleaseImage( &pyramid );106 cvReleaseImage( &prev_pyramid );107108 imageOF = cvCreateImage( cvSize( frame_optical_flow->width,109 frame_optical_flow->height), 8, 3);110111 grey = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );112 prev_grey = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );113 pyramid = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );114 prev_pyramid = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );115 points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));116 points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));117118 points3d [0]= (CvPoint3D32f*)cvAlloc(MAX_COUNT*sizeof(points3d[0][0]));119 points3d [1]= (CvPoint3D32f*)cvAlloc(MAX_COUNT*sizeof(points3d[0][0]));120121 status = (char*)cvAlloc(MAX_COUNT);122 flags = 0;123 }124125126 cvCopy(frame_optical_flow, imageOF, NULL);127128 cvCvtColor(imageOF, grey, CV_BGR2GRAY);129130131 if(need_to_init)132 {133 /* automatic initialization */134 IplImage* eig = cvCreateImage( cvGetSize(grey), 32, 1 );135 IplImage* temp = cvCreateImage( cvGetSize(grey), 32, 1 );136137 /* Se inicializan los puntos de seguimiento para los huesos */138 initializeCoordinates(1);139140141 cvFindCornerSubPix( grey, points[1], count,142 cvSize(win_size,win_size), cvSize(-1,-1),143 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));144 cvReleaseImage( &eig );145 cvReleaseImage( &temp );146147 add_remove_pt = 0;148 }149 else if( count > 0 )150 {151 cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid,152 points[0], points[1], count,153 cvSize(win_size,win_size), 3, status, 0,154 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03),155 flags );156 flags |= CV_LKFLOW_PYR_A_READY;157158 for( i = k = 0; i < count; i++ )159 {160 if( add_remove_pt )161 {162 double dx = pt.x - points[1][i].x;

D.2. Código fuente <strong>de</strong> los módulos más relevantes. 165103 cvReleaseImage( &grey );104 cvReleaseImage( &prev_grey );105 cvReleaseImage( &pyramid );106 cvReleaseImage( &prev_pyramid );107108 imageOF = cvCreateImage( cvSize( frame_optical_flow->width,109 frame_optical_flow->height), 8, 3);110111 grey = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );112 prev_grey = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );113 pyramid = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );114 prev_pyramid = cvCreateImage( cvGetSize(frame_optical_flow), 8, 1 );115 points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));116 points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));117118 points3d [0]= (CvPoint3D32f*)cvAlloc(MAX_COUNT*sizeof(points3d[0][0]));119 points3d [1]= (CvPoint3D32f*)cvAlloc(MAX_COUNT*sizeof(points3d[0][0]));120121 status = (char*)cvAlloc(MAX_COUNT);122 flags = 0;123 }124125126 cvCopy(frame_optical_flow, imageOF, NULL);127128 cvCvtColor(imageOF, grey, CV_BGR2GRAY);129130131 if(need_to_init)132 {133 /* automatic initialization */134 IplImage* eig = cvCreateImage( cvGetSize(grey), 32, 1 );135 IplImage* temp = cvCreateImage( cvGetSize(grey), 32, 1 );136137 /* Se inicializan los puntos <strong>de</strong> seguimiento para los huesos */138 initializeCoordinates(1);139140141 cvFindCornerSubPix( grey, points[1], count,142 cvSize(win_size,win_size), cvSize(-1,-1),143 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));144 cvReleaseImage( &eig );145 cvReleaseImage( &temp );146147 add_remove_pt = 0;148 }149 else if( count > 0 )150 {151 cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid,152 points[0], points[1], count,153 cvSize(win_size,win_size), 3, status, 0,154 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03),155 flags );156 flags |= CV_LKFLOW_PYR_A_READY;157158 for( i = k = 0; i < count; i++ )159 {160 if( add_remove_pt )161 {162 double dx = pt.x - points[1][i].x;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!