简体   繁体   English

从最近的邻居距离比绘图匹配

[英]Drawing matches from Nearest Neighbour Distance Ratio

In openCV I have adapted this tutorial code in my application 在openCV中,我已在我的应用程序中改编了此教程代码

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

I have been trying to prune the matches using the Nearest Neighbour Distance Ratio to only draw matches above a certain threshold value. 我一直在尝试使用“最近的邻居距离比率”来修剪匹配项,以便仅将匹配项高于某个阈值。

  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]);
      }
  }

I am then trying to draw the matches in good_NNmatches using the same method demonstrated in the tutorial but I get the following error: 然后,我尝试使用本教程中演示的相同方法在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

When trying to call drawMatches using good_nnMatches instead of good_matches as described in the tutorial. 尝试使用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 );

I just had the same problem. 我只是有同样的问题。 In my case I was doing 就我而言

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

Where the matches matchesBig2Small were computed from bigimg to smallimg 匹配matchesBig2Small的匹配matchesBig2Small是从bigimgsmallimg

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

So the error you got is actually saying that it's looking for the keypoint index in smallimg that actually refers to the second image, which is bigger so you're exceeding the size of smallimg . 因此,您得到的错误实际上是在寻找smallimg中的关键点索引,该索引实际上指向第二张图像,该索引较大,因此您超出了smallimg的大小。 So either change the call to drawMatches to: 因此,可以drawMatches的调用drawMatches为:

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

Or change the way you're computing the matches to be from smallimg to bigimg , so instead of matchesBig2Small you'd have matchesSmall2Big , and then the call would be: 或者改变你的计算比赛是从路smallimgbigimg ,所以不是matchesBig2Small你必须matchesSmall2Big ,然后呼叫将是:

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

Ok after some trial and error I appear to have solved this issue now. 在经过一番尝试和错误之后,我看来现在已经解决了这个问题。

I have changed the drawMatches method to swap the "roiImg" and key points with "compare" and key points. 我更改了drawMatches方法,以将“ roiImg”和关键点与“ compare”和关键点交换。

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

This stopped the assertion error. 这停止了​​断言错误。 But due to swapping the values around when it came to retrieving the key points I also had to make some changes. 但是由于要在检索关键点时交换价值,我还必须进行一些更改。 trainIdx and queryIdx have also been swapped to account for the previous changes made. trainIdx和queryIdx也已交换,以说明以前所做的更改。

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

I have done it and i don not have anyproblems I hope that this code can help you 我已经做到了,没有任何问题,希望这段代码对您有所帮助

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM