如何在Opencv中绘制匹配项

How to draw matches in Opencv?

本文关键字:绘制 Opencv      更新时间:2023-10-16

我已经匹配了两个图像的两个描述符向量:

cv::Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
std::vector<std::vector<cv::DMatch> >  matches;
float maxDist = 10.0;
bdm->radiusMatch(descr2, descr1, matches, maxDist);
// descr1 from image1, descr2 from image2
std::vector<char> mask(matches.size(), 1);

但是现在我想从两个图像中绘制找到的匹配项。

这不起作用:

drawMatches(gmlimg, keylines, walls, keylines1, matches, outImg, cv::Scalar::all(-1), cv::Scalar::all(-1), mask, DrawLinesMatchesFlags::DEFAULT);

这两者都不是:

drawLineMatches(gmlimg, keylines, walls, keylines1, matches, outImg, cv::Scalar::all(-1), cv::Scalar::all(-1), mask, DrawLinesMatchesFlags::DEFAULT);

由于您获得的匹配项为 std::vector< std::vector<cv::DMatch> > ,这是您在使用 BinaryDescriptorMatcher 时将使用的,您可以按如下方式绘制匹配项:

std::vector<DMatch> matches_to_draw;
std::vector< std::vector<DMatch> >matches_from_matcher;
std::vector< cv::Keypoint > keypoints_Object, keypoints_Scene; // Keypoints 
// Iterate through the matches from descriptor
for(unsigned int i = 0; i < matches_from_matcher.size(); i++)
{
    if (matches_from_matcher[i].size() >= 1)
    {
      cv::DMatch v = matches[i][0];
      /*
       May be you can add a filtering here for the matches
       Skip it if you want to see all the matches
       Something like this - avg is the average distance between all keypoint pairs
       double difference_for_each_match = fabs(keypoints_Object[v.queryIdx].pt.y
                                         - keypoints_Scene[v.trainIdx].pt.y);
       if( (fabs (avg - difference_for_each_match)) <= 5))
     {
       matches_to_draw.push_back(v);
     }
     */
     // This is for all matches
     matches_to_draw.push_back(v);
    }
}
cv::drawMatches(image, keypoints_Object, walls, keypoints_Scene, matches_to_draw, outImg, cv::Scalar::all(-1), cv::Scalar::all(-1), mask, DrawLinesMatchesFlags::DEFAULT);`

出局应该有匹配和关键点。

让我知道它是否有帮助!