robustMatch

来源:互联网 发布:淘宝大学官方视频教程 编辑:程序博客网 时间:2024/06/05 15:09
本帖最后由 hy17003 于 2015-5-17 16:15 编辑

最近在看《OpenCV2 计算机视觉编程手册》,学到第9章,这一章大致讲的是从两幅不同视角的同一场景图像中找到相应的匹配并计算出基础矩阵,方法非常好。作者先用surf检测出两幅图像的关键点,然后使用比率测试、对称测试、RANSAC测试对匹配进行筛选,最后选出高质量的匹配点,进行计算出基础矩阵。但是问题是,在比率测试中出现了问题。比率测试是这样的:对于每个特征点,在另一幅图像中找到两个候选的匹配点(使用BruteForceMatcher::knnMatch方法实现,k=2),其中一个是最优匹配点,另一个为次优匹配点,如果最优匹配点的测量距离非常小,而次优匹配点的测量距离相对较大,那么最优匹配点无疑是安全可靠的,如果两个候选匹配点的测量距离相近,那么如果选择其中之一作为匹配点很可能出错,是不可靠的。比率测试正是检查这两个距离的比值,以除去不安全的匹配。
这本应该是一个极强的检测算法,但是,在程序中,发现knnMatch似乎有些问题。knnMatch的功能是为每一个描述符找出k个最好的匹配。但是程序中,设置k=2,理应找出2个最好的匹配,但调试发现它给出的匹配数多数不为2,甚至是几百万个!而全部特征点的个数只有1000多个,显然这些数据是无效的!尝试访问这些无效的数据会触发异常,即使使用该书的源码,也会出现这种情况。
如何解决?我用的是vs2013+OpenCV 2.4.9。
我的代码如下:
  1. // RobustMatch.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <opencv2\opencv.hpp>
  5. #include <opencv2\nonfree\nonfree.hpp>
  6. #include <opencv2\features2d\features2d.hpp>
  7. #include <opencv2\core\core.hpp>
  8. #include <opencv2\legacy\legacy.hpp>
  9. #include <iostream>
  10. using namespace std;
  11. using namespace cv;

  12. /*********************公共变量*************************/
  13. //检测器
  14. SurfFeatureDetector detector(10.0);
  15. //提取器
  16. SurfDescriptorExtractor extractor;
  17. //第一个以及第二个最近邻之间的最大比率
  18. float ratio=0.65f;
  19. //是否再次优化F矩阵
  20. bool refineF=true;
  21. //到极线的最小矩离
  22. double distance=3.0;
  23. //置信等级(概率)
  24. double confidence=0.95;

  25. /**********************************************************************
  26. * 函数名:ratioTest
  27. * 参  数:matches
  28. * 返  回:移除的匹配数量
  29. * 说  明:对当前匹配进行筛选,最优匹配和次优匹配响应强度大于ratio的匹配以及
  30. *  孤立的匹配。
  31. ***********************************************************************/
  32. int ratioTest(std::vector<std::vector<cv::DMatch>>& matches)
  33. {
  34.         int removed = 0;
  35.         std::vector<std::vector<cv::DMatch>>::iterator matchIt = matches.begin();
  36.         for (; matchIt != matches.end();matchIt++)
  37.         {
  38.                 if (matchIt->size()  > 1)
  39.                 {
  40.                         //移除不合格比率的匹配(问题出现在这里)
  41.                         if ((*matchIt)[0].distance / (*matchIt)[1].distance > ratio)
  42.                         {
  43.                                 matchIt->clear();
  44.                                 removed++;
  45.                         }
  46.                 }
  47.                 else
  48.                 {
  49.                         //移除孤立的匹配
  50.                         matchIt->clear();
  51.                         removed++;
  52.                 }
  53.         }
  54.         return removed;
  55. }
  56. /**********************************************************************
  57. * 函数名:symmetryTest
  58. * 参  数:matches1:左匹配
  59. *       matches2:右匹配
  60. *       symMatche:输出的对称匹配
  61. * 返  回:无
  62. * 说  明:对左、右匹配进行检查,输出对称的匹配。
  63. ***********************************************************************/
  64. void symmetryTest(std::vector<std::vector<cv::DMatch>>& matches1,
  65.         std::vector<std::vector<cv::DMatch>>& matches2, std::vector<cv::DMatch>& symMatches)
  66. {
  67.         //遍历左匹配
  68.         for (auto &leftMatchRef : matches1)
  69.         {
  70.                 if (leftMatchRef.size() < 2)
  71.                         continue;
  72.                 //遍历右匹配
  73.                 for (auto &rightMatchRef : matches2)
  74.                 {
  75.                         if (rightMatchRef.size() < 2)
  76.                                 continue;
  77.                         //对称性测试
  78.                         if ((leftMatchRef[0].queryIdx == rightMatchRef[0].trainIdx) &&
  79.                                 (leftMatchRef[0].trainIdx == rightMatchRef[0].queryIdx))
  80.                         {
  81.                                 symMatches.push_back(cv::DMatch(leftMatchRef[0].queryIdx, leftMatchRef[0].trainIdx,
  82.                                         leftMatchRef[0].distance));
  83.                                 break;
  84.                         }
  85.                 }
  86.         }
  87. }

  88. /**********************************************************************
  89. * 函数名:ransacTest
  90. * 参  数:matches:当前匹配(输入)
  91. *       keypoints1:图像1检测到的关键点(输入)
  92. *       keypoints2:图像2检测到的关键点(输入)
  93. *       outMatches:完成测试的匹配(输出)
  94. * 返  回:基础矩阵
  95. * 说  明:对当前匹配进行RANSAC测试,计算基础矩阵,同时返回通过测试的匹配
  96. ***********************************************************************/
  97. cv::Mat ransacTest(
  98.         const std::vector<cv::DMatch>& matches, const std::vector<cv::KeyPoint>& keypoints1,
  99.         const std::vector<cv::KeyPoint>& keypoints2, std::vector<cv::DMatch>& outMatches)
  100. {
  101.         //将Keypoints转换到Point2f
  102.         std::vector<cv::Point2f>points1, points2;
  103.         for (auto &matchesRef : matches)
  104.         {
  105.                 //左图像关键点
  106.                 float x = keypoints1[matchesRef.queryIdx].pt.x;
  107.                 float y = keypoints1[matchesRef.queryIdx].pt.y;
  108.                 points1.push_back(cv::Point2f(x, y));
  109.                 //右图像关键点
  110.                 x = keypoints2[matchesRef.trainIdx].pt.x;
  111.                 y = keypoints2[matchesRef.trainIdx].pt.y;
  112.                 points2.push_back(cv::Point2f(x, y));
  113.         }
  114.         //基于RANSAC计算F矩阵
  115.         std::vector<uchar>inlines(points1.size(), 0);
  116.         cv::Mat fundemental = cv::findFundamentalMat(
  117.                 cv::Mat(points1),
  118.                 cv::Mat(points2),
  119.                 inlines,
  120.                 CV_FM_RANSAC,
  121.                 3.0,
  122.                 confidence
  123.                 );
  124.         //提取通过的匹配
  125.         std::vector<uchar>::const_iterator itIn = inlines.begin();
  126.         std::vector<cv::DMatch>::const_iterator itM = matches.begin();
  127.         for (; itIn != inlines.end(); itIn++, itM++)
  128.         {
  129.                 if (*itIn)
  130.                 {
  131.                         outMatches.push_back(*itM);
  132.                 }
  133.         }
  134.         //二次拟合
  135.         if (refineF)
  136.         {
  137.                 points1.clear();
  138.                 points2.clear();
  139.                 for (auto &matchRef : outMatches)
  140.                 {
  141.                         float x = keypoints1[matchRef.queryIdx].pt.x;
  142.                         float y = keypoints1[matchRef.queryIdx].pt.y;
  143.                         points1.push_back(cv::Point2f(x, y));

  144.                         x = keypoints2[matchRef.trainIdx].pt.x;
  145.                         y = keypoints2[matchRef.trainIdx].pt.y;
  146.                         points2.push_back(cv::Point2f(x, y));
  147.                 }
  148.                 fundemental = cv::findFundamentalMat(
  149.                         cv::Mat(points1),
  150.                         cv::Mat(points2),
  151.                         CV_FM_8POINT
  152.                         );
  153.         }
  154.         return fundemental;
  155. }
  156. /**********************************************************************
  157. * 函数名:match
  158. * 参  数:image1 :图像1(输入)
  159. *        image2:图像2(输入)
  160. *        matches:经过多重测试剩下的高质量的匹配(输出)
  161. *        keypoints1:用于保存图像1检测到的关键点(输出)
  162. *        keypoints2:用于保存图像2检测到的关键点(输出)
  163. * 返  回:基础矩阵
  164. * 说  明:对输出的两幅图像进行特征检测、计算描述子,进而使用BruteForceMatcher
  165. * 进行匹配,对初始得到的匹配关系再依次进行比率测试、对称测试最后进行Ransac
  166. * 验证,并得到两个相机的基础矩阵。
  167. ***********************************************************************/
  168. cv::Mat match(cv::Mat &image1, cv::Mat &image2, std::vector<cv::DMatch>&matches,
  169.         std::vector<cv::KeyPoint>&keypoints1, std::vector<cv::KeyPoint>&keypoints2)
  170. {
  171.         //1.a 检测SURF特征
  172.         detector.detect(image1, keypoints1);
  173.         detector.detect(image2, keypoints2);
  174.         //1.b 计算SURF描述子
  175.         cv::Mat descriptors1, descriptors2;
  176.         extractor.compute(image1, keypoints1, descriptors1);
  177.         extractor.compute(image2, keypoints2, descriptors2);
  178.         //2 匹配两幅图像的描述子
  179.         //2.a创建匹配器
  180.         cv::BruteForceMatcher<cv::L2<float>>matcher;
  181.         //2.b计算图1->图2,图2->图1 的k最近邻(k=2)
  182.         std::vector<std::vector<cv::DMatch>>matcher1;
  183.         std::vector<std::vector<cv::DMatch>>matcher2;
  184.         //这里调用了knnMatch
  185.         matcher.knnMatch(descriptors1, descriptors2, matcher1, 2);
  186.         matcher.knnMatch(descriptors2, descriptors1, matcher2, 2);

  187.         //3.比率测式
  188.         int removed = ratioTest(matcher1);
  189.         removed = ratioTest(matcher2);
  190.         //4.对称性测试
  191.         std::vector<cv::DMatch>symMatches;
  192.         symmetryTest(matcher1, matcher2, symMatches);
  193.         //5.RANSAC最终验证
  194.         cv::Mat fundemental = ransacTest(symMatches, keypoints1, keypoints2, matches);
  195.         return fundemental;
  196. }
  197. int _tmain(int argc, _TCHAR* argv[])
  198. {
  199.         //1.读取图像
  200.         Mat image1 = imread("canal1.jpg", 0);
  201.         Mat image2 = imread("canal2.jpg", 0);
  202.         if (!image1.data || !image2.data)
  203.         {
  204.                 return 1;
  205.         }
  206.         //2.检测
  207.         std::vector<cv::DMatch> matches;
  208.         std::vector<KeyPoint> keypoints1, keypoints2;
  209.         match(image1, image2, matches, keypoints1, keypoints2);
  210.         //3.绘制效果图
  211.         Mat imgMatch;
  212.         drawMatches(image1, keypoints1, image2, keypoints2, matches, imgMatch);
  213.         imshow("效果图", imgMatch);
  214.         waitKey(0);
  215.         return 0;
  216. }
复制代码


以上是我改写原书的程序,为的是看起来方便,原书的代码在附件里。
除了以上的程序外,这里还有一些相似的例子:http://www.cesclub.com/bw/jishuz ... 013/0905/70335.html
http://www.cnblogs.com/wangguchangqing/p/4333873.html
0 0
原创粉丝点击