featureAssociation.h

Go to the documentation of this file.
00001 
00016 #ifndef DLR_COMPUTERVISION_FEATUREASSOCIATION_H
00017 #define DLR_COMPUTERVISION_FEATUREASSOCIATION_H
00018 
00019 #include <dlrNumeric/array2D.h>
00020 
00021 namespace dlr {
00022 
00023   namespace computerVision {
00024 
00079     template<class Iterator0, class Iterator1, class Functor>
00080     std::vector< std::pair<size_t, size_t> >
00081     associateFeaturesScott91(Iterator0 sequence0Begin, Iterator0 sequence0End,
00082                              Iterator1 sequence1Begin, Iterator1 sequence1End,
00083                              Functor similarityFunctor);
00084     
00085   } // namespace computerVision
00086     
00087 } // namespace dlr
00088 
00089 
00090 /* ============ Definitions of inline & template functions ============ */
00091 
00092 
00093 #include <cmath>
00094 #include <dlrLinearAlgebra/linearAlgebra.h>
00095 #include <dlrNumeric/maxRecorder.h>
00096 #include <dlrNumeric/utilities.h>
00097 
00098 
00099 namespace dlr {
00100 
00101   namespace computerVision {
00102 
00103 
00104     // This function template implements the feature association
00105     // algorithm of Guy Scott and H. Christopher Longuet-Higgins.
00106     template<class Iterator0, class Iterator1, class Functor>
00107     std::vector< std::pair<size_t, size_t> >
00108     associateFeaturesScott91(Iterator0 sequence0Begin, Iterator0 sequence0End,
00109                              Iterator1 sequence1Begin, Iterator1 sequence1End,
00110                              Functor similarityFunctor)
00111     {
00112       // Count the number of features in each sequence.  Even if this
00113       // operation is O(n), it will be dominated by the more expensive
00114       // steps below.
00115       size_t sequence0Length = sequence0End - sequence0Begin;
00116       size_t sequence1Length = sequence1End - sequence1Begin;
00117 
00118       // Compute the similarity matrix.
00119       dlr::numeric::Array2D<double> GMatrix(sequence0Length, sequence1Length);
00120       Iterator0 begin0 = sequence0Begin;
00121       for(size_t rr = 0; rr < sequence0Length; ++rr) {
00122         Iterator1 begin1 = sequence1Begin;
00123         for(size_t cc = 0; cc < sequence1Length; ++cc) {
00124           GMatrix(rr, cc) = similarityFunctor(*begin0, *begin1);
00125           ++begin1;
00126         }
00127         ++begin0;
00128       }
00129 
00130       // Compute a new similarity matrix, comprising only rotations
00131       // and reflections, that is "similar" to G (see the paper for
00132       // more details).
00133       dlr::numeric::Array2D<double> uMatrix;
00134       dlr::numeric::Array1D<double> sigmaArray;
00135       dlr::numeric::Array2D<double> vTransposeMatrix;
00136       dlr::linearAlgebra::singularValueDecomposition(
00137         GMatrix, uMatrix, sigmaArray, vTransposeMatrix);
00138       dlr::numeric::Array2D<double> PMatrix =
00139         dlr::numeric::matrixMultiply(uMatrix, vTransposeMatrix);
00140 
00141       // Find the max elements for each row and column of P.
00142       std::vector< dlr::numeric::MaxRecorder<double, size_t> > rowMaxes(
00143         sequence0Length);
00144       std::vector< dlr::numeric::MaxRecorder<double, size_t> > columnMaxes(
00145         sequence1Length);
00146       for(size_t rr = 0; rr < sequence0Length; ++rr) {
00147         for(size_t cc = 0; cc < sequence1Length; ++cc) {
00148           double similarity = PMatrix(rr, cc);
00149           rowMaxes[rr].test(similarity, cc);
00150           columnMaxes[cc].test(similarity, rr);
00151         }
00152       }
00153 
00154       // Valid correspondences are those for which the similarity is
00155       // max of both row and column.
00156       std::vector< std::pair<size_t, size_t> > result;
00157       for(size_t rr = 0; rr < sequence0Length; ++rr) {
00158         size_t bestColumn = rowMaxes[rr].getPayload();
00159         if(columnMaxes[bestColumn].getPayload() == rr) {
00160           result.push_back(std::make_pair(rr, bestColumn));
00161         }
00162       }
00163 
00164       return result;
00165     }
00166 
00167   } // namespace computerVision
00168     
00169 } // namespace dlr
00170 
00171 #endif /* #ifndef DLR_COMPUTERVISION_FEATUREASSOCIATION_H */

Generated on Wed Nov 25 12:15:04 2009 for dlrComputerVision Utility Library by  doxygen 1.5.8