Camera tracking: presets for default tracking settings
authorSergey Sharybin <sergey.vfx@gmail.com>
Sat, 26 Nov 2011 21:09:29 +0000 (21:09 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Sat, 26 Nov 2011 21:09:29 +0000 (21:09 +0000)
Internal changes are almost finished, changes in the interface need to be discussed.

release/scripts/presets/tracking_settings/blurry_movie.py [new file with mode: 0644]
release/scripts/presets/tracking_settings/default.py [new file with mode: 0644]
release/scripts/presets/tracking_settings/fast_motion.py [new file with mode: 0644]
release/scripts/startup/bl_operators/presets.py
release/scripts/startup/bl_ui/space_clip.py
source/blender/blenkernel/BKE_tracking.h
source/blender/blenkernel/intern/movieclip.c
source/blender/blenkernel/intern/tracking.c
source/blender/blenloader/intern/readfile.c
source/blender/makesdna/DNA_tracking_types.h
source/blender/makesrna/intern/rna_tracking.c

diff --git a/release/scripts/presets/tracking_settings/blurry_movie.py b/release/scripts/presets/tracking_settings/blurry_movie.py
new file mode 100644 (file)
index 0000000..ad96533
--- /dev/null
@@ -0,0 +1,8 @@
+import bpy
+settings = bpy.context.edit_movieclip.tracking.settings
+
+settings.default_tracker = 'KLT'
+settings.default_pyramid_levels = 4
+settings.default_correlation_min = 0.75
+settings.default_pattern_size = 11
+settings.default_search_size = 202
diff --git a/release/scripts/presets/tracking_settings/default.py b/release/scripts/presets/tracking_settings/default.py
new file mode 100644 (file)
index 0000000..4f1e23f
--- /dev/null
@@ -0,0 +1,8 @@
+import bpy
+settings = bpy.context.edit_movieclip.tracking.settings
+
+settings.default_tracker = 'KLT'
+settings.default_pyramid_levels = 2
+settings.default_correlation_min = 0.75
+settings.default_pattern_size = 11
+settings.default_search_size = 51
diff --git a/release/scripts/presets/tracking_settings/fast_motion.py b/release/scripts/presets/tracking_settings/fast_motion.py
new file mode 100644 (file)
index 0000000..0ca768a
--- /dev/null
@@ -0,0 +1,8 @@
+import bpy
+settings = bpy.context.edit_movieclip.tracking.settings
+
+settings.default_tracker = 'KLT'
+settings.default_pyramid_levels = 2
+settings.default_correlation_min = 0.75
+settings.default_pattern_size = 11
+settings.default_search_size = 121
index 100b21f..1eeae28 100644 (file)
@@ -360,6 +360,27 @@ class AddPresetTrackingTrackColor(AddPresetBase, Operator):
     preset_subdir = "tracking_track_color"
 
 
+class AddPresetTrackingSettings(AddPresetBase, Operator):
+    '''Add a motion tracking settings preset'''
+    bl_idname = "clip.tracking_settings_preset_add"
+    bl_label = "Add Tracking Settings Preset"
+    preset_menu = "CLIP_MT_tracking_settings_presets"
+
+    preset_defines = [
+        "settings = bpy.context.edit_movieclip.tracking.settings"
+    ]
+
+    preset_values = [
+        "settings.default_tracker",
+        "settings.default_pyramid_levels",
+        "settings.default_correlation_min",
+        "settings.default_pattern_size",
+        "settings.default_search_size",
+    ]
+
+    preset_subdir = "tracking_settings"
+
+
 class AddPresetKeyconfig(AddPresetBase, Operator):
     '''Add a Keyconfig Preset'''
     bl_idname = "wm.keyconfig_preset_add"
index b04d81b..dbfa6a2 100644 (file)
@@ -92,6 +92,9 @@ class CLIP_PT_tools_marker(Panel):
         return clip and sc.mode == 'TRACKING'
 
     def draw(self, context):
+        sc = context.space_data
+        clip = sc.clip
+        settings = clip.tracking.settings
         layout = self.layout
 
         col = layout.column(align=True)
@@ -99,6 +102,36 @@ class CLIP_PT_tools_marker(Panel):
         col.operator("clip.detect_features")
         col.operator("clip.delete_track")
 
+        box = layout.box()
+        row = box.row(align=True)
+        row.prop(settings, "show_default_expanded", text="", emboss=False)
+        row.label(text="Default Settings")
+
+        if settings.show_default_expanded:
+            col = box.column()
+            row = col.row(align=True)
+            label = bpy.types.CLIP_MT_tracking_settings_presets.bl_label
+            row.menu('CLIP_MT_tracking_settings_presets', text=label)
+            row.operator("clip.tracking_settings_preset_add",
+                         text="", icon='ZOOMIN')
+            props = row.operator("clip.track_color_preset_add",
+                                 text="", icon='ZOOMOUT')
+            props.remove_active = True
+
+            col.separator()
+
+            col2 = col.column(align=True)
+            col2.prop(settings, "default_pattern_size")
+            col2.prop(settings, "default_search_size")
+
+            col.label(text="Tracker:")
+            col.prop(settings, "default_tracker", text="")
+
+            if settings.default_tracker == 'KLT':
+                col.prop(settings, "default_pyramid_levels")
+            else:
+                col.prop(settings, "default_correlation_min")
+
 
 class CLIP_PT_tools_tracking(Panel):
     bl_space_type = 'CLIP_EDITOR'
@@ -447,18 +480,24 @@ class CLIP_PT_track_settings(Panel):
         clip = context.space_data.clip
         settings = clip.tracking.settings
 
+        col = layout.column()
+
         active = clip.tracking.tracks.active
         if active:
-            layout.prop(active, "tracker")
+            col.prop(active, "tracker")
+
             if active.tracker == 'KLT':
-                layout.prop(active, "pyramid_levels")
+                col.prop(active, "pyramid_levels")
             elif active.tracker == 'SAD':
-                layout.prop(active, "correlation_min")
+                col.prop(active, "correlation_min")
+
+            col.separator()
+            col.label(text="Global Settings:")
 
-        layout.prop(settings, "frames_adjust")
-        layout.prop(settings, "speed")
-        layout.prop(settings, "frames_limit")
-        layout.prop(settings, "margin")
+        col.prop(settings, "speed")
+        col.prop(settings, "frames_adjust")
+        col.prop(settings, "frames_limit")
+        col.prop(settings, "margin")
 
 
 class CLIP_PT_stabilization(Panel):
@@ -866,6 +905,14 @@ class CLIP_MT_track_color_presets(Menu):
     draw = bpy.types.Menu.draw_preset
 
 
+class CLIP_MT_tracking_settings_presets(Menu):
+    """Predefined track color"""
+    bl_label = "Tracking Settings"
+    preset_subdir = "tracking_settings"
+    preset_operator = "script.execute_preset"
+    draw = bpy.types.Menu.draw_preset
+
+
 class CLIP_MT_track_color_specials(Menu):
     bl_label = "Track Color Specials"
 
index 4a3b505..e55d40e 100644 (file)
@@ -45,6 +45,7 @@ struct Camera;
 struct Object;
 struct Scene;
 
+void BKE_tracking_init_settings(struct MovieTracking *tracking);
 void BKE_tracking_clamp_track(struct MovieTrackingTrack *track, int event);
 void BKE_tracking_track_flag(struct MovieTrackingTrack *track, int area, int flag, int clear);
 
index 382e1d7..75d8ec5 100644 (file)
@@ -369,19 +369,7 @@ static MovieClip *movieclip_alloc(const char *name)
 
        clip->aspx= clip->aspy= 1.0f;
 
-       clip->tracking.camera.sensor_width= 35.0f;
-       clip->tracking.camera.pixel_aspect= 1.0f;
-       clip->tracking.camera.units= CAMERA_UNITS_MM;
-
-       clip->tracking.settings.frames_limit= 0;
-       clip->tracking.settings.keyframe1= 1;
-       clip->tracking.settings.keyframe2= 30;
-       clip->tracking.settings.dist= 1;
-
-       clip->tracking.stabilization.scaleinf= 1.0f;
-       clip->tracking.stabilization.locinf= 1.0f;
-       clip->tracking.stabilization.rotinf= 1.0f;
-       clip->tracking.stabilization.maxscale= 2.0f;
+       BKE_tracking_init_settings(&clip->tracking);
 
        clip->proxy.build_size_flag= IMB_PROXY_25;
        clip->proxy.build_tc_flag= IMB_TC_RECORD_RUN|IMB_TC_FREE_RUN|IMB_TC_INTERPOLATED_REC_DATE_FREE_RUN;
index 65ca0a8..449832f 100644 (file)
@@ -70,14 +70,38 @@ typedef struct MovieDistortion {
 
 /*********************** common functions *************************/
 
+void BKE_tracking_init_settings(MovieTracking *tracking)
+{
+       tracking->camera.sensor_width= 35.0f;
+       tracking->camera.pixel_aspect= 1.0f;
+       tracking->camera.units= CAMERA_UNITS_MM;
+
+       tracking->settings.default_tracker= TRACKER_KLT;
+       tracking->settings.default_pyramid_levels= 2;
+       tracking->settings.default_minimum_correlation= 0.75;
+       tracking->settings.default_pattern_size= 11;
+       tracking->settings.default_search_size= 51;
+       tracking->settings.default_pyramid_levels= 2;
+       tracking->settings.frames_limit= 0;
+       tracking->settings.keyframe1= 1;
+       tracking->settings.keyframe2= 30;
+       tracking->settings.dist= 1;
+
+       tracking->stabilization.scaleinf= 1.0f;
+       tracking->stabilization.locinf= 1.0f;
+       tracking->stabilization.rotinf= 1.0f;
+       tracking->stabilization.maxscale= 2.0f;
+}
+
 void BKE_tracking_clamp_track(MovieTrackingTrack *track, int event)
 {
        int a;
        float pat_min[2];
        float pat_max[2];
        float max_pyramid_level_factor = 1.0;
+
        if (track->tracker == TRACKER_KLT) {
-               max_pyramid_level_factor = 1 << (track->pyramid_levels - 1);
+               max_pyramid_level_factor= 1 << (track->pyramid_levels - 1);
        }
 
        /* sort */
@@ -146,7 +170,7 @@ void BKE_tracking_clamp_track(MovieTrackingTrack *track, int event)
                float dim[2];
                sub_v2_v2v2(dim, track->pat_max, track->pat_min);
                {
-                       float search_ratio = 2.3f * max_pyramid_level_factor;
+                       float search_ratio= 2.3f * max_pyramid_level_factor;
 
                        /* resize the search area to something sensible based
                         * on the number of pyramid levels */
@@ -190,25 +214,27 @@ MovieTrackingTrack *BKE_tracking_add_track(MovieTracking *tracking, float x, flo
 {
        MovieTrackingTrack *track;
        MovieTrackingMarker marker;
+       MovieTrackingSettings *settings= &tracking->settings;
 
-       /* pick reasonable defaults */
-       float pat[2]= {5.5f, 5.5f}, search[2]= {25.5f, 25.5f}; /* TODO: move to default setting? */
+       float half_pattern= (float)settings->default_pattern_size/2.0f;
+       float half_search= (float)settings->default_search_size/2.0f;
+       float pat[2], search[2];
 
-       pat[0] /= (float)width;
-       pat[1] /= (float)height;
+       pat[0]= half_pattern/(float)width;
+       pat[1]= half_pattern/(float)height;
 
-       search[0] /= (float)width;
-       search[1] /= (float)height;
+       search[0]= half_search/(float)width;
+       search[1]= half_search/(float)height;
 
        track= MEM_callocN(sizeof(MovieTrackingTrack), "add_marker_exec track");
        strcpy(track->name, "Track");
 
        /* default to KLT tracker */
-       track->tracker = TRACKER_KLT;
-       track->pyramid_levels = 2;
+       track->tracker = settings->default_tracker;
+       track->pyramid_levels = settings->default_pyramid_levels;
 
        /* set SAD defaults even though it's not selected by default */
-       track->minimum_correlation= 0.75f;
+       track->minimum_correlation= settings->default_minimum_correlation;
 
        memset(&marker, 0, sizeof(marker));
        marker.pos[0]= x;
@@ -223,6 +249,8 @@ MovieTrackingTrack *BKE_tracking_add_track(MovieTracking *tracking, float x, flo
 
        BKE_tracking_insert_marker(track, &marker);
 
+       BKE_tracking_clamp_track(track, CLAMP_PYRAMID_LEVELS);
+
        BLI_addtail(&tracking->tracks, track);
        BKE_track_unique_name(tracking, track);
 
index 1912973..3db36a0 100644 (file)
@@ -12607,11 +12607,25 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        /* put compatibility code here until next subversion bump */
        {
                Scene *sce;
+               MovieClip *clip;
+
                for(sce = main->scene.first; sce; sce = sce->id.next) {
                        if (sce->r.im_format.depth == 0) {
                                do_versions_image_settings_2_60(sce);
                        }
                }
+
+               for (clip= main->movieclip.first; clip; clip= clip->id.next) {
+                       MovieTrackingSettings *settings= &clip->tracking.settings;
+
+                       if(settings->default_pyramid_levels==0) {
+                               settings->default_tracker= TRACKER_KLT;
+                               settings->default_pyramid_levels= 2;
+                               settings->default_minimum_correlation= 0.75;
+                               settings->default_pattern_size= 11;
+                               settings->default_search_size= 51;
+                       }
+               }
        }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
index 88173e6..635be80 100644 (file)
@@ -107,16 +107,25 @@ typedef struct MovieTrackingTrack {
        float minimum_correlation;                      /* minimal correlation which is still treated as successful tracking */
 
        /* ** KLT tracker settings ** */
-       int pyramid_levels;             /* number of pyramid levels to use for KLT tracking */
-       char pad5[4];
+       short pyramid_levels;           /* number of pyramid levels to use for KLT tracking */
+       char pad5[6];
 } MovieTrackingTrack;
 
 typedef struct MovieTrackingSettings {
+       /* ** default tracker settings */
+       short default_tracker;                          /* tracking algorithm used by default */
+       short default_pyramid_levels;           /* number of pyramid levels to use for KLT tracking */
+       float default_minimum_correlation;      /* minimal correlation which is still treated as successful tracking */
+       short default_pattern_size;                     /* size of pattern area for new tracks */
+       short default_search_size;                      /* size of search area for new tracks */
+
        /* ** common tracker settings ** */
        short speed;                    /* speed of tracking */
        short frames_limit;             /* number of frames to be tarcked during single tracking session (if TRACKING_FRAMES_LIMIT is set) */
        short margin;                   /* margin from frame boundaries */
-       char pad[2];
+
+       short pad;
+       int flag;
 
        int adjframes;                  /* re-adjust every N frames */
 
@@ -125,8 +134,7 @@ typedef struct MovieTrackingSettings {
 
        /* ** which camera intrinsics to refine. uses on the REFINE_* flags */
        short refine_camera_intrinsics;
-
-       char pad2[6];
+       short pad2;
 
        /* ** tool settings ** */
 
@@ -172,8 +180,6 @@ typedef struct MovieTrackingStats {
 
 typedef struct MovieTracking {
        MovieTrackingSettings settings; /* different tracking-related settings */
-       char pad2[4];
-
        MovieTrackingCamera camera;             /* camera intrinsics */
        ListBase tracks;                                /* all tracks */
        MovieTrackingReconstruction reconstruction;     /* reconstruction data */
@@ -204,6 +210,9 @@ enum {
 #define TRACK_CUSTOMCOLOR      (1<<7)
 #define TRACK_USE_2D_STAB      (1<<8)
 
+/* MovieTrackingSettings->flag */
+#define TRACKING_SETTINGS_SHOW_DEFAULT_EXPANDED        (1<<0)
+
 /* MovieTrackingSettings->tracker */
 #define TRACKER_KLT            0
 #define TRACKER_SAD            1
index eb35120..8d20aea 100644 (file)
 
 #include "WM_api.h"
 
+static void rna_tracking_defaultSettings_levelsUpdate(Main *UNUSED(bmain), Scene *scene, PointerRNA *ptr)
+{
+       MovieClip *clip= (MovieClip*)ptr->id.data;
+       MovieTracking *tracking= &clip->tracking;
+       MovieTrackingSettings *settings= &tracking->settings;
+
+       if(settings->default_tracker==TRACKER_KLT) {
+               int max_pyramid_level_factor= 1 << (settings->default_pyramid_levels - 1);
+               float search_ratio= 2.3f * max_pyramid_level_factor;
+
+               settings->default_search_size= settings->default_pattern_size*search_ratio;
+       }
+}
+
+static void rna_tracking_defaultSettings_patternUpdate(Main *UNUSED(bmain), Scene *scene, PointerRNA *ptr)
+{
+       MovieClip *clip= (MovieClip*)ptr->id.data;
+       MovieTracking *tracking= &clip->tracking;
+       MovieTrackingSettings *settings= &tracking->settings;
+
+       if(settings->default_search_size<settings->default_pattern_size)
+               settings->default_search_size= settings->default_pattern_size;
+}
+
+static void rna_tracking_defaultSettings_searchUpdate(Main *UNUSED(bmain), Scene *scene, PointerRNA *ptr)
+{
+       MovieClip *clip= (MovieClip*)ptr->id.data;
+       MovieTracking *tracking= &clip->tracking;
+       MovieTrackingSettings *settings= &tracking->settings;
+
+       if(settings->default_pattern_size>settings->default_search_size)
+               settings->default_pattern_size= settings->default_search_size;
+}
+
 static void rna_tracking_tracks_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
 {
        MovieClip *clip= (MovieClip*)ptr->id.data;
@@ -232,6 +266,11 @@ static MovieTrackingMarker *rna_trackingTrack_marker_find_frame(MovieTrackingTra
 
 #else
 
+static EnumPropertyItem tracker_items[] = {
+       {TRACKER_KLT, "KLT", 0, "KLT", "Kanade–Lucas–Tomasi tracker which works with most of video clips, a bit slower than SAD"},
+       {TRACKER_SAD, "SAD", 0, "SAD", "Sum of Absolute Differences tracker which can be used when MLT tracker fails"},
+       {0, NULL, 0, NULL, NULL}};
+
 static int rna_matrix_dimsize_4x4[]= {4, 4};
 
 static void rna_def_trackingSettings(BlenderRNA *brna)
@@ -348,6 +387,56 @@ static void rna_def_trackingSettings(BlenderRNA *brna)
        RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
        RNA_def_property_enum_items(prop, cleanup_items);
        RNA_def_property_ui_text(prop, "Action", "Cleanup action to execute");
+
+       /* ** default tracker settings ** */
+
+       prop= RNA_def_property(srna, "show_default_expanded", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flag", TRACKING_SETTINGS_SHOW_DEFAULT_EXPANDED);
+       RNA_def_property_ui_text(prop, "Show Expanded", "Show the expanded in the user interface");
+       RNA_def_property_ui_icon(prop, ICON_TRIA_RIGHT, 1);
+
+       /* tracking algorithm */
+       prop= RNA_def_property(srna, "default_tracker", PROP_ENUM, PROP_NONE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_enum_items(prop, tracker_items);
+       RNA_def_property_update(prop, 0, "rna_tracking_defaultSettings_levelsUpdate");
+       RNA_def_property_ui_text(prop, "Tracker", "Tracking algorithm to use");
+
+       /* pyramid level for pyramid klt tracking */
+       prop= RNA_def_property(srna, "default_pyramid_levels", PROP_INT, PROP_NONE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_int_sdna(prop, NULL, "default_pyramid_levels");
+       RNA_def_property_range(prop, 1, 16);
+       RNA_def_property_update(prop, 0, "rna_tracking_defaultSettings_levelsUpdate");
+       RNA_def_property_ui_text(prop, "Pyramid levels", "Default number of pyramid levels (increase on blurry footage)");
+
+       /* minmal correlation - only used for SAD tracker */
+       prop= RNA_def_property(srna, "default_correlation_min", PROP_FLOAT, PROP_NONE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_float_sdna(prop, NULL, "default_minimum_correlation");
+       RNA_def_property_range(prop, -1.0f, 1.0f);
+       RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3);
+       RNA_def_property_ui_text(prop, "Correlation", "Default minimal value of correlation between matched pattern and reference which is still treated as successful tracking");
+
+       /* default pattern size */
+       prop= RNA_def_property(srna, "default_pattern_size", PROP_INT, PROP_NONE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_int_sdna(prop, NULL, "default_pattern_size");
+       RNA_def_property_range(prop, 5, 1000);
+       RNA_def_property_update(prop, 0, "rna_tracking_defaultSettings_patternUpdate");
+       RNA_def_property_ui_text(prop, "Pattern Size", "Size of pattern area for newly creating tracks");
+
+       /* default search size */
+       prop= RNA_def_property(srna, "default_search_size", PROP_INT, PROP_NONE);
+       RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+       RNA_def_property_int_sdna(prop, NULL, "default_search_size");
+       RNA_def_property_range(prop, 5, 1000);
+       RNA_def_property_update(prop, 0, "rna_tracking_defaultSettings_searchUpdate");
+       RNA_def_property_ui_text(prop, "Search Size", "Size of search area for newly creating tracks");
 }
 
 static void rna_def_trackingCamera(BlenderRNA *brna)
@@ -465,11 +554,6 @@ static void rna_def_trackingTrack(BlenderRNA *brna)
        FunctionRNA *func;
        PropertyRNA *parm;
 
-       static EnumPropertyItem tracker_items[] = {
-               {TRACKER_KLT, "KLT", 0, "KLT", "Kanade–Lucas–Tomasi tracker which works with most of video clips, a bit slower than SAD"},
-               {TRACKER_SAD, "SAD", 0, "SAD", "Sum of Absolute Differences tracker which can be used when MLT tracker fails"},
-               {0, NULL, 0, NULL, NULL}};
-
        rna_def_trackingMarker(brna);
 
        srna= RNA_def_struct(brna, "MovieTrackingTrack", NULL);
@@ -535,7 +619,7 @@ static void rna_def_trackingTrack(BlenderRNA *brna)
        RNA_def_property_float_sdna(prop, NULL, "minimum_correlation");
        RNA_def_property_range(prop, -1.0f, 1.0f);
        RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3);
-       RNA_def_property_ui_text(prop, "Correlation", "Minimal value of correlation between mathed pattern and reference which is still treated as successful tracking");
+       RNA_def_property_ui_text(prop, "Correlation", "Minimal value of correlation between matched pattern and reference which is still treated as successful tracking");
 
        /* markers */
        prop= RNA_def_property(srna, "markers", PROP_COLLECTION, PROP_NONE);