00001 #ifndef GFM_DESCRIPTORS_BASED_MATCHER_HPP
00002 #define GFM_DESCRIPTORS_BASED_MATCHER_HPP
00003 #include "gfm/Matcher.hpp"
00004 #include "gfm/MatchingResult.hpp"
00005 #include "fdetect/InterestFeature.hpp"
00006 #include "fdetect/DetectionResult.hpp"
00007 #include "jmath/jann.hpp"
00008 #include "jmath/ublasExtra.hpp"
00009 #include <opencv2/core/core.hpp>
00010
00011 namespace jafar {
00012 namespace gfm_v2 {
00017 template<typename InterestFeatureT>
00018 class DescriptorsBasedMatcher : public gfm_v2::Matcher<InterestFeatureT> {
00019 public:
00020 typedef typename InterestFeatureT::DescriptorType::RealType RealType;
00021 typedef DescriptorsBasedMatcher<InterestFeatureT> MatcherType;
00022 typedef fdetect_v2::DetectionResult<InterestFeatureT> DetectionResult;
00023 typedef gfm_v2::MatchingResult<InterestFeatureT> MatchingResult;
00024 typedef gfm_v2::MatchSourceInfo<InterestFeatureT> MatchSourceInfo;
00025 private:
00026 class DescriptorsMatchSourceInfo : public MatchSourceInfo {
00027 public:
00028 DescriptorsMatchSourceInfo(const fdetect_v2::DetectionResult<InterestFeatureT>& dr) :
00029 MatchSourceInfo(dr) {
00030 JFR_ASSERT(dr.size() > 0, "detection result is empty")
00031 size_t descriptor_size = dr[0]->descriptor().size();
00032 JFR_ASSERT(descriptor_size > 0, "descriptors are empty")
00033 m_descriptors.resize(dr.size(), descriptor_size);
00034 size_t i, j;
00035 for(i = 0; i < dr.size(); i++)
00036 for(j = 0; j < descriptor_size; j++)
00037 m_descriptors(i,j) = dr[i]->descriptor().m_descr[j];
00038 }
00039 ~DescriptorsMatchSourceInfo() {}
00040 virtual void reinit() { m_descriptors.clear(); }
00041 size_t size() const { return this->points().size(); }
00042 const InterestFeatureT* operator[](int nindex) const {
00043 return this->points()[index];
00044 }
00045 ublas::matrix< RealType > m_descriptors;
00046 };
00048 struct similar_feature{
00050 const InterestFeatureT* feature;
00052 RealType distance;
00053 similar_feature(const InterestFeatureT* _feature,
00054 RealType _distance) :
00055 feature(_feature), distance(_distance) {}
00056 similar_feature(): feature(0), distance(0) {}
00057 };
00059 struct matched_feature{
00061 const InterestFeatureT* observed;
00063 std::vector<similar_feature> candidates;
00064 matched_feature(): observed(0), candidates(0) {}
00065 };
00066
00067 public:
00068 virtual MatchSourceInfo* initMatchSourceInfo(fdetect_v2::DetectionResult<InterestFeatureT>& pointsource )
00069 {
00070 return new MatcherType::DescriptorsMatchSourceInfo(pointsource);
00071 }
00072
00073 virtual gfm_v2::MatchingResult<InterestFeatureT> computeMatch(
00074 gfm_v2::MatchSourceInfo<InterestFeatureT>* refsourceinfo,
00075 gfm_v2::MatchSourceInfo<InterestFeatureT>* matchsourceinfo);
00076
00086 DescriptorsBasedMatcher(int knn = 2,
00087 int checks = 20,
00088 RealType threshold = 0.8,
00089 bool enforce = false,
00090 RealType increment = 1.5,
00091 int bins = 20) :
00092 m_knn(knn), m_checks(checks), m_uniquess_threshold(threshold),
00093 m_enforce(enforce), m_increment(increment), m_bins(bins) {}
00094
00096 static void vote_size_orientation(gfm_v2::MatchingResult<InterestFeatureT> &mr,
00097 RealType scale_increment = 1.5,
00098 int rotation_bins = 20);
00100 RealType distance(unsigned int matched_index)
00101 {
00102 typename std::map<unsigned int,RealType>::iterator dist_it;
00103 dist_it = m_distances.find(matched_index);
00104 JFR_ASSERT(dist_it != m_distances.end(),
00105 "no matched feature with index "<<matched_index<<" found");
00106 return dist_it->second;
00107 }
00108
00109 private:
00110 int m_knn;
00111 int m_checks;
00112 RealType m_uniquess_threshold;
00113 bool m_enforce;
00114 RealType m_increment;
00115 int m_bins;
00116 std::map<unsigned int,RealType> m_distances;
00118 void brute_force( gfm_v2::MatchSourceInfo<InterestFeatureT>* refsourceinfo,
00119 gfm_v2::MatchSourceInfo<InterestFeatureT>* matchsourceinfo,
00120 std::vector<matched_feature>& matches,
00121 int knn,
00122 int checks);
00124 std::vector<matched_feature>
00125 vote_oneness(std::vector<matched_feature>& matches, RealType threshold);
00126 };
00127
00128
00129
00130
00131
00132 }
00133 }
00134 #include "gfm/DescriptorsBasedMatcher.hxx"
00135 #endif