2016-01-27 66 views
0

目前我在opevcv工作與Python,但是當我使用在ORB匹配誤差與OpenCV的3

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,誰能告訴我爲什麼我得到這個錯誤? 我們可以使用匹配器或不使用python?

+0

有您創建的[匹配對象(http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html)第一?。類似於'matcher = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck = True)' – Miki

+0

不,實際上我是這個領域的新手,所以我不知道創建匹配器對象的正確語法是什麼 – whishky

+0

檢查我發佈在鏈接 – Miki

回答

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()