如何在 Opencv 中绘制匹配项?
How to draw matches in Opencv?
我已经匹配了两个图像的两个描述符向量:
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);`
outImg 应该有匹配和绘制的关键点。
如果有帮助请告诉我!
我已经匹配了两个图像的两个描述符向量:
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);`
outImg 应该有匹配和绘制的关键点。
如果有帮助请告诉我!