特征匹配

一旦我们找到了两个相似物体的特征,并计算它们的描述符, 就可以寻找它们之间的匹配了。最简单的方法就是取第一个集合的每个特征的描述符,计算与第二个集合所有特征描述符的距离,选取最近的距离最为最好匹配(选择的距离度量应该和描述符相匹配)。这是一个暴力搜索。
在匹配完成之后,我们应该定义一些标准来判定物体是否找到。比如说定义一个匹配最小点数目的阈值,如果匹配的点的数目大于这个值,我们认为物体找到。否则我们说没有足够的证据说这个识别是成功的。
code


MIN_MATCHES = 15
cap = cv2.imread('scene.jpg', 0)    
model = cv2.imread('model.jpg', 0)
# ORB keypoint detector
orb = cv2.ORB_create()              
# create brute force  matcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)  
# Compute model keypoints and its descriptors
kp_model, des_model = orb.detectAndCompute(model, None)  
# Compute scene keypoints and its descriptors
kp_frame, des_frame = orb.detectAndCompute(cap, None)
# Match frame descriptors with model descriptors
matches = bf.match(des_model, des_frame)
# Sort them in the order of their distance
matches = sorted(matches, key=lambda x: x.distance)

if len(matches) > MIN_MATCHES:
    # draw first 15 matches.
    cap = cv2.drawMatches(model, kp_model, cap, kp_frame,
                          matches[:MIN_MATCHES], 0, flags=2)
    # show result
    cv2.imshow('frame', cap)
    cv2.waitKey(0)
else:
    print "Not enough matches have been found - %d/%d" % (len(matches),
                                                          MIN_MATCHES)
posted @ 2017-11-17 16:56  jinzhongxiao  阅读(503)  评论(0编辑  收藏  举报