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

Sergey Sharybin g.ulairi at gmail.com
Thu Oct 13 19:54:05 CEST 2011


Revision: 40991
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=40991
Author:   nazgul
Date:     2011-10-13 17:54:05 +0000 (Thu, 13 Oct 2011)
Log Message:
-----------
Camera tracking integration
===========================

Added option to choose which method to use for feature detector: FAST or Moravec

Moravec was supposed to be more "stable", but it collected worse feedback.
before returning back to FAST detector added option to easier toggle between
different detector to compare it's result and find out if we can leave only FAST.

Modified Paths:
--------------
    branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.cc
    branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.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_tracking.h
    branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c
    branches/soc-2011-tomato/source/blender/editors/space_clip/tracking_ops.c

Added Paths:
-----------
    branches/soc-2011-tomato/extern/libmv/patches/detect.patch

Modified: branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.cc
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.cc	2011-10-13 17:44:59 UTC (rev 40990)
+++ branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.cc	2011-10-13 17:54:05 UTC (rev 40991)
@@ -23,15 +23,59 @@
 ****************************************************************************/
 
 #include "libmv/simple_pipeline/detect.h"
+#include <third_party/fast/fast.h>
 #include <stdlib.h>
-#include <string.h>
+#include <memory.h>
 
+#ifdef __SSE2__
+#include <emmintrin.h>
+#endif
+
 namespace libmv {
 
 typedef unsigned int uint;
 
+std::vector<Feature> DetectFAST(const unsigned char* data, int width, int height, int stride,
+                           int min_trackness, int min_distance) {
+  std::vector<Feature> features;
+  // TODO(MatthiasF): Support targetting a feature count (binary search trackness)
+  int num_features;
+  xy* all = fast9_detect(data, width, height,
+                         stride, min_trackness, &num_features);
+  if(num_features == 0) {
+    free(all);
+    return features;
+  }
+  int* scores = fast9_score(data, stride, all, num_features, min_trackness);
+  // TODO: merge with close feature suppression
+  xy* nonmax = nonmax_suppression(all, scores, num_features, &num_features);
+  free(all);
+  // Remove too close features
+  // TODO(MatthiasF): A resolution independent parameter would be better than distance
+  // e.g. a coefficient going from 0 (no minimal distance) to 1 (optimal circle packing)
+  // FIXME(MatthiasF): this method will not necessarily give all maximum markers
+  if(num_features) features.reserve(num_features);
+  for(int i = 0; i < num_features; ++i) {
+    xy xy = nonmax[i];
+    Feature a = { xy.x, xy.y, scores[i], 7 };
+    // compare each feature against filtered set
+    for(int j = 0; j < features.size(); j++) {
+      Feature& b = features[j];
+      if ( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) < min_distance*min_distance ) {
+        // already a nearby feature
+        goto skip;
+      }
+    }
+    // otherwise add the new feature
+    features.push_back(a);
+    skip: ;
+  }
+  free(scores);
+  free(nonmax);
+  return features;
+}
+
 #ifdef __SSE2__
-#include <emmintrin.h>
 static uint SAD(const ubyte* imageA, const ubyte* imageB, int strideA, int strideB) {
   __m128i a = _mm_setzero_si128();
   for(int i = 0; i < 16; i++) {
@@ -52,7 +96,7 @@
 }
 #endif
 
-void Detect(ubyte* image, int stride, int width, int height, Feature* detected, int* count, int distance, ubyte* pattern) {
+void DetectMORAVEC(ubyte* image, int stride, int width, int height, Feature* detected, int* count, int distance, ubyte* pattern) {
   unsigned short histogram[256];
   memset(histogram,0,sizeof(histogram));
   ubyte* scores = new ubyte[width*height];

Modified: branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.h
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.h	2011-10-13 17:44:59 UTC (rev 40990)
+++ branches/soc-2011-tomato/extern/libmv/libmv/simple_pipeline/detect.h	2011-10-13 17:54:05 UTC (rev 40991)
@@ -25,30 +25,55 @@
 #ifndef LIBMV_SIMPLE_PIPELINE_DETECT_H_
 #define LIBMV_SIMPLE_PIPELINE_DETECT_H_
 
-#ifdef __cplusplus
+#include <vector>
+
 namespace libmv {
-#endif
 
 typedef unsigned char ubyte;
 
 /*!
-    \a Feature is the 2D location of a detected feature in an image.
+    A Feature is the 2D location of a detected feature in an image.
 
-    \a x, \a y is the position of the center in pixels (from image top-left).
-    \a score is an estimate of how well the pattern will be tracked.
-    \a size can be used as an initial size to track the pattern.
+    \a x, \a y is the position of the feature in pixels from the top left corner.
+    \a score is an estimate of how well the feature will be tracked.
+    \a size can be used as an initial pattern size to track the feature.
 
     \sa Detect
 */
 struct Feature {
+  /// Position in pixels (from top-left corner)
+  /// \note libmv might eventually support subpixel precision.
   float x, y;
+  /// Trackness of the feature
   float score;
+  /// Size of the feature in pixels
   float size;
 };
- //radius for non maximal suppression
+
 /*!
     Detect features in an image.
 
+    You need to input a single channel 8-bit image using pointer to image \a data,
+    \a width, \a height and \a stride (i.e bytes per line).
+
+    You can tweak the count of detected features using \a min_trackness, which is
+    the minimum score to add a feature, and \a min_distance which is the minimal
+    distance accepted between two featuress.
+
+    \note You can binary search over \a min_trackness to get a given feature count.
+
+    \note a way to get an uniform distribution of a given feature count is:
+          \a min_distance = \a width * \a height / desired_feature_count ^ 2
+
+    \return All detected feartures matching given parameters
+*/
+std::vector<Feature> DetectFAST(const unsigned char* data, int width, int height,
+                           int stride, int min_trackness = 128,
+                           int min_distance = 120);
+
+/*!
+    Detect features in an image.
+
     \a image is a single channel 8-bit image of size \a width x \a height
 
     \a detected is an array with space to hold \a *count features.
@@ -63,10 +88,8 @@
     \note \a You can crop the image (to avoid detecting markers near the borders) without copying:
              image += marginY*stride+marginX, width -= 2*marginX, height -= 2*marginY;
 */
-void Detect(ubyte* image, int stride, int width, int height, Feature* detected, int* count, int distance /*=32*/, ubyte* pattern /*=0*/);
+void DetectMORAVEC(ubyte* image, int stride, int width, int height, Feature* detected, int* count, int distance /*=32*/, ubyte* pattern /*=0*/);
 
-#ifdef __cplusplus
 }
-#endif
 
 #endif

Modified: branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp	2011-10-13 17:44:59 UTC (rev 40990)
+++ branches/soc-2011-tomato/extern/libmv/libmv-capi.cpp	2011-10-13 17:54:05 UTC (rev 40991)
@@ -502,11 +502,13 @@
 
 /* ************ feature detector ************ */
 
-struct libmv_Features *libmv_detectFeatures(unsigned char *data, int width, int height, int stride,
-			int margin, int count, int min_distance)
+struct libmv_Features *libmv_detectFeaturesFAST(unsigned char *data, int width, int height, int stride,
+			int margin, int min_trackness, int min_distance)
 {
-	libmv::Feature *features = new libmv::Feature[count];
-	libmv_Features *libmv_features = new libmv_Features;
+	libmv::Feature *features = NULL;
+	std::vector<libmv::Feature> v;
+	libmv_Features *libmv_features = new libmv_Features();
+	int i= 0, count;
 
 	if(margin) {
 		data += margin*stride+margin;
@@ -514,15 +516,49 @@
 		height -= 2*margin;
 	}
 
-	libmv::Detect(data, stride, width, height, features, &count, min_distance, NULL);
+	v = libmv::DetectFAST(data, width, height, stride, min_trackness, min_distance);
 
-	libmv_features->count= count;
-	libmv_features->margin= margin;
-	libmv_features->features= features;
+	count = v.size();
 
-	return libmv_features ;
+	if(count) {
+		features= new libmv::Feature[count];
+
+		for(std::vector<libmv::Feature>::iterator it = v.begin(); it != v.end(); it++) {
+			features[i++]= *it;
+		}
+	}
+
+	libmv_features->features = features;
+	libmv_features->count = count;
+	libmv_features->margin = margin;
+
+	return (libmv_Features *)libmv_features;
 }
 
+struct libmv_Features *libmv_detectFeaturesMORAVEC(unsigned char *data, int width, int height, int stride,
+			int margin, int count, int min_distance)
+{
+	libmv::Feature *features = NULL;
+	libmv_Features *libmv_features = new libmv_Features;
+
+	if(count) {
+		if(margin) {
+			data += margin*stride+margin;
+			width -= 2*margin;
+			height -= 2*margin;
+		}
+
+		features = new libmv::Feature[count];
+		libmv::DetectMORAVEC(data, stride, width, height, features, &count, min_distance, NULL);
+	}
+
+	libmv_features->count = count;
+	libmv_features->margin = margin;
+	libmv_features->features = features;
+
+	return libmv_features;
+}
+
 int libmv_countFeatures(struct libmv_Features *libmv_features)
 {
 	return libmv_features->count;
@@ -530,7 +566,7 @@
 
 void libmv_getFeature(struct libmv_Features *libmv_features, int number, double *x, double *y, double *score, double *size)
 {
-	libmv::Feature feature = libmv_features->features[number];
+	libmv::Feature feature= libmv_features->features[number];
 
 	*x = feature.x + libmv_features->margin;
 	*y = feature.y + libmv_features->margin;
@@ -540,7 +576,10 @@
 
 void libmv_destroyFeatures(struct libmv_Features *libmv_features)
 {
-	delete (libmv::Feature *)libmv_features;
+	if(libmv_features->features)
+		delete [] libmv_features->features;
+
+	delete libmv_features;
 }
 
 /* ************ camera intrinsics ************ */

Modified: branches/soc-2011-tomato/extern/libmv/libmv-capi.h
===================================================================
--- branches/soc-2011-tomato/extern/libmv/libmv-capi.h	2011-10-13 17:44:59 UTC (rev 40990)
+++ branches/soc-2011-tomato/extern/libmv/libmv-capi.h	2011-10-13 17:54:05 UTC (rev 40991)
@@ -72,7 +72,9 @@
 void libmv_destroyReconstruction(struct libmv_Reconstruction *libmv_reconstruction);
 
 /* feature detector */
-struct libmv_Features *libmv_detectFeatures(unsigned char *data, int width, int height, int stride,
+struct libmv_Features *libmv_detectFeaturesFAST(unsigned char *data, int width, int height, int stride,
+			int margin, int min_trackness, int min_distance);
+struct libmv_Features *libmv_detectFeaturesMORAVEC(unsigned char *data, int width, int height, int stride,
 			int margin, int count, int min_distance);
 int libmv_countFeatures(struct libmv_Features *libmv_features);
 void libmv_getFeature(struct libmv_Features *libmv_features, int number, double *x, double *y, double *score, double *size);

Added: branches/soc-2011-tomato/extern/libmv/patches/detect.patch
===================================================================
--- branches/soc-2011-tomato/extern/libmv/patches/detect.patch	                        (rev 0)
+++ branches/soc-2011-tomato/extern/libmv/patches/detect.patch	2011-10-13 17:54:05 UTC (rev 40991)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list