opencv中knnmatch的返回值,cv2.DMatch解释

转载于:https://blog.csdn.net/wphkadn/article/details/85805105

queryIdx : 查询点的索引(当前要寻找匹配结果的点在它所在图片上的索引).
trainIdx : 被查询到的点的索引(存储库中的点的在存储库上的索引)
imgIdx : 有争议(常为0)

DMatch的定义
/*
 * Struct for matching: query descriptor index, train descriptor index, train image index and distance between descriptors.
 */
struct CV_EXPORTS_W_SIMPLE DMatch
{
    CV_WRAP DMatch() : queryIdx(-1), trainIdx(-1), imgIdx(-1), distance(FLT_MAX) {}
    CV_WRAP DMatch( int _queryIdx, int _trainIdx, float _distance ) :
            queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1), distance(_distance) {}
    CV_WRAP DMatch( int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) :
            queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx), distance(_distance) {}
CV_PROP_RW <span class="token keyword">int</span> queryIdx<span class="token punctuation">;</span> <span class="token comment">// query descriptor index</span>
CV_PROP_RW <span class="token keyword">int</span> trainIdx<span class="token punctuation">;</span> <span class="token comment">// train descriptor index</span>
CV_PROP_RW <span class="token keyword">int</span> imgIdx<span class="token punctuation">;</span>   <span class="token comment">// train image index</span>

CV_PROP_RW <span class="token keyword">float</span> distance<span class="token punctuation">;</span>

<span class="token comment">// less is better</span>
<span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token operator">&lt;</span><span class="token punctuation">(</span> <span class="token keyword">const</span> DMatch <span class="token operator">&amp;</span>m <span class="token punctuation">)</span> <span class="token keyword">const</span>
<span class="token punctuation">{<!-- --></span>
    <span class="token keyword">return</span> distance <span class="token operator">&lt;</span> m<span class="token punctuation">.</span>distance<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
具体解释,验证过程

我们从图1中提取了 sift/surf/orb或其他种类的特征点vector<KeyPoint> keypoints_1以及相应的描述子descriptor1,大小为319,从图2中也提取了特征点vector<KeyPoint> keypoints_2以及相应的描述子descriptor2,大小为320.

//descriptor1中的每一个特征点的描述子称为 d1i,descriptor2中的每一个特征点的描述子称为 d2i.

//通过opencv的`FlannBasedMatcher`或者`BruteForceMatcher<>`进行匹配,
// 在descriptor2中挑选两个离 `d1i`最近的描述子放入matches之中。
	FlannBasedMatcher matcher;
	std::vector<vector<DMatch>> matches;
    matcher.knnMatch(descriptor1,descriptor2,matches,2);  

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

通过调试得到如图所示结果:
在这里插入图片描述
可以看出 queryIdx是按顺序增大的, 相应的trainIdx是不断跳变的,且imgIdx一直是0.
可以从matches[0]看出,这里的queryIdx是指第0张图的第0个特征点匹配到了第1张图的第6个特征点(距离最近),同时第0张图的第0个特征点匹配到了第1张图的第128个特征点(距离第2近)。

于是 queryIdx 是你想要为“它”找到匹配结果的“它”的索引。 trainIdx是 “它”的匹配结果的索引。
由于imgIdx一直是0,只能说在这里可能没什么用。有些文章说 imgIdx指的是第0张图,有的说是第1张图的意思,从这里感觉更应该是指第0张图。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值