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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
//gcc -L/usr/lib -W -Wall -L/usr/X11R6/lib -I/usr/X11R6/include -I/usr/include/opencv -lX11 -lm -lcv -lglut -lGLU -lGL -lhighgui -o calibration_and_3DTracking calibration_and_3DTracking.cpp `pkg-config --cflags --libs opencv`
//./calibration_and_3DTracking number_of_interior_corners_widthwise number_of_interior_corners_upright number_of_images_for_calibration
//./calibration_and_3DTracking 6 7 7
#include<cv.h>
#include<cvaux.h>
#include<highgui.h>
#include <GL/glut.h>
using namespace std;
#define IsRGB(s) ((s[0] == 'R') && (s[1] == 'G') && (s[2] == 'B'))
#define IsBGR(s) ((s[0] == 'B') && (s[1] == 'G') && (s[2] == 'R'))
bool initCalib=true;
bool initCoords=true;
bool boolInit;
bool initPos=false;
char *status = 0;
CvCapture *capture = 0;
CvMat* distortion_coeffs;
CvMat* image_points;
CvMat* image_points2;
CvMat* intrinsic_matrix;
CvMat* object_points;
CvMat* object_points2;
CvMat* point_counts;
CvMat* point_counts2;
CvMat* rotation_vector;
CvMat* rotMat; // rotation matrix
CvMat* rv; // rotation vector
CvMat* translation_vector;// translation vector
CvPoint2D32f* corners;
CvPoint2D32f *points[2] = {0,0}, *swap_points;
CvSize board_sz;
double axisTranslatX0;
double axisTranslatY0;
double axisTranslatZ0;
GLfloat light_position0[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_shininess[] = { 100.0 };
GLfloat mat_specular[] = { 3000.0, 3000.0, 3000.0, 3000.0 };
GLfloat mat_surface1[] = { 0.0, 1.0, 0.0, 0.0 };
GLfloat mat_surface2[] = { 1.0, 0.0, 0.0, 0.0 };
GLfloat mat_surface3[] = { 0.0, 0.0, 1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLuint imageID;
GLuint imageID2;
float cx,cy; //camera center
int board_w;
int board_h;
int board_n;
int flags = 0;
int found;
int win_size = 10;
int n_boards = 0;
int frameWidth, frameHeight;
int corner_count;
int successes = 0;
int step, nframe = 0;
const int board_dt = 20; //Wait 20 frames per chessboard view
int counts, init;
int countLK = 0;
int window1 = 0;
int window2 = 0;
IplImage *grey =0;
IplImage *prev_grey =0;
IplImage *pyramid = 0;
IplImage *prev_pyramid =0;
IplImage* swap_temp;
IplImage *image=0;
IplImage *gray_image=0;
/***************************************************************/
void window2_Reshape(int w, int h);
void window2_Display();
/***************************************************************/
//opengl function to draw a teapot
void gltDrawUnitAxes2(void)
{
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glColor3f(1.0f, 1.0f, 1.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_surface1);
glutSolidTeapot(0.6) ;
glTranslatef(2.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_surface2);
glutSolidTeapot(0.6) ;
glTranslatef(-4.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_surface3);
glutSolidTeapot(0.6) ;
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);
}
/***************************************************************/
//function to track the chessboard and compute the extrinsic parameters
void opticalFlow(IplImage* frame){
cvCopy( frame, image, 0);
cvCvtColor( frame, grey, CV_BGR2GRAY );
cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid, points[0], points[1], corner_count, cvSize(win_size,win_size), 5, status, 0, cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20,0.3), flags);
cvFindCornerSubPix(grey,points[1], corner_count,cvSize(11,11),cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
cvDrawChessboardCorners(frame, board_sz, points[1],corner_count, found);
cvShowImage( "Calibration", frame );
cvWaitKey(10);
if( corner_count == board_n ) {
for( int j=0; j<board_n; ++j ) {
//point coordinates in world space
CV_MAT_ELEM(*image_points, float,j,0) = points[1][j].x;
CV_MAT_ELEM(*image_points, float,j,1) = points[1][j].y;
//point coordinates in chessboard coordinate system (one unit per square)
CV_MAT_ELEM(*object_points,float,j,0) = j/board_w;
CV_MAT_ELEM(*object_points,float,j,1) = j%board_w;
CV_MAT_ELEM(*object_points,float,j,2) = 0.0f;
successes++;
}
//extrinsic parameters
cvFindExtrinsicCameraParams2(object_points, image_points,intrinsic_matrix,distortion_coeffs,rotation_vector, translation_vector);
}
//transform the rotation vector in a rotation matrix
cvRodrigues2(rotation_vector,rotMat,NULL);
if(initCoords==true){
//initial position of the chessboard
axisTranslatX0=CV_MAT_ELEM( *translation_vector, float, 0,0);
axisTranslatY0=CV_MAT_ELEM( *translation_vector, float, 1,0);
axisTranslatZ0=CV_MAT_ELEM( *translation_vector, float, 2,0);
initCoords=false;
}
glutPostRedisplay();
CV_SWAP( prev_grey, grey, swap_temp );
CV_SWAP( prev_pyramid, pyramid, swap_temp );
CV_SWAP( points[0], points[1], swap_points );
}
/***************************************************************/
//calibration function to get the intrinsic parameters of the camera
void initCalibration(IplImage* frame)
{
image = cvCreateImage( cvGetSize(frame), 8, 3 );
image->origin = frame->origin;
cvNamedWindow( "Calibration" );
gray_image = cvCreateImage(cvGetSize(frame),8,1);
//frame capture for calibration
while(successes < n_boards) {
if((nframe++ % board_dt) == 0) {
int found = cvFindChessboardCorners(frame, board_sz, corners, &corner_count,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
cvCvtColor(frame, gray_image, CV_BGR2GRAY);
cvFindCornerSubPix(gray_image, corners, corner_count,cvSize(11,11),cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
cvDrawChessboardCorners(frame, board_sz, corners,corner_count, found);
cvShowImage( "Calibration", frame );
// add the detected chessboard to data
if( corner_count == board_n && found) {
step = successes*board_n;
for( int i=step, j=0; j<board_n; ++i,++j ) {
CV_MAT_ELEM(*image_points, float,i,0) = corners[j].x;
CV_MAT_ELEM(*image_points, float,i,1) = corners[j].y;
CV_MAT_ELEM(*object_points,float,i,0) = (j/board_w);
CV_MAT_ELEM(*object_points,float,i,1) = (j%board_w);
CV_MAT_ELEM(*object_points,float,i,2) = 0.0f;
}
CV_MAT_ELEM(*point_counts, int,successes,0) = board_n;
successes++;
printf("Collected our %d of %d needed chessboard images\n",successes,n_boards);
}
}
cvWaitKey(10);
frame = cvQueryFrame( capture );
cvCopy( frame, image, 0 );
}
//ALLOCATE MATRICES ACCORDING TO HOW MANY CHESSBOARDS FOUND
object_points2 = cvCreateMat(successes*board_n,3,CV_32FC1);
image_points2 = cvCreateMat(successes*board_n,2,CV_32FC1);
point_counts2 = cvCreateMat(successes,1,CV_32SC1);
//TRANSFER THE POINTS INTO THE CORRECT SIZE MATRICES
for(int i = 0; i<successes*board_n; ++i){
CV_MAT_ELEM( *image_points2, float, i, 0) =
CV_MAT_ELEM( *image_points, float, i, 0);
CV_MAT_ELEM( *image_points2, float,i,1) =
CV_MAT_ELEM( *image_points, float, i, 1);
CV_MAT_ELEM(*object_points2, float, i, 0) =
CV_MAT_ELEM( *object_points, float, i, 0) ;
CV_MAT_ELEM( *object_points2, float, i, 1) =
CV_MAT_ELEM( *object_points, float, i, 1) ;
CV_MAT_ELEM( *object_points2, float, i, 2) =
CV_MAT_ELEM( *object_points, float, i, 2) ;
}
for(int i=0; i<successes; ++i){ //These are all the same number
CV_MAT_ELEM( *point_counts2, int, i, 0) =
CV_MAT_ELEM( *point_counts, int, i, 0);
}
// At this point we have all of the chessboard corners we need.
// Initialize the intrinsic matrix such that the two focal
// lengths have a ratio of 1.0
//
CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0 ) = 1.0f;
CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1 ) = 1.0f;
//CALIBRATE THE CAMERA!
cvCalibrateCamera2(object_points2, image_points2, point_counts2, cvGetSize( image ), intrinsic_matrix, distortion_coeffs, NULL, NULL, 0 );
cx=CV_MAT_ELEM( *intrinsic_matrix, float, 0, 2);
cy=CV_MAT_ELEM( *intrinsic_matrix, float, 1, 2);
/**********************************************/
//end of calibration
rotation_vector = cvCreateMat(3,1,CV_32FC1);
translation_vector = cvCreateMat(3,1,CV_32FC1);
rotMat= cvCreateMat(3,3,CV_32FC1);
grey = cvCreateImage( cvGetSize(image), 8, 1 ); //image courante en niveau de gris
prev_grey = cvCreateImage( cvGetSize(image), 8, 1 ); //image précédente en niveau de gris
pyramid = cvCreateImage( cvGetSize(image), 8, 1 );
prev_pyramid = cvCreateImage( cvGetSize(image), 8, 1 );
points[0] = (CvPoint2D32f*)cvAlloc(board_n*sizeof(points[0][0])); //points précédents du damier
points[1] = (CvPoint2D32f*)cvAlloc(board_n*sizeof(points[0][0])); //points courants du damier
status=(char*)cvAlloc(board_n);
image = cvQueryFrame( capture );
cvCvtColor( image, prev_grey, CV_BGR2GRAY );
//recherche du damier
found = cvFindChessboardCorners(image, board_sz, corners, &corner_count,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
cvCvtColor(image, gray_image, CV_BGR2GRAY);
//precision subpixelique
cvFindCornerSubPix(gray_image, corners, corner_count,cvSize(11,11),cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
cvDrawChessboardCorners(image, board_sz, corners,corner_count, found);
cvShowImage( "Calibration", image );
CV_SWAP( corners, points[0], swap_points );
}
/***************************************************************/
//main loop function
void window1_Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
IplImage *frame = 0;
frame = cvQueryFrame( capture );
if(initCalib==true){ //execute camera calibration one time
initCalibration(frame);
initCalib=false;
}
successes=0;
opticalFlow(frame); //then trakcing of the chessboard
glLoadIdentity();
glTranslatef(0.0f, 0.0f,-1.0f);
// must be done else wrong upside down
glRotatef(180.0f, 1, 0, 0); // rotate the matrix to rotate the video texture.
GLenum format = IsBGR(image->channelSeq) ? GL_BGR_EXT : GL_RGBA;
// upload to openGL texture
if(!init && !boolInit)
{
glGenTextures(1, &imageID);
glBindTexture(GL_TEXTURE_2D, imageID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->width, image->height, 0, format, GL_UNSIGNED_BYTE, image->imageData);
init= 1; // init lucas-kanade is over
boolInit = true; // init opticalFlow is over
}
else
{
glBindTexture(GL_TEXTURE_2D, imageID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image->width, image->height, format, GL_UNSIGNED_BYTE, image->imageData);
}
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-1, -1, 0);
glTexCoord2f(1, 0); glVertex3f(1, -1, 0);
glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
glTexCoord2f(0, 1); glVertex3f(-1, 1, 0);
glEnd();
glLoadIdentity();
glutSwapBuffers();
glFlush();
glDisable(GL_TEXTURE_2D);
}
/***************************************************************/
void window1_glutReshape(int width,int height){
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(0.0f,(GLfloat)width/(GLfloat)height,0.1f,200.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/***************************************************************/
void window2_Reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80.0,(float)w/h,0.1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/***************************************************************/
void dessinerRepere(unsigned int echelle){
glPushMatrix();
glScalef(echelle,echelle,echelle);
glBegin(GL_LINES);
glColor3ub(0,0,255); //axe X en bleu
glVertex3d(0,0,0);
glVertex3d(1,0,0);
glColor3ub(0,255,0); //axe Y en vert
glVertex3d(0,0,0);
glVertex3d(0,1,0);
glColor3ub(255,0,0); //axe Z en rouge
glVertex3d(0,0,0);
glVertex3d(0,0,1);
glEnd();
glPopMatrix();
}
/***************************************************************/
//openGL window where I apply the chessboard movements to the teapot
void window2_Display()
{
window2_Reshape(640,480);
//create opengl matrix (rotation and translation) from the matrix computed with cvRodrigues and the translation vector from cvFindExtrinsicCameraParams2
GLdouble RTMat[16];
RTMat[0] = CV_MAT_ELEM( *rotMat, float, 0,0);
RTMat[1] = CV_MAT_ELEM( *rotMat, float, 1,0);
RTMat[2] = CV_MAT_ELEM( *rotMat, float, 2,0);
RTMat[3] = 0.0f;
RTMat[4] = CV_MAT_ELEM( *rotMat, float, 0,1);
RTMat[5] = CV_MAT_ELEM( *rotMat, float, 1,1);
RTMat[6] = CV_MAT_ELEM( *rotMat, float, 2,1);
RTMat[7] = 0.0f;
RTMat[8] = CV_MAT_ELEM( *rotMat, float, 0,2);
RTMat[9] = CV_MAT_ELEM( *rotMat, float, 1,2);
RTMat[10] = CV_MAT_ELEM( *rotMat, float,2,2);
RTMat[11] = 0.0f;
//I substract the initial position in the translation to start from the origin in openGL
RTMat[12] = CV_MAT_ELEM( *translation_vector, float, 0,0)-axisTranslatX0;//0.0f;
RTMat[13] = CV_MAT_ELEM( *translation_vector, float, 1,0)-axisTranslatY0;//0.0f;
RTMat[14] = CV_MAT_ELEM( *translation_vector, float, 2,0)-axisTranslatZ0;//0.0f;
RTMat[15] = 1.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,2,4, 0,0.5f,0, 0,1,0);
dessinerRepere(50);
glPushMatrix();
if(initPos==true){
glTranslatef(0.0f,0.0f,0.0f);
glTranslatef(0.0f,0.0f, 0.0f);
glTranslatef(0.0f, 0.0f,0.0f);
initPos=false;
}
//apply the movement
glMultMatrixd(RTMat);
gltDrawUnitAxes2();
glPopMatrix();
glutSwapBuffers();
glFlush();
glutPostRedisplay();
}
/***************************************************************/
int main(int argc, char *argv[])
{
board_w = atoi(argv[1]); //number of chessboard interior corners widthwise
board_h = atoi(argv[2]); //number of chessboard interior corners upright
n_boards = atoi(argv[3]); //number of images used for the calibration
board_n = board_w * board_h; //total number of corners
board_sz = cvSize( board_w, board_h );
//ALLOCATE STORAGE
image_points = cvCreateMat(n_boards*board_n,2,CV_32FC1);
object_points = cvCreateMat(n_boards*board_n,3,CV_32FC1);
point_counts = cvCreateMat(n_boards,1,CV_32SC1);
intrinsic_matrix = cvCreateMat(3,3,CV_32FC1);
distortion_coeffs = cvCreateMat(4,1,CV_32FC1);
rv = cvCreateMat(3,1,CV_32FC1);
corners = new CvPoint2D32f[ board_n ];
frameWidth= 640;
frameHeight=480;
boolInit = false;
capture = cvCaptureFromCAM( 0 );
if( !capture )
{
printf("LK_Error: Can't open Webcam\n");
return -1;
}
cvQueryFrame( capture );
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640,480);
//window 1
window1 = glutCreateWindow("Tracking");
glutPositionWindow(0,0);
glutReshapeFunc(window1_glutReshape);
glutDisplayFunc(window1_Draw);
glutIdleFunc(window1_Draw);
// window 2
window2 = glutCreateWindow("3D-glWindow 2");
glutPositionWindow(0,480);
glutReshapeFunc(window2_Reshape);
glutDisplayFunc(window2_Display);
glutIdleFunc(glutPostRedisplay);
glutMainLoop();
return 0;
} |
Partager