CvHomography.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  CvHomography.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *******************************************************************************/
00007 
00008 #include "CvHomography.h"
00009 
00010 #include "Architecture/Config/Config.h"
00011 
00012 #include <math.h>
00013 #include <cv.h>
00014 
00015 #define THIS CvHomography
00016 
00017 using namespace std;
00018 
00019 THIS::THIS ( vector< KeyPoint >* keyPoints1, vector< KeyPoint >* keyPoints2, std::list< KeyPointMatch >& matches )
00020 {
00021   m_KeyPoints1 = keyPoints1;
00022   m_KeyPoints2 = keyPoints2;
00023   m_Matches = matches;
00024   m_Success = false;
00025   m_MaxReprojectionError = Config::getFloat( "ObjectRecognition.Ransac.fMaxReprojectionError" );
00026 }
00027 
00028 THIS::~THIS()
00029 {
00030 }
00031 
00032 bool THIS::computeHomography()
00033 {
00034   double homMat[9];
00035   CvMat homMatCv;
00036 
00037   memset ( homMat, 0, 9*sizeof ( double ) );
00038   homMatCv = cvMat ( 3, 3, CV_64F, homMat );
00039 
00040   std::vector<CvPoint2D32f> points1Cv, points2Cv;
00041   CvMat points1CvMat, points2CvMat;
00042 
00043   int numMatches = m_Matches.size();
00044 
00045   if ( numMatches < 4 )
00046   {
00047     return false;
00048   }
00049 
00050   // Set vectors to correct size
00051   points1Cv.resize ( numMatches );
00052   points2Cv.resize ( numMatches );
00053 
00054   // Copy Ipoints from match vector into cvPoint vectors
00055   std::list<KeyPointMatch>::iterator currentMatch = m_Matches.begin();
00056   int i = 0;
00057   while ( currentMatch != m_Matches.end() )
00058   {
00059     points1Cv[i] = cvPoint2D32f ( ( *m_KeyPoints1 ) [ currentMatch->index1 ].x,
00060                                   ( *m_KeyPoints1 ) [ currentMatch->index1 ].y );
00061     points2Cv[i] = cvPoint2D32f ( ( *m_KeyPoints2 ) [ currentMatch->index2 ].x,
00062                                   ( *m_KeyPoints2 ) [ currentMatch->index2 ].y );
00063     currentMatch++;
00064     i++;
00065   }
00066 
00067   points1CvMat = cvMat ( 1, numMatches, CV_32FC2, &points1Cv[0] );
00068   points2CvMat = cvMat ( 1, numMatches, CV_32FC2, &points2Cv[0] );
00069 
00070   // Find the homography (transformation) between the two sets of points
00071 
00072   //0 - regular method using all the point pairs
00073   //CV_RANSAC - RANSAC-based robust method
00074   //CV_LMEDS - Least-Median robust method
00075 
00076   int method = 0;
00077   switch (Config::getInstance()->getInt( "ObjectRecognition.Homography.iMethod" ))
00078   {
00079     case 0 :
00080       method = 0;
00081       break;
00082     case 1 :
00083       method = CV_RANSAC;
00084       break;
00085     case 2 :
00086       method = CV_LMEDS;
00087       break;
00088     default:
00089       ROS_ERROR_STREAM("Undefined methode to find homography");
00090       break;
00091   }
00092 
00093   m_Success = cvFindHomography( &points2CvMat, &points1CvMat, &homMatCv, method, m_MaxReprojectionError );
00094 
00095 //   float n=sqrt(homMat[0]*homMat[0]+homMat[3]*homMat[3]) * sqrt(homMat[1]*homMat[1]+homMat[4]*homMat[4]);
00096 //
00097 //   float det = homMat[0]*homMat[4] - homMat[1]*homMat[3];
00098 //   det /= n;
00099 //   float l = fabs( det );
00100 //
00101 //   if ( l < 0.8 )
00102 //   {
00103 //     m_Success = false;
00104 //   }
00105 //
00106 //   TRACE_INFO( "det: " << det );
00107 //
00108 //   /*
00109 //   float scalar= homMat[0]*homMat[1] + homMat[3]*homMat[4];
00110 //   scalar /= n;
00111 //   */
00112 
00113   m_Homography = Homography( homMat );
00114 
00115   return m_Success;
00116 }
00117 
00118 void THIS::eliminateBadMatches()
00119 {
00120   vector<Point2D> points2;
00121   vector<Point2D> projPoints;
00122 
00123   points2.reserve( m_Matches.size() );
00124 
00125   std::list<KeyPointMatch>::iterator currentMatch = m_Matches.begin();
00126   while ( currentMatch != m_Matches.end() )
00127   {
00128     Point2D pos2 = ( *m_KeyPoints2 ) [ currentMatch->index2 ].position();
00129     points2.push_back( pos2 );
00130     currentMatch++;
00131   }
00132 
00133   m_Homography.transform( points2, projPoints );
00134 
00135   currentMatch = m_Matches.begin();
00136   int i = 0;
00137   while ( currentMatch != m_Matches.end() )
00138   {
00139     Point2D pos1 = ( *m_KeyPoints1 ) [ currentMatch->index1 ].position();
00140     float scale = ( *m_KeyPoints1 ) [ currentMatch->index1 ].scale;
00141     if ( pos1.distance( projPoints[i] ) > m_MaxReprojectionError*scale )
00142     {
00143       currentMatch = m_Matches.erase( currentMatch );
00144     }
00145     else
00146     {
00147       currentMatch++;
00148     }
00149     i++;
00150   }
00151 }
00152 
00153 #undef THIS


or_libs
Author(s): raphael
autogenerated on Mon Oct 6 2014 02:53:18