Skip to content

Instantly share code, notes, and snippets.

@kauevestena
Last active June 4, 2017 03:22
Show Gist options
  • Save kauevestena/023daa83a70a46031c1a to your computer and use it in GitHub Desktop.
Save kauevestena/023daa83a70a46031c1a to your computer and use it in GitHub Desktop.
to remove outliers from the matching algorithms
void filterMatches(const std::vector<cv::DMatch> matches,const std::vector<cv::KeyPoint>&keypoints1,const std::vector<cv::KeyPoint>& keypoints2,std::vector<cv::DMatch>& goodMatches,double dist,double confidence_interval)
{
goodMatches.clear();
// converting keypoints to just 2D points
std::vector<cv::Point2f> points1, points2;
for (std::vector<cv::DMatch>::const_iterator it= matches.begin();it!= matches.end(); ++it)
{
// from de matches, extract just the keypoints of the left frame
float x= keypoints1[it->queryIdx].pt.x;
float y= keypoints1[it->queryIdx].pt.y;
points1.push_back(cv::Point2f(x,y));
// from de matches, extract just the keypoints of the left frame
x= keypoints2[it->trainIdx].pt.x;
y= keypoints2[it->trainIdx].pt.y;
points2.push_back(cv::Point2f(x,y));
}
// calculating the Fundamental mat with the ransac algorithm
std::vector<uchar> inliers(points1.size(),0);
cv::Mat fundamental= cv::findFundamentalMat(cv::Mat(points1),cv::Mat(points2),inliers,CV_FM_RANSAC,dist,confidence_interval); // intervalo de confianca
// with the inliers, choose from the matches only the "good" ones
std::vector<uchar>::const_iterator
itIn= inliers.begin();
std::vector<cv::DMatch>::const_iterator
itM= matches.begin();
// for all matches
for ( ;itIn!= inliers.end(); ++itIn, ++itM)
{
if (*itIn)
{ // it is a valid match
goodMatches.push_back(*itM);
}
}
}
void keypoints2points(std::vector<cv::Point2f> &points1,std::vector<cv::Point2f> &points2,const std::vector<cv::KeyPoint>&keypoints1,const std::vector<cv::KeyPoint>&keypoints2,const std::vector<cv::DMatch> matches)
{
for (std::vector<cv::DMatch>::const_iterator it= matches.begin();it!= matches.end(); ++it)
{
float x,y;
// from the matches, extract just the keypoints of the left frame
x= keypoints1[it->queryIdx].pt.x;
y= keypoints1[it->queryIdx].pt.y;
points1.push_back(cv::Point2f(x,y));
// from the matches, extract just the keypoints of the right frame
x= keypoints2[it->trainIdx].pt.x;
y= keypoints2[it->trainIdx].pt.y;
points2.push_back(cv::Point2f(x,y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment