OpenCV doesn't come with “external” libraries

无人久伴 提交于 2019-12-24 16:02:13

问题


I tried this example from the OpenCV website:

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

Executing the example, viz. python test.py, gives the following error:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    sift = cv2.SIFT()
AttributeError: 'module' object has no attribute 'SIFT'

I had installed OpenCV from source, building manually. All modules were built by make, if I recall correctly.

This question suggested that I install opencv-contrib from its GitHub repository. I did, and I still get this error.

My system is Ubuntu 15.04 64-bit.


回答1:


I'm not entirely sure if this is applicable, but at some point they stopped supporting SIFT in the later versions of opencv I believe due to the fact that it is patented or something related (source?), however an alternate is to use ORB which will have a similar effect.

You could try something like this:

from cv2 import ORB as SIFT

However in the event that you get an import error this also might work for you:

SIFT = cv2.ORB_create

If you insert those near the top of your file, then likely you can leave "SIFT" as it is throughout the file (well more or less, you get the idea, basically replace the cv2.Sift() with sift = SIFT() and you should be in better shape.)



来源:https://stackoverflow.com/questions/31257576/opencv-doesnt-come-with-external-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!