【问题标题】:Drawing matches from Nearest Neighbour Distance Ratio根据最近邻距离比绘制匹配
【发布时间】:2014-04-17 14:23:15
【问题描述】:

在 openCV 中,我已经在我的应用程序中修改了这个教程代码

http://docs.opencv.org/2.4.2/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography

我一直在尝试使用最近邻距离比来修剪匹配,以仅绘制高于某个阈值的匹配。

  double ratio = 0.9;
  std::vector< vector<DMatch > > nnMatches;
  std::vector< DMatch > good_NNmatches;
  matcher.knnMatch(descriptors_scene, descriptors_object, nnMatches, 2 );

  for(int k = 0; k < nnMatches.size(); k++)
  {
      if(nnMatches[k][0].distance / nnMatches[k][1].distance > ratio)
      {
          good_NNmatches.push_back(nnMatches[k][0]);
      }
  }

然后我尝试使用教程中演示的相同方法在 good_NNmatches 中绘制匹配项,但出现以下错误:

OpenCV Error: Assertion failed (i1 >= 0 && i1 < static_cast<int>(keypoints1.size())) in     drawMatches, file /Users/cgray/Downloads/opencv-2.4.6/modules/features2d/src/draw.cpp, line 207
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/cgray/Downloads/opencv-2.4.6/modules/features2d/src/draw.cpp:207: error: (-215) i1 >= 0 && i1 < static_cast<int>(keypoints1.size()) in function drawMatches

当尝试使用 good_nnMatches 而不是本教程中描述的 good_matches 调用 drawMatches 时。

drawMatches( roiImg, keypoints_object, compare, keypoints_scene,
                     good_NNmatches, img_matches, Scalar::all(-1), Scalar::all(-1),
                     vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

【问题讨论】:

  • 我不知道这是否是您的问题的原因,但您的条件是错误的。如果nnMatches[k][0].distance / nnMatches[k][1].distance &lt; ratio,则应在good_NNmatches 中添加匹配项。
  • 这并没有解决断言错误,但我现在已经更改了条件并找到了解决问题的方法,谢谢。
  • 我们为什么不删除 DRAW.CPP 中的断言??????????

标签: c++ opencv feature-detection


【解决方案1】:

我也遇到了同样的问题。就我而言,我正在做

cv::Mat output;
drawMatches( smallimg, keyptsSm, bigimg, keyptsBg, matchesBig2Small, output );

匹配 matchesBig2Small 是从 bigimg 计算到 smallimg

std::vector<cv::DMatch> matchesBig2Small; 
//match from bigimg to smallimg

所以你得到的错误实际上是说它正在寻找smallimg 中的关键点索引,它实际上是指第二个图像,它更大,所以你超过了smallimg 的大小。所以要么把drawMatches的电话改成:

drawMatches( bigimg, keyptsBg, smallimg, keyptsSm, matchesBig2Small, output );

或者将计算匹配项的方式从 smallimg 更改为 bigimg,因此您将使用 matchesSmall2Big 而不是 matchesBig2Small,然后调用将是:

std::vector<cv::DMatch> matchesSmall2Big; 
//match from smallimg to bigimg
drawMatches( smallimg, keyptsSm, bigimg, keyptsBg, matchesSmall2Big, output );

【讨论】:

  • 谢谢,这正是我的错误。
【解决方案2】:

好的,经过反复试验,我现在似乎已经解决了这个问题。

我已更改 drawMatches 方法,将“roiImg”和关键点与“比较”和关键点交换。

drawMatches(compare, keypoints_scene, roiImg, keypoints_object,
               good_NNmatches, img_matches, Scalar::all(-1), Scalar::all(-1),
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

这停止了断言错误。但是由于在检索关键点时交换了值,我还必须进行一些更改。 trainIdx 和 queryIdx 也已交换,以说明之前所做的更改。

obj.push_back( keypoints_object[ good_NNmatches[i].trainIdx ].pt );
scene.push_back( keypoints_scene[ good_NNmatches[i].queryIdx ].pt );

【讨论】:

    【解决方案3】:

    我已经做到了,我没有任何问题 希望这段代码可以帮到你

    double NNDR;
    
        for(int i=0;i<(int)ORB_ORB_matches.size(); i++){
    
            NNDR= ( ORB_ORB_matches[i][0].distance/ORB_ORB_matches[i][1].distance );
    
            if(NNDR <= 0.9){
    
                ORB_single_matches.push_back (ORB_ORB_matches[i][0]);
    
            }
    
        }
    drawMatches( image_1, keypoints_1, image_2, keypoints_2, ORB_single_matches, img_matches,Scalar::all(-1),Scalar::all(-1),vector<char>(),`enter code here`          DrawMatchesFlags::DRAW_RICH_KEYPOINTS|DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2021-07-25
      • 1970-01-01
      • 2011-06-18
      • 2020-08-28
      • 2016-10-31
      • 2017-06-21
      • 2011-11-12
      • 2018-02-06
      相关资源
      最近更新 更多