1、bf.match()
# 建立暴力匹配
bf = cv2.BFMatcher()
# 对描述子进行匹配
# des1、des2分别是图片img1、img2的特征向量
matches = bf.match(des1, des2)
如图所示,matches为数据类型为list,包含了所匹配的特征点,list中每个元素的数据类型为DMatch。
DMatch的数据结构包括:queryIdx、trainIdx、distance
queryIdx:某一特征点在本帧图像的索引,即在img1特征点的索引;
trainIdx:trainIdx是该特征点在另一张图像中相匹配的特征点的索引,即img2特征点的索引;
distance:代表这一对匹配的特征点描述符的欧式距离,数值越小也就说明俩个特征点越相近print('queryIdx=%d' % matches[0].queryIdx) print('trainIdx=%d' % matches[0].trainIdx) print('distance=%d' % matches[0].distance) queryIdx=0 trainIdx=24 distance=370
2、bf.knnMatch
# 建立暴力匹配
bf = cv2.BFMatcher()
# 对描述子进行匹配
# k取2
matches = bf.knnMatch(des1, des2, k=2)
可以看出,与match返回值不同的是,knnmatch返回的是两个匹配点,一个是最近邻一个是次近邻。