Camera tracking integration
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 7 Sep 2011 08:09:56 +0000 (08:09 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 7 Sep 2011 08:09:56 +0000 (08:09 +0000)
===========================

- Fix for pixel aspect: solver supposed claibration was made against
  aspected image, manual calibration worked in non-aspected image.
- Fixed incorrect usage of sensor size when creating projection matrix
  for tracking structure.

source/blender/blenkernel/intern/tracking.c
source/blender/editors/space_clip/clip_draw.c

index 87077ee993ad6aee33b436848e4205ff02b08473..96a11303e651e576d92852ad7bc329e65f7898b6 100644 (file)
@@ -1403,24 +1403,25 @@ void BKE_get_tracking_mat(Scene *scene, float mat[4][4])
 void BKE_tracking_projection_matrix(MovieTracking *tracking, int framenr, int winx, int winy, float mat[4][4])
 {
        MovieReconstructedCamera *camera;
-       float lens= tracking->camera.focal*32.0f/(float)winx;
+       float lens= tracking->camera.focal*tracking->camera.sensor_width/(float)winx;
        float viewfac, pixsize, left, right, bottom, top, clipsta, clipend;
        float winmat[4][4];
+       float ycor= 1.f/tracking->camera.pixel_aspect;
 
        clipsta= 0.1f;
        clipend= 1000.0f;
 
        if(winx >= winy)
-               viewfac= (lens*winx)/32.0f;
+               viewfac= (lens*winx)/tracking->camera.sensor_width;
        else
-               viewfac= (lens*winy)/32.0f;
+               viewfac= (ycor*lens*winy)/tracking->camera.sensor_width;
 
        pixsize= clipsta/viewfac;
 
        left= -0.5f*(float)winx*pixsize;
-       bottom= -0.5f*(float)winy*pixsize;
+       bottom= -0.5f*ycor*(float)winy*pixsize;
        right=  0.5f*(float)winx*pixsize;
-       top=  0.5f*(float)winy*pixsize;
+       top=  0.5f*ycor*(float)winy*pixsize;
 
        perspective_m4(winmat, left, right, bottom, top, clipsta, clipend);
 
@@ -1439,12 +1440,13 @@ void BKE_tracking_apply_intrinsics(MovieTracking *tracking, float co[2], float n
 
 #ifdef WITH_LIBMV
        double x, y;
+       float aspy= 1.f/tracking->camera.pixel_aspect;
 
        /* normalize coords */
        x= (co[0]-camera->principal[0]) / camera->focal;
-       y= (co[1]-camera->principal[1]) / camera->focal;
+       y= (co[1]-camera->principal[1] * aspy) / camera->focal;
 
-       libmv_applyCameraIntrinsics(camera->focal, camera->principal[0], camera->principal[1],
+       libmv_applyCameraIntrinsics(camera->focal, camera->principal[0], camera->principal[1] * aspy,
                                camera->k1, camera->k2, camera->k3, x, y, &x, &y);
 
        /* result is in image coords already */
@@ -1459,12 +1461,13 @@ void BKE_tracking_invert_intrinsics(MovieTracking *tracking, float co[2], float
 
 #ifdef WITH_LIBMV
        double x= co[0], y= co[1];
+       float aspy= 1.f/tracking->camera.pixel_aspect;
 
-       libmv_InvertIntrinsics(camera->focal, camera->principal[0], camera->principal[1],
+       libmv_InvertIntrinsics(camera->focal, camera->principal[0], camera->principal[1] * aspy,
                                camera->k1, camera->k2, camera->k3, x, y, &x, &y);
 
        nco[0]= x * camera->focal + camera->principal[0];
-       nco[1]= y * camera->focal + camera->principal[1];
+       nco[1]= y * camera->focal + camera->principal[1] * aspy;
 #endif
 }
 
index b9e5ac79f6f6a1a90c28f52e9f7f09c44d2c4ad3..aebb3ebbe1d4fc4893031efd6e92df74f13bcdab 100644 (file)
@@ -872,7 +872,7 @@ static void draw_tracking_tracks(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                glPointSize(3.0f);
 
                aspy= 1.f/clip->tracking.camera.pixel_aspect;
-               BKE_tracking_projection_matrix(tracking, framenr, width, height*aspy, mat);
+               BKE_tracking_projection_matrix(tracking, framenr, width, height, mat);
 
                track= tracking->tracks.first;
                while(track) {
@@ -886,20 +886,23 @@ static void draw_tracking_tracks(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                                        mul_v4_m4v4(pos, mat, vec);
 
                                        pos[0]= (pos[0]/(pos[3]*2.0f)+0.5f)*width;
-                                       pos[1]= (pos[1]/(pos[3]*2.0f)+0.5f)*height;
+                                       pos[1]= (pos[1]/(pos[3]*2.0f)+0.5f)*height*aspy;
 
-                                       BKE_tracking_apply_intrinsics(tracking, pos, pos);
+                                       if(pos[0]>=0.f && pos[1]>=0.f && pos[0]<=width && pos[1]<=height*aspy) {
+                                               BKE_tracking_apply_intrinsics(tracking, pos, pos);
 
-                                       vec[0]= (marker->pos[0]+track->offset[0])*width;
-                                       vec[1]= (marker->pos[1]+track->offset[1])*height;
-                                       sub_v2_v2(vec, pos);
+                                               vec[0]= (marker->pos[0]+track->offset[0])*width;
+                                               vec[1]= (marker->pos[1]+track->offset[1])*height*aspy;
 
-                                       if(len_v2(vec)<3) glColor3f(0.0f, 1.0f, 0.0f);
-                                       else glColor3f(1.0f, 0.0f, 0.0f);
+                                               sub_v2_v2(vec, pos);
 
-                                       glBegin(GL_POINTS);
-                                               glVertex3f(pos[0]/width, pos[1]/height, 0);
-                                       glEnd();
+                                               if(len_v2(vec)<3) glColor3f(0.0f, 1.0f, 0.0f);
+                                               else glColor3f(1.0f, 0.0f, 0.0f);
+
+                                               glBegin(GL_POINTS);
+                                                       glVertex3f(pos[0]/width, pos[1]/(height*aspy), 0);
+                                               glEnd();
+                                       }
                                }
                        }
 
@@ -939,8 +942,9 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip, int wid
        const int n= 10;
        int i, j, a;
        float pos[2], tpos[2], grid[11][11][2];
-       float dx= (float)width/n, dy= (float)height/n;
        MovieTracking *tracking= &clip->tracking;
+       float aspy= 1.f/tracking->camera.pixel_aspect;
+       float dx= (float)width/n, dy= (float)height/n*aspy;
 
        if(sc->mode!=SC_MODE_DISTORTION)
                return;
@@ -1016,7 +1020,7 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip, int wid
                                BKE_tracking_apply_intrinsics(tracking, pos, grid[i][j]);
 
                                grid[i][j][0]/= width;
-                               grid[i][j][1]/= height;
+                               grid[i][j][1]/= height*aspy;
 
                                pos[0]+= dx;
                        }
@@ -1066,10 +1070,10 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip, int wid
                                                                        int steps;
 
                                                                        pos[0]= stroke->points[i].x*width;
-                                                                       pos[1]= stroke->points[i].y*height;
+                                                                       pos[1]= stroke->points[i].y*height*aspy;
 
                                                                        npos[0]= stroke->points[i+1].x*width;
-                                                                       npos[1]= stroke->points[i+1].y*height;
+                                                                       npos[1]= stroke->points[i+1].y*height*aspy;
 
                                                                        len= len_v2v2(pos, npos);
                                                                        steps= ceil(len/5.f);
@@ -1085,7 +1089,7 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip, int wid
 
                                                                        for(j= 0; j<steps; j++) {
                                                                                BKE_tracking_apply_intrinsics(tracking, pos, tpos);
-                                                                               glVertex2f(tpos[0]/width, tpos[1]/height);
+                                                                               glVertex2f(tpos[0]/width, tpos[1]/(height*aspy));
 
                                                                                add_v2_v2(pos, dpos);
                                                                        }