我在OpenCV网站上尝试了这个例子:
import numpy as np
import cv2
from matplotlib import pyplot as plt
# changed the image names from box* since the sample images were not given on the site
img1 = cv2.imread('burger.jpg',0) # queryImage
img2 = cv2.imread('burger.jpg',0) # trainImage
# Initiate SIFT detector
sift = cv2.SIFT()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in xrange(len(matches))]
# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i]=[1,0]
draw_params = dict(matchColor = (0,255,0),
singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = 0)
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
plt.imshow(img3,),plt.show()
执行示例,即python test.py
,会出现以下错误:
Traceback (most recent call last):
File "test.py", line 10, in <module>
sift = cv2.SIFT()
AttributeError: 'module' object has no attribute 'SIFT'
我从源代码安装了OpenCV,手动构建。如果我没有记错的话,所有模块都是由make
构建的。
这个问题建议我从它的GitHub存储库安装opencv-contrib
。我做到了,但我仍然会犯这个错误。
我的系统是Ubuntu 15.04 64位。
我不完全确定这是否适用,但在某个时候,他们在opencv的后期版本中停止了对SIFT的支持,我相信这是因为它是专利或相关的(来源?(,然而,另一种选择是使用ORB,这将产生类似的效果。
你可以试试这样的东西:
from cv2 import ORB as SIFT
然而,如果你遇到导入错误,这也可能对你有用:
SIFT = cv2.ORB_create
如果你在文件顶部附近插入这些,那么很可能你可以在整个文件中保留"SIFT"(或多或少,你明白了,基本上用SIFT=SIFT((替换cv2.SIFT((,你应该会有更好的状态。(