ORB与opencv 3匹配错误

时间:2016-01-27 13:22:24

标签: python-2.7 opencv opencv3.0 orb flann

目前我正在使用python运行opevcv但是当我使用

    kp1 = orb.detect(img1,None)
    kp2 = orb.detect(img2,None)
    kp1, des1 = orb.compute(img1, kp1)
    kp2, des2 = orb.compute(img2, kp2)
    matches = matcher.match(des1, des2)

我得到了未定义匹配器的错误

    matches = matcher.match(des1, des2)
    NameError: name 'matcher' is not defined

,我正在使用opencv 3.0.0和python 2.7,谁能告诉我为什么我会收到此错误? 我们可以使用matcher或不使用python ??

2 个答案:

答案 0 :(得分:2)

您需要先创建matcher对象。可以在OpenCV tutorials上找到完整的示例:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage

# Initiate ORB detector
orb = cv2.ORB()

# find the keypoints and descriptors with ORB
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)

# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# Match descriptors.
matches = bf.match(des1,des2)

# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)

# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)

plt.imshow(img3),plt.show()

答案 1 :(得分:0)

在上面的代码中,而不是orb = cv2.ORB()

use orb = cv2.ORB_create()

这解决了版本兼容性问题:

  

TypeError:自身的类型不正确(必须为'Feature2D'或其   导数)