Tracking: Specify image image for (un)distortion model
authorSergey Sharybin <sergey.vfx@gmail.com>
Fri, 17 Apr 2020 14:51:00 +0000 (16:51 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 20 Apr 2020 14:26:31 +0000 (16:26 +0200)
Allows to support distortion models which needs to know actual
image dimensions to apply or inverse camera intrinsics.

source/blender/blenkernel/BKE_tracking.h
source/blender/blenkernel/intern/constraint.c
source/blender/blenkernel/intern/movieclip.c
source/blender/blenkernel/intern/tracking.c
source/blender/compositor/operations/COM_MovieDistortionOperation.cpp
source/blender/editors/space_clip/clip_draw.c
source/blender/editors/space_clip/clip_editor.c
source/blender/editors/space_clip/clip_utils.c

index 2023674543895aebc9cae1735bd2d51872b0753d..bb88fbf863bd93677e130a2d4936baf7bc644e1d 100644 (file)
@@ -261,8 +261,16 @@ void BKE_tracking_distortion_undistort_v2(struct MovieDistortion *distortion,
                                           float r_co[2]);
 void BKE_tracking_distortion_free(struct MovieDistortion *distortion);
 
-void BKE_tracking_distort_v2(struct MovieTracking *tracking, const float co[2], float r_co[2]);
-void BKE_tracking_undistort_v2(struct MovieTracking *tracking, const float co[2], float r_co[2]);
+void BKE_tracking_distort_v2(struct MovieTracking *tracking,
+                             int image_width,
+                             int image_height,
+                             const float co[2],
+                             float r_co[2]);
+void BKE_tracking_undistort_v2(struct MovieTracking *tracking,
+                               int image_width,
+                               int image_height,
+                               const float co[2],
+                               float r_co[2]);
 
 struct ImBuf *BKE_tracking_undistort_frame(struct MovieTracking *tracking,
                                            struct ImBuf *ibuf,
@@ -276,6 +284,8 @@ struct ImBuf *BKE_tracking_distort_frame(struct MovieTracking *tracking,
                                          float overscan);
 
 void BKE_tracking_max_distortion_delta_across_bound(struct MovieTracking *tracking,
+                                                    int image_width,
+                                                    int image_height,
                                                     struct rcti *rect,
                                                     bool undistort,
                                                     float delta[2]);
index 099fdacf401240fd30895b459eca69c8c186f5df..bf100b2e9496fb0950f46c55d11943b3b7bb950a 100644 (file)
@@ -4694,7 +4694,7 @@ static void followtrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase
         pos[0] *= width;
         pos[1] *= height;
 
-        BKE_tracking_undistort_v2(tracking, pos, pos);
+        BKE_tracking_undistort_v2(tracking, width, height, pos, pos);
 
         /* Normalize pixel coordinates back. */
         pos[0] /= width;
index 193fe859def15c6666d9a4b64341835e21d6db45..7991559d1ce28f1224b8feaa0a2a9a94e85009fd 100644 (file)
@@ -1512,7 +1512,8 @@ void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClip
             undist_marker.pos[0] *= width;
             undist_marker.pos[1] *= height * aspy;
 
-            BKE_tracking_undistort_v2(&clip->tracking, undist_marker.pos, undist_marker.pos);
+            BKE_tracking_undistort_v2(
+                &clip->tracking, width, height, undist_marker.pos, undist_marker.pos);
 
             undist_marker.pos[0] /= width;
             undist_marker.pos[1] /= height * aspy;
index 0204667b3bd1427134c0bb56b6f364174982fb09..97d95cb7e46cb2bb5fb0c6a3848efe50b7f5a7f3 100644 (file)
@@ -2288,13 +2288,15 @@ void BKE_tracking_distortion_free(MovieDistortion *distortion)
   MEM_freeN(distortion);
 }
 
-void BKE_tracking_distort_v2(MovieTracking *tracking, const float co[2], float r_co[2])
+void BKE_tracking_distort_v2(
+    MovieTracking *tracking, int image_width, int image_height, const float co[2], float r_co[2])
 {
   const MovieTrackingCamera *camera = &tracking->camera;
   const float aspy = 1.0f / tracking->camera.pixel_aspect;
 
   libmv_CameraIntrinsicsOptions camera_intrinsics_options;
-  tracking_cameraIntrinscisOptionsFromTracking(tracking, 0, 0, &camera_intrinsics_options);
+  tracking_cameraIntrinscisOptionsFromTracking(
+      tracking, image_width, image_height, &camera_intrinsics_options);
   libmv_CameraIntrinsics *intrinsics = libmv_cameraIntrinsicsNew(&camera_intrinsics_options);
 
   /* Normalize coordinates. */
@@ -2309,13 +2311,15 @@ void BKE_tracking_distort_v2(MovieTracking *tracking, const float co[2], float r
   r_co[1] = y;
 }
 
-void BKE_tracking_undistort_v2(MovieTracking *tracking, const float co[2], float r_co[2])
+void BKE_tracking_undistort_v2(
+    MovieTracking *tracking, int image_width, int image_height, const float co[2], float r_co[2])
 {
   const MovieTrackingCamera *camera = &tracking->camera;
   const float aspy = 1.0f / tracking->camera.pixel_aspect;
 
   libmv_CameraIntrinsicsOptions camera_intrinsics_options;
-  tracking_cameraIntrinscisOptionsFromTracking(tracking, 0, 0, &camera_intrinsics_options);
+  tracking_cameraIntrinscisOptionsFromTracking(
+      tracking, image_width, image_height, &camera_intrinsics_options);
   libmv_CameraIntrinsics *intrinsics = libmv_cameraIntrinsicsNew(&camera_intrinsics_options);
 
   double x = co[0], y = co[1];
@@ -2361,13 +2365,19 @@ ImBuf *BKE_tracking_distort_frame(MovieTracking *tracking,
 }
 
 void BKE_tracking_max_distortion_delta_across_bound(MovieTracking *tracking,
+                                                    int image_width,
+                                                    int image_height,
                                                     rcti *rect,
                                                     bool undistort,
                                                     float delta[2])
 {
   float pos[2], warped_pos[2];
   const int coord_delta = 5;
-  void (*apply_distortion)(MovieTracking * tracking, const float pos[2], float out[2]);
+  void (*apply_distortion)(MovieTracking * tracking,
+                           int image_width,
+                           int image_height,
+                           const float pos[2],
+                           float out[2]);
 
   if (undistort) {
     apply_distortion = BKE_tracking_undistort_v2;
@@ -2387,7 +2397,7 @@ void BKE_tracking_max_distortion_delta_across_bound(MovieTracking *tracking,
     pos[0] = a;
     pos[1] = rect->ymin;
 
-    apply_distortion(tracking, pos, warped_pos);
+    apply_distortion(tracking, image_width, image_height, pos, warped_pos);
 
     delta[0] = max_ff(delta[0], fabsf(pos[0] - warped_pos[0]));
     delta[1] = max_ff(delta[1], fabsf(pos[1] - warped_pos[1]));
@@ -2396,7 +2406,7 @@ void BKE_tracking_max_distortion_delta_across_bound(MovieTracking *tracking,
     pos[0] = a;
     pos[1] = rect->ymax;
 
-    apply_distortion(tracking, pos, warped_pos);
+    apply_distortion(tracking, image_width, image_height, pos, warped_pos);
 
     delta[0] = max_ff(delta[0], fabsf(pos[0] - warped_pos[0]));
     delta[1] = max_ff(delta[1], fabsf(pos[1] - warped_pos[1]));
@@ -2415,7 +2425,7 @@ void BKE_tracking_max_distortion_delta_across_bound(MovieTracking *tracking,
     pos[0] = rect->xmin;
     pos[1] = a;
 
-    apply_distortion(tracking, pos, warped_pos);
+    apply_distortion(tracking, image_width, image_height, pos, warped_pos);
 
     delta[0] = max_ff(delta[0], fabsf(pos[0] - warped_pos[0]));
     delta[1] = max_ff(delta[1], fabsf(pos[1] - warped_pos[1]));
@@ -2424,7 +2434,7 @@ void BKE_tracking_max_distortion_delta_across_bound(MovieTracking *tracking,
     pos[0] = rect->xmax;
     pos[1] = a;
 
-    apply_distortion(tracking, pos, warped_pos);
+    apply_distortion(tracking, image_width, image_height, pos, warped_pos);
 
     delta[0] = max_ff(delta[0], fabsf(pos[0] - warped_pos[0]));
     delta[1] = max_ff(delta[1], fabsf(pos[1] - warped_pos[1]));
index ec858ee2c196220e19cdb86529497ca542aa7576..fe2943419c56df43afce69c49a89066ebfa4b1a9 100644 (file)
@@ -50,7 +50,8 @@ void MovieDistortionOperation::initExecution()
     full_frame.xmin = full_frame.ymin = 0;
     full_frame.xmax = this->m_width;
     full_frame.ymax = this->m_height;
-    BKE_tracking_max_distortion_delta_across_bound(tracking, &full_frame, !this->m_apply, delta);
+    BKE_tracking_max_distortion_delta_across_bound(
+        tracking, this->m_width, this->m_height, &full_frame, !this->m_apply, delta);
 
     /* 5 is just in case we didn't hit real max of distortion in
      * BKE_tracking_max_undistortion_delta_across_bound
index a1652ca0e88d9a5005d0452da34c986b5f5939d6..4a51b3c8b8aaef0a6bfefc7836b293992c83d666 100644 (file)
@@ -1651,7 +1651,7 @@ static void draw_tracking_tracks(SpaceClip *sc,
           pos[0] = (pos[0] / (pos[3] * 2.0f) + 0.5f) * width;
           pos[1] = (pos[1] / (pos[3] * 2.0f) + 0.5f) * height * aspy;
 
-          BKE_tracking_distort_v2(tracking, pos, npos);
+          BKE_tracking_distort_v2(tracking, width, height, pos, npos);
 
           if (npos[0] >= 0.0f && npos[1] >= 0.0f && npos[0] <= width && npos[1] <= height * aspy) {
             vec[0] = (marker->pos[0] + track->offset[0]) * width;
@@ -1777,7 +1777,7 @@ static void draw_distortion(SpaceClip *sc,
     for (i = 0; i <= n; i++) {
       for (j = 0; j <= n; j++) {
         if (i == 0 || j == 0 || i == n || j == n) {
-          BKE_tracking_distort_v2(tracking, pos, tpos);
+          BKE_tracking_distort_v2(tracking, width, height, pos, tpos);
 
           for (a = 0; a < 4; a++) {
             int ok;
@@ -1810,7 +1810,7 @@ static void draw_distortion(SpaceClip *sc,
       pos[0] = idx[a][0] * dx;
       pos[1] = idx[a][1] * dy;
 
-      BKE_tracking_undistort_v2(tracking, pos, tpos);
+      BKE_tracking_undistort_v2(tracking, width, height, pos, tpos);
 
       minmax_v2v2_v2(min, max, tpos);
     }
@@ -1821,7 +1821,7 @@ static void draw_distortion(SpaceClip *sc,
 
     for (i = 0; i <= n; i++) {
       for (j = 0; j <= n; j++) {
-        BKE_tracking_distort_v2(tracking, pos, grid[i][j]);
+        BKE_tracking_distort_v2(tracking, width, height, pos, grid[i][j]);
 
         grid[i][j][0] /= width;
         grid[i][j][1] /= height * aspy;
@@ -1897,8 +1897,8 @@ static void draw_distortion(SpaceClip *sc,
 
                 /* we want to distort only long straight lines */
                 if (stroke->totpoints == 2) {
-                  BKE_tracking_undistort_v2(tracking, pos, pos);
-                  BKE_tracking_undistort_v2(tracking, npos, npos);
+                  BKE_tracking_undistort_v2(tracking, width, height, pos, pos);
+                  BKE_tracking_undistort_v2(tracking, width, height, npos, npos);
                 }
 
                 sub_v2_v2v2(dpos, npos, pos);
@@ -1907,7 +1907,7 @@ static void draw_distortion(SpaceClip *sc,
                 immBegin(GPU_PRIM_LINE_STRIP, steps + 1);
 
                 for (j = 0; j <= steps; j++) {
-                  BKE_tracking_distort_v2(tracking, pos, tpos);
+                  BKE_tracking_distort_v2(tracking, width, height, pos, tpos);
                   immVertex2f(position, tpos[0] / width, tpos[1] / (height * aspy));
 
                   add_v2_v2(pos, dpos);
index cce01947ab7cbd16f8e01d2f6b32b907a00310c8..bb38338301381906a299b5982379a4330d3a056f 100644 (file)
@@ -546,7 +546,7 @@ void ED_clip_point_undistorted_pos(SpaceClip *sc, const float co[2], float r_co[
     r_co[0] *= width;
     r_co[1] *= height * aspy;
 
-    BKE_tracking_undistort_v2(&clip->tracking, r_co, r_co);
+    BKE_tracking_undistort_v2(&clip->tracking, width, height, r_co, r_co);
 
     r_co[0] /= width;
     r_co[1] /= height * aspy;
@@ -580,7 +580,7 @@ void ED_clip_point_stable_pos(
     float aspy = 1.0f / tracking->camera.pixel_aspect;
     float tmp[2] = {*xr * width, *yr * height * aspy};
 
-    BKE_tracking_distort_v2(tracking, tmp, tmp);
+    BKE_tracking_distort_v2(tracking, width, height, tmp, tmp);
 
     *xr = tmp[0] / width;
     *yr = tmp[1] / (height * aspy);
index 4a0df454a787a4bcedaebfd866af70ec905d9ac7..03f791ad70d9d121a0baf82a5d1c4e3808ddfc2c 100644 (file)
@@ -166,7 +166,8 @@ static float calculate_reprojection_error_at_marker(MovieClip *clip,
   reprojected_position[1] = (reprojected_position[1] / (reprojected_position[3] * 2.0f) + 0.5f) *
                             clip_height * aspy;
 
-  BKE_tracking_distort_v2(tracking, reprojected_position, reprojected_position);
+  BKE_tracking_distort_v2(
+      tracking, clip_width, clip_height, reprojected_position, reprojected_position);
 
   marker_position[0] = (marker->pos[0] + track->offset[0]) * clip_width;
   marker_position[1] = (marker->pos[1] + track->offset[1]) * clip_height * aspy;