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

Sergey Sharybin g.ulairi at gmail.com
Wed Sep 28 15:31:33 CEST 2011


Revision: 40656
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=40656
Author:   nazgul
Date:     2011-09-28 13:31:32 +0000 (Wed, 28 Sep 2011)
Log Message:
-----------
Camera tracking integration
===========================

- Merge Movie Distort and Movie Undistort nodes into one node
  called Movie Distortion where action (distort/undistort is specified).
- Implemented more proper distortion/undistortion for scaled images.

NOTE: Please, delete distortion nodes with current blender first
      before opening files with blender versions from this commit/

Modified Paths:
--------------
    branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.cc
    branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.h
    branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp
    branches/soc-2011-tomato/extern/libmv/libmv-capi.h
    branches/soc-2011-tomato/extern/libmv/patches/series
    branches/soc-2011-tomato/source/blender/blenkernel/BKE_node.h
    branches/soc-2011-tomato/source/blender/blenkernel/BKE_tracking.h
    branches/soc-2011-tomato/source/blender/blenkernel/intern/movieclip.c
    branches/soc-2011-tomato/source/blender/blenkernel/intern/node.c
    branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c
    branches/soc-2011-tomato/source/blender/editors/space_node/drawnode.c
    branches/soc-2011-tomato/source/blender/makesrna/intern/rna_nodetree.c
    branches/soc-2011-tomato/source/blender/makesrna/intern/rna_nodetree_types.h
    branches/soc-2011-tomato/source/blender/nodes/CMakeLists.txt
    branches/soc-2011-tomato/source/blender/nodes/NOD_composite.h
    branches/soc-2011-tomato/source/blender/nodes/composite/nodes/node_composite_movieclip.c

Added Paths:
-----------
    branches/soc-2011-tomato/extern/libmv/patches/scaled_distortion.patch
    branches/soc-2011-tomato/source/blender/nodes/composite/nodes/node_composite_moviedistortion.c

Removed Paths:
-------------
    branches/soc-2011-tomato/source/blender/nodes/composite/nodes/node_composite_moviedistort.c
    branches/soc-2011-tomato/source/blender/nodes/composite/nodes/node_composite_movieundistort.c

Modified: branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.cc
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.cc	2011-09-28 13:31:18 UTC (rev 40655)
+++ branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.cc	2011-09-28 13:31:32 UTC (rev 40656)
@@ -24,6 +24,7 @@
 namespace libmv {
 
 struct Offset { signed char ix,iy; unsigned char fx,fy; };
+struct Grid { struct Offset *offset; int width, height; };
 
 CameraIntrinsics::CameraIntrinsics()
     : K_(Mat3::Identity()),
@@ -38,8 +39,7 @@
       undistort_(0) {}
 
 CameraIntrinsics::~CameraIntrinsics() {
-  if(distort_) delete[] distort_;
-  if(undistort_) delete[] undistort_;
+  FreeLookupGrid();
 }
 
 /// Set the entire calibration matrix at once.
@@ -146,11 +146,17 @@
 
 // TODO(MatthiasF): downsample lookup
 template<typename WarpFunction>
-void CameraIntrinsics::ComputeLookupGrid(Offset* grid, int width, int height) {
+void CameraIntrinsics::ComputeLookupGrid(Grid* grid, int width, int height) {
+  double aspx = (double)width / image_width_;
+  double aspy = (double)height / image_height_;
+
   for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
+      double src_x = x / aspx, src_y = y / aspy;
       double warp_x, warp_y;
-      WarpFunction(this,x,y,&warp_x,&warp_y);
+      WarpFunction(this,src_x,src_y,&warp_x,&warp_y);
+      warp_x *= aspx;
+      warp_y *= aspy;
       int ix = int(warp_x), iy = int(warp_y);
       int fx = round((warp_x-ix)*256), fy = round((warp_y-iy)*256);
       if(fx == 256) { fx=0; ix++; }
@@ -162,10 +168,10 @@
       if( iy >= height-2 ) iy = height-2;
       if ( ix-x > -128 && ix-x < 128 && iy-y > -128 && iy-y < 128 ) {
         Offset offset = { ix-x, iy-y, fx, fy };
-        grid[y*width+x] = offset;
+        grid->offset[y*width+x] = offset;
       } else {
         Offset offset = { 0, 0, 0, 0 };
-        grid[y*width+x] = offset;
+        grid->offset[y*width+x] = offset;
       }
     }
   }
@@ -173,11 +179,11 @@
 
 // TODO(MatthiasF): cubic B-Spline image sampling, bilinear lookup
 template<typename T,int N>
-static void Warp(const Offset* grid, const T* src, T* dst,
+static void Warp(const Grid* grid, const T* src, T* dst,
                  int width, int height) {
   for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
-      Offset offset = grid[y*width+x];
+      Offset offset = grid->offset[y*width+x];
       const T* s = &src[((y+offset.iy)*width+(x+offset.ix))*N];
       for (int i = 0; i < N; i++) {
         dst[(y*width+x)*N+i] = ((s[        i] * (256-offset.fx) + s[        N+i] * offset.fx) * (256-offset.fy)
@@ -188,8 +194,17 @@
 }
 
 void CameraIntrinsics::FreeLookupGrid() {
-  if(distort_) delete distort_, distort_=0;
-  if(undistort_) delete undistort_, undistort_=0;
+  if(distort_) {
+    delete distort_;
+    delete distort_->offset;
+    distort_ = NULL;
+  }
+
+  if(undistort_) {
+    delete undistort_;
+    delete undistort_->offset;
+    undistort_ = NULL;
+  }
 }
 
 // FIXME: C++ templates limitations makes thing complicated, but maybe there is a simpler method.
@@ -211,11 +226,50 @@
   }
 };
 
+void CameraIntrinsics::CheckDistortLookupGrid(int width, int height)
+{
+  if(distort_) {
+    if(distort_->width != width || distort_->height != height) {
+      delete [] distort_->offset;
+      distort_->offset = NULL;
+    }
+  } else {
+    distort_ = new Grid;
+    distort_->offset = NULL;
+  }
+
+  if(!distort_->offset) {
+      distort_->offset = new Offset[width*height];
+      ComputeLookupGrid<InvertIntrinsicsFunction>(distort_,width,height);
+  }
+
+  distort_->width = width;
+  distort_->height = height;
+}
+
+void CameraIntrinsics::CheckUndistortLookupGrid(int width, int height)
+{
+  if(undistort_) {
+    if(undistort_->width != width || undistort_->height != height) {
+      delete [] undistort_->offset;
+      undistort_->offset = NULL;
+    }
+  } else {
+    undistort_ = new Grid;
+    undistort_->offset = NULL;
+  }
+
+  if(!undistort_->offset) {
+      undistort_->offset = new Offset[width*height];
+      ComputeLookupGrid<ApplyIntrinsicsFunction>(undistort_,width,height);
+  }
+
+  undistort_->width = width;
+  undistort_->height = height;
+}
+
 void CameraIntrinsics::Distort(const float* src, float* dst, int width, int height, int channels) {
-  if(!distort_) {
-    distort_ = new Offset[width*height];
-    ComputeLookupGrid<InvertIntrinsicsFunction>(distort_,width,height);
-  }
+  CheckDistortLookupGrid(width, height);
        if(channels==1) Warp<float,1>(distort_,src,dst,width,height);
   else if(channels==2) Warp<float,2>(distort_,src,dst,width,height);
   else if(channels==3) Warp<float,3>(distort_,src,dst,width,height);
@@ -224,10 +278,7 @@
 }
 
 void CameraIntrinsics::Distort(const unsigned char* src, unsigned char* dst, int width, int height, int channels) {
-  if(!distort_) {
-    distort_ = new Offset[width*height];
-    ComputeLookupGrid<InvertIntrinsicsFunction>(distort_,width,height);
-  }
+  CheckDistortLookupGrid(width, height);
        if(channels==1) Warp<unsigned char,1>(distort_,src,dst,width,height);
   else if(channels==2) Warp<unsigned char,2>(distort_,src,dst,width,height);
   else if(channels==3) Warp<unsigned char,3>(distort_,src,dst,width,height);
@@ -236,10 +287,7 @@
 }
 
 void CameraIntrinsics::Undistort(const float* src, float* dst, int width, int height, int channels) {
-  if(!undistort_) {
-    undistort_ = new Offset[width*height];
-    ComputeLookupGrid<ApplyIntrinsicsFunction>(undistort_,width,height);
-  }
+  CheckUndistortLookupGrid(width, height);
        if(channels==1) Warp<float,1>(undistort_,src,dst,width,height);
   else if(channels==2) Warp<float,2>(undistort_,src,dst,width,height);
   else if(channels==3) Warp<float,3>(undistort_,src,dst,width,height);
@@ -248,10 +296,7 @@
 }
 
 void CameraIntrinsics::Undistort(const unsigned char* src, unsigned char* dst, int width, int height, int channels) {
-  if(!undistort_) {
-    undistort_ = new Offset[width*height];
-    ComputeLookupGrid<ApplyIntrinsicsFunction>(undistort_,width,height);
-  }
+  CheckUndistortLookupGrid(width, height);
        if(channels==1) Warp<unsigned char,1>(undistort_,src,dst,width,height);
   else if(channels==2) Warp<unsigned char,2>(undistort_,src,dst,width,height);
   else if(channels==3) Warp<unsigned char,3>(undistort_,src,dst,width,height);

Modified: branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.h
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.h	2011-09-28 13:31:18 UTC (rev 40655)
+++ branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/camera_intrinsics.h	2011-09-28 13:31:32 UTC (rev 40656)
@@ -26,8 +26,6 @@
 
 namespace libmv {
 
-struct Offset;
-
 class CameraIntrinsics {
  public:
   CameraIntrinsics();
@@ -123,7 +121,9 @@
                  int width, int height, int channels);
 
  private:
-  template<typename WarpFunction> void ComputeLookupGrid(Offset* grid, int width, int height);
+  template<typename WarpFunction> void ComputeLookupGrid(struct Grid* grid, int width, int height);
+  void CheckUndistortLookupGrid(int width, int height);
+  void CheckDistortLookupGrid(int width, int height);
   void FreeLookupGrid();
 
   // The traditional intrinsics matrix from x = K[R|t]X.
@@ -140,8 +140,8 @@
   // independent of image size.
   double k1_, k2_, k3_, p1_, p2_;
 
-  Offset* distort_;
-  Offset* undistort_;
+  struct Grid *distort_;
+  struct Grid *undistort_;
 };
 
 }  // namespace libmv

Modified: branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp	2011-09-28 13:31:18 UTC (rev 40655)
+++ branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp	2011-09-28 13:31:32 UTC (rev 40656)
@@ -546,13 +546,14 @@
 /* ************ camera intrinsics ************ */
 
 struct libmv_CameraIntrinsics *libmv_CameraIntrinsicsNew(double focal_length, double principal_x, double principal_y,
-			double k1, double k2, double k3)
+			double k1, double k2, double k3, int width, int height)
 {
 	libmv::CameraIntrinsics *intrinsics= new libmv::CameraIntrinsics();
 
 	intrinsics->SetFocalLength(focal_length, focal_length);
 	intrinsics->SetPrincipalPoint(principal_x, principal_y);
 	intrinsics->SetRadialDistortion(k1, k2, k3);
+	intrinsics->SetImageSize(width, height);
 
 	return (struct libmv_CameraIntrinsics *) intrinsics;
 }
@@ -565,7 +566,7 @@
 }
 
 void libmv_CameraIntrinsicsUpdate(struct libmv_CameraIntrinsics *libmvIntrinsics, double focal_length,
-			double principal_x, double principal_y, double k1, double k2, double k3)
+			double principal_x, double principal_y, double k1, double k2, double k3, int width, int height)
 {
 	libmv::CameraIntrinsics *intrinsics = (libmv::CameraIntrinsics *) libmvIntrinsics;
 
@@ -577,6 +578,9 @@
 
 	if (intrinsics->k1() != k1 || intrinsics->k2() != k2 || intrinsics->k3() != k3)
 		intrinsics->SetRadialDistortion(k1, k2, k3);
+
+	if (intrinsics->image_width() != width || intrinsics->image_height() != height)
+		intrinsics->SetImageSize(width, height);
 }
 
 void libmv_CameraIntrinsicsUndistortByte(struct libmv_CameraIntrinsics *libmvIntrinsics,
@@ -584,9 +588,6 @@
 {
 	libmv::CameraIntrinsics *intrinsics = (libmv::CameraIntrinsics *) libmvIntrinsics;
 
-	if (intrinsics->image_width() != width || intrinsics->image_height() != height)
-		intrinsics->SetImageSize(width, height);
-
 	intrinsics->Undistort(src, dst, width, height, channels);
 }
 
@@ -595,9 +596,6 @@
 {
 	libmv::CameraIntrinsics *intrinsics = (libmv::CameraIntrinsics *) libmvIntrinsics;
 
-	if (intrinsics->image_width() != width || intrinsics->image_height() != height)
-		intrinsics->SetImageSize(width, height);
-
 	intrinsics->Undistort(src, dst, width, height, channels);
 }
 
@@ -605,10 +603,6 @@
 			unsigned char *src, unsigned char *dst, int width, int height, int channels)
 {
 	libmv::CameraIntrinsics *intrinsics = (libmv::CameraIntrinsics *) libmvIntrinsics;
-
-	if (intrinsics->image_width() != width || intrinsics->image_height() != height)
-		intrinsics->SetImageSize(width, height);
-
 	intrinsics->Distort(src, dst, width, height, channels);
 }
 
@@ -617,9 +611,6 @@
 {

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list