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


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43