toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] tag/tag ransac.h ransac_estimators.h


From: Gerhard Reitmayr
Subject: [Toon-members] tag/tag ransac.h ransac_estimators.h
Date: Tue, 12 Jun 2007 20:05:09 +0000

CVSROOT:        /cvsroot/toon
Module name:    tag
Changes by:     Gerhard Reitmayr <gerhard>      07/06/12 20:05:09

Modified files:
        tag            : ransac.h ransac_estimators.h 

Log message:
        added plane estimation

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/tag/tag/ransac.h?cvsroot=toon&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/tag/tag/ransac_estimators.h?cvsroot=toon&r1=1.4&r2=1.5

Patches:
Index: ransac.h
===================================================================
RCS file: /cvsroot/toon/tag/tag/ransac.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- ransac.h    13 Jun 2006 17:19:27 -0000      1.6
+++ ransac.h    12 Jun 2007 20:05:08 -0000      1.7
@@ -89,6 +89,61 @@
     return bestScore;
 }
 
+/// basic MSAC implementation. The function is templated on the observation 
data type
+/// and the transformation data type which must conform to the following 
interface:
+/// @code
+/// class Estimator {
+///     Estimator();
+///     // Estimate from a sequence of observations
+///     template <class It> bool estimate(It begin, It End);
+///     // return the score for the given observation
+///     template <class Obs, class Tol> double score(const Obs& obs) const;
+/// };
+/// @endcode
+/// see the file @ref ransac_estimators.h for some Estimator classes for 
various transformations.
+/// @param[in] observations a vector of observations (usually point matches)
+/// @param[in] sample_size the number of samples used estimate a transformation
+/// @param[in] tolerance the tolerance (passed with each observation) to the 
transformation to check for inliers
+/// @param[in] N the number of hypotheses to test
+/// @param[out] best the transformation hypothesis with the highest inlier 
count
+/// @param[out] inlier a vector of bools that describes the inlier set of the 
winning hypothesis
+/// @return the score of the winning hypothesis
+/// @ingroup ransac
+template <class Obs, class Trans, class Tol> double find_MSAC_inliers(const 
std::vector<Obs>& observations, int sample_size, const Tol& tolerance, size_t N,
+                                                                       Trans& 
best, std::vector<bool>& inlier)
+{
+    std::vector<bool> thisInlier(observations.size());
+    const double toleranceSquared = tolerance * tolerance;
+    double bestScore = observations.size() * toleranceSquared;
+
+    std::vector<size_t> sample_index(sample_size);
+    std::vector<Obs> sample(sample_size);
+    while (N--) {
+       randomTuple(sample_index, observations.size());
+       for (int i=0;i<sample_size; i++)
+           sample[i] = observations[sample_index[i]];
+       Trans thisT(best);
+       thisT.estimate(sample.begin(), sample.end());
+       double score = 0;
+       for (size_t i=0; i<observations.size(); i++) {
+           const Obs& o = observations[i];
+           const double s = thisT.score(o);
+           if (s < toleranceSquared) {
+               thisInlier[i] = true;
+               score += s;
+           } else {
+               thisInlier[i] = false;
+               score += toleranceSquared;
+           }
+       }
+       if (score < bestScore) {
+           bestScore = score;
+           inlier = thisInlier;
+           best = thisT;
+       }
+    }
+    return bestScore;
+}
 
  inline double getShrinkRatio(unsigned int H, unsigned int N, unsigned int B)
  {

Index: ransac_estimators.h
===================================================================
RCS file: /cvsroot/toon/tag/tag/ransac_estimators.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- ransac_estimators.h 13 Jun 2006 02:37:32 -0000      1.4
+++ ransac_estimators.h 12 Jun 2007 20:05:08 -0000      1.5
@@ -2,6 +2,7 @@
 #define TAG_RANSAC_ESTIMATORS_H
 
 #include <vector>
+#include <algorithm>
 #include <cassert>
 
 #include <TooN/TooN.h>
@@ -137,7 +138,8 @@
            E[2] = e2.template slice<2,3>();
            
            TooN::SVD<3> svdE(E);
-           E = svdE.get_U()*TooN::diagmult(makeVector(1,1,0),svdE.get_VT());
+           const TooN::Vector<3> temp = (TooN::make_Vector, 1, 1, 0);
+           E = svdE.get_U()*TooN::diagmult(temp,svdE.get_VT());
            return true;            
        }
        
@@ -228,15 +230,58 @@
        t[1] = Aty[2];
     }
 
-    template <class M> inline bool isInlier(const M& m, double r) const {
+    template <class M> inline double score(const M& m) const {
        const TooN::Vector<2>& a = first_point(m);
        const TooN::Vector<2>& b = second_point(m);
        const TooN::Vector<2> disp = A*a + t - b;
-       return (disp*disp) <= r*r * noise(m);
+       return (disp*disp);
+    }
+
+    template <class M> inline bool isInlier(const M& m, double r) const {
+       return this->score(m) <= r*r * noise(m);
     }
 };
 
 
+struct PlaneFromPoints {
+    /// the plane equation coefficients as homogeneous vector with unit 
normal, or (0,0,0,1)
+    TooN::Vector<4> plane;
+
+    PlaneFromPoints() : plane(TooN::zeros<4>()) {}
+
+    template <class It> void estimate(It begin, It end){
+       assert(std::distance(begin, end) >= 3);
+       if( std::distance(begin, end) == 3 ){  // fast special case
+            const TooN::Vector<3> d1 = *(begin+1) - *begin;
+            const TooN::Vector<3> d2 = *(begin+2) - *begin;
+            plane.template slice<0,3>() = d1 ^ d2;
+            TooN::normalize(plane.template slice<0,3>());
+            plane[3] = -(*begin) * plane.template slice<0,3>();
+       } else {
+            TooN::Matrix<> design(std::distance(begin, end), 4);
+            for(It p = begin; p != end; ++p)
+               design[p-begin] = TooN::unproject(*p);
+             TooN::SVD<> s(design);
+            plane = s.get_VT()[3];
+             const double d = sqrt(plane.template slice<0,3>() * 
plane.template slice<0,3>());
+            if(d > 1e-10){
+               plane /= d;
+            } else {
+               plane = (TooN::make_Vector, 0, 0, 0, 1);
+            }
+       }
+    }
+
+    template <class M> inline double score(const M & m) const {
+       const double d = plane * TooN::unproject(m);
+       return d*d;
+    }
+
+    template <class M> inline bool isInlier(const M& m, double r) const {
+       return this->score(m) <= r*r * noise(m);
+    }
+};
+
 } // namespace tag
 
 #endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]