学习OpenCV——KeyPoint Matching 优化方式

来源:互联网 发布:vb语言基本代码 编辑:程序博客网 时间:2024/06/14 04:19

今天读Mastering OpenCV with Practical Computer Vision Projects 中的第三章里面讲到了几种特征点匹配的优化方式,在此记录。

在图像特征点检测完成后(特征点检测参考:学习OpenCV——BOW特征提取函数(特征点篇)),就会进入Matching  procedure。



1. OpenCV提供了两种Matching方式

• Brute-force matcher (cv::BFMatcher) 

• Flann-based matcher (cv::FlannBasedMatcher)

Brute-force matcher就是用暴力方法找到点集一中每个descriptor在点集二中距离最近的descriptor;

Flann-based matcher 使用快速近似最近邻搜索算法寻找(用快速的第三方库近似最近邻搜索算法

一般把点集一称为 train set (训练集)对应模板图像,点集二称为 query set(查询集)对应查找模板图的目标图像。

为了提高检测速度,你可以调用matching函数前,先训练一个matcher。训练过程可以首先使用cv::FlannBasedMatcher来优化,为descriptor建立索引树,这种操作将在匹配大量数据时发挥巨大作用(比如在上百幅图像的数据集中查找匹配图像)。而Brute-force matcher在这个过程并不进行操作,它只是将train descriptors保存在内存中。



2. 在matching过程可以使用cv::DescriptorMatcher的如下功能来进行匹配:

  • 简单查找最优匹配:void match(const Mat& queryDescriptors, vector<DMatch>& matches,const vector<Mat>& masks=vector<Mat>() );
  • 为每个descriptor查找K-nearest-matches:void knnMatch(const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k,const vector<Mat>&masks=vector<Mat>(),bool compactResult=false );
  • 查找那些descriptors间距离小于特定距离的匹配:void radiusMatch(const Mat& queryDescriptors, vector<vector<DMatch> >& matches, maxDistance, const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );

3. matching结果包含许多错误匹配,错误的匹配分为两种:

  • False-positive matches: 将非对应特征点检测为匹配(我们可以对他做文章,尽量消除它)
  • False-negative matches: 未将匹配的特征点检测出来(无法处理,因为matching算法拒绝)
为了消除False-positive matches采用如下两种方式:
  • Cross-match filter:
在OpenCV中 cv::BFMatcher class已经支持交叉验证,建立 cv::BFMatcher将第二参数声明为true
cv::Ptr<cv::DescriptorMatcher> matcher(new cv::BFMatcher(cv::NORM_HAMMING,true));

经过Cross-match filter的结果:

  • Ratio test
使用KNN-matching算法,令K=2。则每个match得到两个最接近的descriptor,然后计算最接近距离和次接近距离之间的比值,当比值大于既定值时,才作为最终match。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. void PatternDetector::getMatches(const cv::Mat& queryDescriptors,  std::vector<cv::DMatch>& matches)  
  2. {  
  3.     matches.clear();  
  4.     if (enableRatioTest)  
  5.     {  
  6.         // To avoid NaNs when best match has   
  7.         // zero distance we will use inverse ratio.   
  8.         const float minRatio = 1.f / 1.5f;  
  9.         // KNN match will return 2 nearest   
  10.         // matches for each query descriptor  
  11.         m_matcher->knnMatch(queryDescriptors, m_knnMatches, 2);  
  12.         for (size_t i=0; i<m_knnMatches.size(); i++)  
  13.         {  
  14.             const cv::DMatch& bestMatch = m_knnMatches[i][0];  
  15.             const cv::DMatch& betterMatch = m_knnMatches[i][1];  
  16.             float distanceRatio = bestMatch.distance /   
  17.                 betterMatch.distance;  
  18.             // Pass only matches where distance ratio between   
  19.             // nearest matches is greater than 1.5   
  20.             // (distinct criteria)  
  21.             if (distanceRatio < minRatio)  
  22.             {  
  23.                 matches.push_back(bestMatch);  
  24.             }  
  25.         }  
  26.     }  
  27.     else  
  28.     {  
  29.         // Perform regular match  
  30.         m_matcher->match(queryDescriptors, matches);  
  31.     }  
  32. }   
void PatternDetector::getMatches(const cv::Mat& queryDescriptors,  std::vector<cv::DMatch>& matches){    matches.clear();    if (enableRatioTest)    {        // To avoid NaNs when best match has         // zero distance we will use inverse ratio.         const float minRatio = 1.f / 1.5f;        // KNN match will return 2 nearest         // matches for each query descriptor        m_matcher->knnMatch(queryDescriptors, m_knnMatches, 2);        for (size_t i=0; i<m_knnMatches.size(); i++)        {            const cv::DMatch& bestMatch = m_knnMatches[i][0];            const cv::DMatch& betterMatch = m_knnMatches[i][1];            float distanceRatio = bestMatch.distance /                 betterMatch.distance;            // Pass only matches where distance ratio between             // nearest matches is greater than 1.5             // (distinct criteria)            if (distanceRatio < minRatio)            {                matches.push_back(bestMatch);            }        }    }    else    {        // Perform regular match        m_matcher->match(queryDescriptors, matches);    }} 



4. Homography estimation

为了进一步提升匹配精度,可以采用随机样本一致性(RANSAC)方法。
因为我们是使用一幅图像(一个平面物体),我们可以将它定义为刚性的,可以在pattern image和query image的特征点之间找到单应性变换(homography transformation)。使用cv::findHomography找到这个单应性变换,使用RANSAC找到最佳单应性矩阵。(由于这个函数使用的特征点同时包含正确和错误匹配点,因此计算的单应性矩阵依赖于二次投影的准确性)
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. bool PatternDetector::refineMatchesWithHomography  
  2. (  
  3. const std::vector<cv::KeyPoint>& queryKeypoints,  
  4. const std::vector<cv::KeyPoint>& trainKeypoints,   
  5. float reprojectionThreshold,  
  6. std::vector<cv::DMatch>& matches,  
  7. cv::Mat& homography  
  8. )  
  9. {  
  10. const int minNumberMatchesAllowed = 8;  
  11. if (matches.size() < minNumberMatchesAllowed)  
  12. return false;  
  13. // Prepare data for cv::findHomography  
  14. std::vector<cv::Point2f> srcPoints(matches.size());  
  15. std::vector<cv::Point2f> dstPoints(matches.size());  
  16. for (size_t i = 0; i < matches.size(); i++)  
  17. {  
  18. srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;  
  19. dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;  
  20. }  
  21. // Find homography matrix and get inliers mask  
  22. std::vector<unsigned char> inliersMask(srcPoints.size());  
  23. homography = cv::findHomography(srcPoints,   
  24. dstPoints,   
  25. CV_FM_RANSAC,   
  26. reprojectionThreshold,   
  27. inliersMask);  
  28. std::vector<cv::DMatch> inliers;  
  29. for (size_t i=0; i<inliersMask.size(); i++)  
  30. {  
  31. if (inliersMask[i])  
  32. inliers.push_back(matches[i]);  
  33. }  
  34. matches.swap(inliers);  
  35. return matches.size() > minNumberMatchesAllowed;  
  36. }   
bool PatternDetector::refineMatchesWithHomography(const std::vector<cv::KeyPoint>& queryKeypoints,const std::vector<cv::KeyPoint>& trainKeypoints, float reprojectionThreshold,std::vector<cv::DMatch>& matches,cv::Mat& homography){const int minNumberMatchesAllowed = 8;if (matches.size() < minNumberMatchesAllowed)return false;// Prepare data for cv::findHomographystd::vector<cv::Point2f> srcPoints(matches.size());std::vector<cv::Point2f> dstPoints(matches.size());for (size_t i = 0; i < matches.size(); i++){srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;}// Find homography matrix and get inliers maskstd::vector<unsigned char> inliersMask(srcPoints.size());homography = cv::findHomography(srcPoints, dstPoints, CV_FM_RANSAC, reprojectionThreshold, inliersMask);std::vector<cv::DMatch> inliers;for (size_t i=0; i<inliersMask.size(); i++){if (inliersMask[i])inliers.push_back(matches[i]);}matches.swap(inliers);return matches.size() > minNumberMatchesAllowed;} 

经过单应性变换的过滤结果



0 0
原创粉丝点击