[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [39552] branches/soc-2011-tomato: Camera tracking integration

Sergey Sharybin g.ulairi at gmail.com
Fri Aug 19 15:41:11 CEST 2011


Revision: 39552
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=39552
Author:   nazgul
Date:     2011-08-19 13:41:11 +0000 (Fri, 19 Aug 2011)
Log Message:
-----------
Camera tracking integration
===========================

Bundle new libmv and add changes needed to support affine tracking.

Affine tracking itself would be added in next commit.

Modified Paths:
--------------
    branches/soc-2011-tomato/extern/libmv/ChangeLog
    branches/soc-2011-tomato/extern/libmv/bundle.sh
    branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.cc
    branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.h
    branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp
    branches/soc-2011-tomato/extern/libmv/libmv-capi.h
    branches/soc-2011-tomato/release/scripts/startup/bl_ui/space_clip.py
    branches/soc-2011-tomato/source/blender/blenkernel/intern/movieclip.c
    branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c
    branches/soc-2011-tomato/source/blender/makesdna/DNA_tracking_types.h
    branches/soc-2011-tomato/source/blender/makesrna/intern/rna_tracking.c

Modified: branches/soc-2011-tomato/extern/libmv/ChangeLog
===================================================================
--- branches/soc-2011-tomato/extern/libmv/ChangeLog	2011-08-19 13:41:00 UTC (rev 39551)
+++ branches/soc-2011-tomato/extern/libmv/ChangeLog	2011-08-19 13:41:11 UTC (rev 39552)
@@ -1,3 +1,31 @@
+commit 702658d2f8616964a6eeb3743fd85e97ac7ff09d
+Author: Matthias Fauconneau <matthias.fauconneau at gmail.com>
+Date:   Fri Aug 19 14:59:24 2011 +0200
+
+    Expose regularization parameters (areaPenalty and conditionPenalty) in API.
+
+commit 3e84ae5fbac10451d4935418f6281a90cedace11
+Author: Matthias Fauconneau <matthias.fauconneau at gmail.com>
+Date:   Fri Aug 19 14:19:27 2011 +0200
+
+    Add LaplaceFilter.
+    Add regularization in affine SAD Tracker (keep constant area and good condition number).
+    UI: Better track display (+enable line antialiasing).
+
+commit 6d26d9a8ccc4ce009fbf253898fea8864dd5001a
+Author: Matthias Fauconneau <matthias.fauconneau at gmail.com>
+Date:   Fri Aug 19 10:25:26 2011 +0200
+
+    Add optimization for integer pixel search.
+    Allows more agressive settings for affine coordinate descent.
+
+commit 70ceae81c0ab561b07e640ecb9933f0a902b57cd
+Author: Matthias Fauconneau <matthias.fauconneau at gmail.com>
+Date:   Fri Aug 19 00:02:12 2011 +0200
+
+    Document coordinate descent method in affine SAD matcher.
+    Add heuristic to prevent high distortions.
+
 commit 75520f4bc4ccbb272a1b4149d3b8d05a90f7f896
 Author: Matthias Fauconneau <matthias.fauconneau at gmail.com>
 Date:   Thu Aug 18 23:14:17 2011 +0200
@@ -434,11 +462,3 @@
 Date:   Tue Jul 19 21:00:37 2011 +0200
 
     Progress in debugging optimized KLT.
-
-commit 354488cca6a29a4aa6ea3f943f693ae4af21e8da
-Author: Matthias Fauconneau <matthias.fauconneau at gmail.com>
-Date:   Tue Jul 19 14:54:43 2011 +0200
-
-    Add optimized version of KLT tracker.
-    
-    FIXME: It currently doesn't work.

Modified: branches/soc-2011-tomato/extern/libmv/bundle.sh
===================================================================
--- branches/soc-2011-tomato/extern/libmv/bundle.sh	2011-08-19 13:41:00 UTC (rev 39551)
+++ branches/soc-2011-tomato/extern/libmv/bundle.sh	2011-08-19 13:41:11 UTC (rev 39552)
@@ -3,6 +3,11 @@
 #BRANCH="keir"
 BRANCH="Matthias-Fauconneau"
 
+if [ -d ./.svn ]; then
+  echo "This script is supposed to work only when using git-svn"
+  exit 1
+fi
+
 repo="git://github.com/${BRANCH}/libmv.git"
 tmp=`mktemp -d`
 

Modified: branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.cc
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.cc	2011-08-19 13:41:00 UTC (rev 39551)
+++ branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.cc	2011-08-19 13:41:11 UTC (rev 39552)
@@ -28,6 +28,20 @@
 
 namespace libmv {
 
+void LaplaceFilter(ubyte* src, ubyte* dst, int width, int height, int strength) {
+  for(int y=1; y<height-1; y++) for(int x=1; x<width-1; x++) {
+    const ubyte* s = &src[y*width+x];
+    int l = 128 +
+        s[-width-1] + s[-width] + s[-width+1] +
+        s[1]        - 8*s[0]    + s[1]        +
+        s[ width-1] + s[ width] + s[ width+1] ;
+    int d = ((256-strength)*s[0] + strength*l) / 256;
+    if(d < 0) d=0;
+    if(d > 255) d=255;
+    dst[y*width+x] = d;
+  }
+}
+
 struct vec2 {
   float x,y;
   inline vec2(float x, float y):x(x),y(y){}
@@ -86,7 +100,8 @@
 }
 #endif
 
-float Track(ubyte* reference, int size, ubyte* image, int stride, int w, int h, mat32* warp) {
+float sq(float x) { return x*x; }
+float Track(ubyte* reference, ubyte* warped, int size, ubyte* image, int stride, int w, int h, mat32* warp, float areaPenalty, float conditionPenalty) {
   mat32 m=*warp;
   uint min=-1;
 
@@ -95,9 +110,8 @@
   for(int y = size/2; y < h-size/2; y++) {
     for(int x = size/2; x < w-size/2; x++) {
       m(0,2) = x, m(1,2) = y;
-      ubyte match[size*size];
-      SamplePattern(image,stride,m,match,size);
-      uint sad = SAD(reference,match,size,size);
+      uint sad = SAD(warped,&image[(y-size/2)*stride+(x-size/2)],stride,size);
+      // TODO: using chroma could help disambiguate some cases
       if(sad < min) {
         min = sad;
         ix = x, iy = y;
@@ -105,20 +119,26 @@
     }
   }
   m(0,2) = ix, m(1,2) = iy;
+  min=-1; //reset score since direct warped search match too well (but the wrong pattern).
 
   // 6D coordinate descent to find affine transform
   float step = 0.5;
-  for(int p = 0; p < 4; p++) { //foreach precision level
-    step /= 2;
+  for(int p = 0; p < 8; p++) { //foreach precision level
     for(int i = 0; i < 2; i++) { // iterate twice per precision level
-      for(int d = 0; d < 6; d++) { // foreach dimension
-        for(float x = -step*2; x <= step*2; x+=step*2) { //test small steps
+      //TODO: other sweep pattern might converge better
+      for(int d=0; d < 6; d++) { // iterate dimension sequentially (cyclic descent)
+        for(float x = -step; x <= step; x+=step) { //solve subproblem (evaluate only along one coordinate)
           mat32 t = m;
           t.data[d] += x;
-          if( d<4 && (t.data[d] > 2 || t.data[d] < -2) ) continue; // avoid big distortion
           ubyte match[size*size];
+          //TODO: better performance would also allow a more exhaustive search
           SamplePattern(image,stride,t,match,size);
           uint sad = SAD(reference,match,size,size);
+          // regularization: keep constant area and good condition
+          float area = t(0,0)*t(1,1)-t(0,1)*t(1,0);
+          float x = sq(t(0,0))+sq(t(0,1)), y = sq(t(1,0))+sq(t(1,1));
+          float condition = x>y ? x/y : y/x;
+          sad += size*size*( areaPenalty*sq(area-1) + conditionPenalty*sq(condition-1) );
           if(sad < min) {
             min = sad;
             m = t;
@@ -126,6 +146,7 @@
         }
       }
     }
+    step /= 2;
   }
   *warp = m;
 

Modified: branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.h
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.h	2011-08-19 13:41:00 UTC (rev 39551)
+++ branches/soc-2011-tomato/extern/libmv/libmv/tracking/sad.h	2011-08-19 13:41:11 UTC (rev 39552)
@@ -32,6 +32,16 @@
 typedef unsigned char ubyte;
 typedef unsigned int uint;
 
+/*!
+    Convolve \a src into \a dst with the discrete laplacian operator.
+
+    \a src and \a dst should be \a width x \a height images.
+    \a strength is an interpolation coefficient (0-256) between original image and the laplacian.
+
+    \note Make sure the search region is filtered with the same strength as the pattern.
+*/
+void LaplaceFilter(ubyte* src, ubyte* dst, int width, int height, int strength);
+
 /// Affine transformation matrix in column major order.
 struct mat32 {
   float data[3*2];
@@ -45,7 +55,6 @@
 #endif
 };
 
-
 /*!
     Sample \a pattern from \a image.
 
@@ -63,13 +72,22 @@
     A similar method is used for motion estimation in video encoders.
 
     \a reference is the pattern to track.
-    the \a size of the pattern should be aligned to 16.
+    \a warped is a warped version of reference for fast unsampled integer search.
+       Best is to directly extract an already warped pattern from previous frame.
+    The \a size of the patterns should be aligned to 16.
     \a image is a reference to the region to search.
     \a stride is size of \a image lines.
 
     On input, \a warp is the predicted affine transformation (e.g from previous frame)
     On return, \a warp is the affine transformation which best match the reference \a pattern
 
+    \a areaPenalty and conditionPenalty control the regularization and need to be tweaked depending on the motion.
+       Setting them to 0 will allow any transformation (including unrealistic distortions and scaling).
+       Good values are between 0-32. 16 can be used as a realistic default.
+       areaPenalty control scaling (decrease to allow pull/zoom, increase to allow only 2D rotation).
+       a large conditionPenalty avoid a large ratio between the largest and smallest axices.
+       It need to be decreased for non-2D rotation (when pattern appears to scale along an axis).
+
     \return Pearson product-moment correlation coefficient between reference and matched pattern.
             This measure of the linear dependence between the patterns
             ranges from −1 (negative correlation) to 1 (positive correlation).
@@ -81,7 +99,8 @@
     \note \a stride allow you to reference your search region instead of copying.
     \note For a 16x speedup, compile this tracker with SSE2 support.
 */
-float Track(ubyte* reference, int size, ubyte* image, int stride, int width, int height, mat32* warp);
+float Track(ubyte* reference, ubyte* warped, int size, ubyte* image, int stride, int width, int height, mat32* warp,
+            float areaPenalty, float conditionPenalty);
 
 #ifdef __cplusplus
 }  // namespace libmv

Modified: branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp	2011-08-19 13:41:00 UTC (rev 39551)
+++ branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp	2011-08-19 13:41:11 UTC (rev 39552)
@@ -324,19 +324,17 @@
 	libmv::SamplePattern(image, stride, mat32, pattern, 16);
 }
 
-int libmv_SADTrackerTrack(unsigned char *pattern, unsigned char *image, int stride,
-			int width, int height, double *x, double *y)
+float libmv_SADTrackerTrack(unsigned char *pattern, unsigned char *warped, unsigned char *image, int stride,
+			int width, int height, float warp[3][2])
 {
-	int result;
+	float result;
 	libmv::mat32 mat32;
 
-	mat32(0, 2)= *x;
-	mat32(1, 2)= *y;
+	memcpy(mat32.data, warp, sizeof(float)*3*2);
 
-	result = libmv::Track(pattern, 16, image, stride, width, height, &mat32);
+	result = libmv::Track(pattern, warped, 16, image, stride, width, height, &mat32, 16, 16);
 
-	*x= mat32(0, 2);
-	*y= mat32(1, 2);
+	memcpy(warp, mat32.data, sizeof(float)*3*2);
 
 	return result;
 }

Modified: branches/soc-2011-tomato/extern/libmv/libmv-capi.h
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv-capi.h	2011-08-19 13:41:00 UTC (rev 39551)
+++ branches/soc-2011-tomato/extern/libmv/libmv-capi.h	2011-08-19 13:41:11 UTC (rev 39552)
@@ -53,8 +53,8 @@
 /* SAD Tracker */
 void libmv_SADSamplePattern(unsigned char *image, int stride,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list