我在OpenCV网站上试过this example:
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
构建的。
This question建议我从其GitHub存储库安装opencv-contrib
。我做了,我仍然得到这个错误。
我的系统是Ubuntu 15.04 64位。
答案 0 :(得分:0)
我不完全确定这是否适用,但在某些时候他们在opencv的更高版本中停止支持SIFT我相信由于它是专利或相关的东西(来源?),但是替代是使用具有类似效果的ORB。
您可以尝试这样的事情:
from cv2 import ORB as SIFT
但是,如果您收到导入错误,这也可能对您有用:
SIFT = cv2.ORB_create
如果你在文件顶部附近插入那些,那么可能你可以离开&#34; SIFT&#34;因为它在整个文件中(或多或少,你得到了想法,基本上用sift = SIFT()替换cv2.Sift(),你应该处于更好的状态。)