CvFundamentalMat.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  CvFundamentalMat.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *******************************************************************************/
00007 
00008 #include "CvFundamentalMat.h"
00009 
00010 //#include "Architecture/Config/Config.h" // TODO
00011 
00012 #include <math.h>
00013 
00014 #define THIS CvFundamentalMat
00015 
00016 THIS::THIS ( std::vector< KeyPoint >* keyPoints1, std::vector< KeyPoint >* keyPoints2, std::list< KeyPointMatch >& matches )
00017 {
00018   m_KeyPoints1 = keyPoints1;
00019   m_KeyPoints2 = keyPoints2;
00020   m_Matches = matches;
00021   m_Success = false;
00022   m_MaxReprojectionError = 1; // TODO Config::getFloat( "ObjectRecognition.Ransac.fMaxReprojectionError" );
00023 }
00024 
00025 THIS::~THIS()
00026 {
00027 }
00028 
00029 bool THIS::computeFundamentalMat()
00030 {
00031   double fundamentalMat[9];
00032 
00033   memset ( fundamentalMat, 0, 9*sizeof ( double ) );
00034   m_FundMatCv = cvMat ( 3, 3, CV_64F, fundamentalMat );
00035 
00036   std::vector<CvPoint2D32f> points1Cv, points2Cv;
00037 
00038 
00039   int numMatches = m_Matches.size();
00040 
00041   if ( numMatches < 4 )
00042   {
00043     return false;
00044   }
00045 
00046   // Set vectors to correct size
00047   points1Cv.resize ( numMatches );
00048   points2Cv.resize ( numMatches );
00049 
00050   // Copy Ipoints from match vector into cvPoint vectors
00051   std::list<KeyPointMatch>::iterator currentMatch = m_Matches.begin();
00052   int i = 0;
00053   while ( currentMatch != m_Matches.end() )
00054   {
00055     points1Cv[i] = cvPoint2D32f ( ( *m_KeyPoints1 ) [ currentMatch->index1 ].x,
00056                                   ( *m_KeyPoints1 ) [ currentMatch->index1 ].y );
00057     points2Cv[i] = cvPoint2D32f ( ( *m_KeyPoints2 ) [ currentMatch->index2 ].x,
00058                                   ( *m_KeyPoints2 ) [ currentMatch->index2 ].y );
00059     currentMatch++;
00060     i++;
00061   }
00062 
00063   m_Points1CvMat = cvMat ( 1, numMatches, CV_32FC2, &points1Cv[0] );
00064   m_Points2CvMat = cvMat ( 1, numMatches, CV_32FC2, &points2Cv[0] );
00065 
00066   int method = 0;
00067   switch (CV_FM_7POINT) // TODO Config::getInstance()->getInt( "ObjectRecognition.FundamentalMat.iMethod" ))
00068   {
00069     case 0 :
00070       method = CV_FM_7POINT;
00071       break;
00072     case 1 :
00073       method = CV_FM_8POINT;
00074       break;
00075     case 2 :
00076       method = CV_FM_RANSAC;
00077       break;
00078     case 3:
00079       method = CV_FM_LMEDS;
00080       break;
00081     default:
00082       // TRACE_ERROR("Undefined methode to find fundamental matrix"); // TODO use ros
00083       break;
00084   }
00085   CvMat* status;
00086   status = cvCreateMat(1,numMatches,CV_8UC1);
00087 
00088 
00089 //int cvFindFundamentalMat(const CvMat* points1, const CvMat* points2, CvMat* fundamental_matrix, int method=CV_FM_RANSAC, double param1=1., double param2=0.99, CvMat* status=NULL)
00090   //returns the number of fundamental matrices found (1 or 3) and 0, if no matrix is found.
00091   m_Success = cvFindFundamentalMat( &m_Points2CvMat, &m_Points1CvMat, &m_FundMatCv, method, m_MaxReprojectionError,0.99,status);
00092 
00093   // TRACE_INFO("Status = " << m_Success );  // TODO use ros
00094 
00095   return m_Success > 0 ? true : false;
00096 }
00097 
00098 void THIS::eliminateBadMatches()
00099 {
00100   std::vector<Point2D> points2;
00101   std::vector<Point2D> projPoints;
00102 
00103   points2.reserve( m_Matches.size() );
00104 
00105   std::list<KeyPointMatch>::iterator currentMatch = m_Matches.begin();
00106   while ( currentMatch != m_Matches.end() )
00107   {
00108     Point2D pos2 = ( *m_KeyPoints2 ) [ currentMatch->index2 ].position();
00109     points2.push_back( pos2 );
00110     currentMatch++;
00111   }
00112 
00113 //  void cvComputeCorrespondEpilines(const CvMat* points, int which_image, const CvMat* fundamental_matrix, CvMat* correspondent_lines)
00114 //which image?
00115   //Tranform object points to scene by fundamental mat
00116 
00117   double correspondent_lines[3];
00118 
00119   memset ( correspondent_lines, 0, 3*sizeof ( double ) );
00120 
00121   CvMat correspondent_linesCV;
00122   correspondent_linesCV= cvMat(3,m_Matches.size(),CV_32F,correspondent_lines);
00123 
00124   cvComputeCorrespondEpilines(&m_Points2CvMat, 1, &m_FundMatCv, &correspondent_linesCV);
00125 
00126   //Compare line distance to point distance
00127 
00128   currentMatch = m_Matches.begin();
00129   int i = 0;
00130   while ( currentMatch != m_Matches.end() )
00131   {
00132 //    Point2D pos1 = ( *m_KeyPoints1 ) [ currentMatch->index1 ].position();
00133 //    float scale = ( *m_KeyPoints1 ) [ currentMatch->index1 ].scale;
00134 //    if ( pos1.distance( projPoints[i] ) > m_MaxReprojectionError*scale )
00135 //     int x = correspondent_lines[i].x;
00136 //     int y = correspondent_lines[i].y;
00137 //    if()
00138 //    {
00139 //      currentMatch = m_Matches.erase( currentMatch );
00140 //    }
00141 //    else
00142 //    {
00143 //      currentMatch++;
00144 //    }
00145     i++;
00146   }
00147 }
00148 
00149 
00150 #undef THIS


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