Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "NNRMatcher.h"
00012
00013 #include "Architecture/Singleton/Clock.h"
00014
00015 #include "Workers/Math/vec2.h"
00016
00017 #include <assert.h>
00018 #include <map>
00019 #include <list>
00020 #include <math.h>
00021
00022 using namespace std;
00023
00024 #define THIS NNRMatcher
00025
00026 THIS::THIS ( std::vector< KeyPoint >* keyPointsA, std::vector< KeyPoint >* keyPointsB )
00027 {
00028 m_KeyPointsA = keyPointsA;
00029 m_KeyPointsB = keyPointsB;
00030 m_Log << "NNRMatcher created\n";
00031 m_Log << "Number of keypoints (scenePoints/objectImagePoints): " << m_KeyPointsA->size() << " / " << m_KeyPointsB->size() << endl;
00032 }
00033
00034 THIS::~THIS()
00035 {
00036 }
00037
00038 void THIS::match ( float maxDistRatio )
00039 {
00040 if ( ( m_KeyPointsA->size() ==0 ) || ( m_KeyPointsB->size() ==0 ) || ( m_Matches.size() !=0 ) ) { return; }
00041
00042 int startTime = Clock::getInstance()->getTimestamp();
00043
00044 int size1=m_KeyPointsA->size();
00045 int size2=m_KeyPointsB->size();
00046
00047 float maxDistRatioSquared=maxDistRatio*maxDistRatio;
00048
00049 vector< unsigned > indPos;
00050 vector< unsigned > indNeg;
00051 indPos.reserve( m_KeyPointsB->size() );
00052 indNeg.reserve( m_KeyPointsB->size() );
00053
00054
00055 for ( int index2=0; index2 < size2; index2++ )
00056 {
00057 if ( (*m_KeyPointsB)[index2].sign > 0 )
00058 {
00059 indPos.push_back( index2 );
00060 }
00061 else
00062 {
00063 indNeg.push_back( index2 );
00064 }
00065 }
00066
00067 for ( int index1=0; index1 < size1; index1++ )
00068 {
00069
00070 int minIndexB=-1;
00071
00072 double minDist=1e10;
00073
00074 double secondMinDist=1e10;
00075
00076
00077 vector< unsigned >& indices2 = (*m_KeyPointsA)[index1].sign > 0 ? indPos : indNeg;
00078
00079 for ( unsigned k=0; k < indices2.size(); k++ )
00080 {
00081 int index2 = indices2[ k ];
00082 double distance = (*m_KeyPointsA)[index1].squaredDistance ( (*m_KeyPointsB)[index2] );
00083
00084 if ( distance < minDist )
00085 {
00086 secondMinDist=minDist;
00087 minDist=distance;
00088 minIndexB=index2;
00089 }
00090 else
00091 if ( distance < secondMinDist )
00092 {
00093 secondMinDist=distance;
00094 }
00095 }
00096 if ( ( minIndexB != -1 ) && ( minDist/secondMinDist < maxDistRatioSquared ) )
00097 {
00098 KeyPointMatch match={ index1, minIndexB, minDist, 0, 0 };
00099 m_Matches.push_back ( match );
00100 m_Log << index1 << "->" << minIndexB << " (d" << minDist << "/r" << minDist/secondMinDist << ") ";
00101 }
00102
00103 }
00104 m_Log << "\n--- " << m_Matches.size() << " keypoints matched in first phase in " << ( Clock::getInstance()->getTimestamp() - startTime ) << "ms\n";
00105
00106 eliminateMultipleMatches();
00107
00108 }
00109
00110
00111 void THIS::eliminateMultipleMatches()
00112 {
00113
00114
00115
00116 int startTime = Clock::getInstance()->getTimestamp();
00117
00118
00119
00120
00121 map< unsigned, MatchElem > bestMatch;
00122
00123 m_Log << "deleting ";
00124
00125 MatchElem currentMatch=m_Matches.begin();
00126 while ( currentMatch != m_Matches.end() )
00127 {
00128 unsigned index2=currentMatch->index2;
00129
00130
00131 map< unsigned, MatchElem >::iterator previous=bestMatch.find ( index2 );
00132
00133
00134 if ( previous == bestMatch.end() )
00135 {
00136 bestMatch[ index2 ] = currentMatch;
00137 currentMatch++;
00138 continue;
00139 }
00140
00141 MatchElem previousMatch = previous->second;
00142
00143
00144 if ( currentMatch->distance < previousMatch->distance )
00145 {
00146 m_Log << previousMatch->index1 << "->" << previousMatch->index2;
00147 m_Log << " (better:" << currentMatch->index1 << "->" << currentMatch->index2 << ") ";
00148 m_Matches.erase ( previousMatch );
00149 bestMatch[ index2 ] = currentMatch;
00150 currentMatch++;
00151 continue;
00152 }
00153
00154
00155 m_Log << currentMatch->index1 << "->" << currentMatch->index2;
00156 m_Log << " (better:" << previousMatch->index1 << "->" << previousMatch->index2 << ") ";
00157 currentMatch=m_Matches.erase ( currentMatch );
00158 }
00159 m_Log << "\n--- " << m_Matches.size() << " remaining after multiple match elimination in " << ( Clock::getInstance()->getTimestamp() - startTime ) << "ms\n";
00160
00161 }
00162
00163 string THIS::getLog()
00164 {
00165 return m_Log.str();
00166 }
00167
00168 #undef THIS