Python 将模块从 opencv_contrib 添加到 OpenCV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26059134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:59:18  来源:igfitidea点击:

Adding modules from opencv_contrib to OpenCV

pythonopencv

提问by Robbert

I'm trying to add the xfeatures2dmodule from opencv_contribto an existing OpenCV/Python project.

我正在尝试将xfeatures2d模块添加opencv_contrib到现有的 OpenCV/Python 项目中。

I've downloaded the latest version of the module from the repo, and built OpenCV again with the following additional params:

我已经从repo下载了最新版本的模块,并使用以下附加参数再次构建了 OpenCV:

OPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib-master/modules
BUILD_opencv_xfeatures2d=ON

Excerpt from build log:

构建日志摘录:

-- Installing: /usr/local/lib/python2.7/site-packages/cv2.so
-- Installing: /usr/local/lib/python3.4/site-packages/cv2.so
-- Installing: /usr/local/lib/libopencv_xfeatures2d.3.0.0.dylib

It appears the new module is installed correctly. I'm able to import cv2in both Python versions. However neither recognise the new features the module is supposed to add.

看来新模块安装正确。我可以cv2在两个 Python 版本中导入。但是,两者都无法识别模块应该添加的新功能。

>>> cv2.SURF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SURF'
>>> cv2.xfeatures2d.SURF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'xfeatures2d'

采纳答案by Sovtek

I encountered this same issue. I'm using python 2.7.6 and OpenCv 3.0 with the additional non-free modules. I do have xfeatures2d present in available modules and can import it, however it was as though xfeatures2d didn't contain SIFT or SURF. No matter how I called them it was the same Error:

我遇到了同样的问题。我将 python 2.7.6 和 OpenCv 3.0 与其他非免费模块一起使用。我确实在可用模块中存在 xfeatures2d 并且可以导入它,但是好像 xfeatures2d 不包含 SIFT 或 SURF。无论我如何称呼它们,都是相同的错误:

"AttributeError: 'module' object has no attribute 'SIFT'

“AttributeError: 'module' 对象没有属性 'SIFT'

I tried the different name spaces suggested, and only recently noticed this detail and GOT IT WORKING!

我尝试了建议的不同名称空间,直到最近才注意到这个细节并且开始工作了!

$ python

>>>import cv2

>>>help(cv2.xfeatures2d)

$蟒蛇

>>>导入cv2

>>>帮助(cv2.xfeatures2d)

You'll notice that it replies that it is now referred to as...

你会注意到它回答说它现在被称为......

FUNCTIONS

SIFT_create(...)

职能

SIFT_create(...)

and

SURF_create(...)
SURF_create(...)

So very simply - the namespace is NOT"cv2.SIFT()" or "cv2.xfeatures2d.SIFT" but rather

非常简单 - 命名空间不是“cv2.SIFT()”或“cv2.xfeatures2d.SIFT”而是

cv2.xfeatures2d.SIFT_create()

cv2.xfeatures2d.SIFT_create()

Please give it a shot!

请试一试!

回答by berak

you are missing the new, additional namespace:

您缺少新的附加命名空间:



>>> help(cv2.xfeatures2d)
Help on module cv2.xfeatures2d in cv2:

NAME
    cv2.xfeatures2d

FILE
    (built-in)

FUNCTIONS
    SIFT(...)
        SIFT([, nfeatures[, nOctaveLayers[, contrastThreshold[, edgeThreshold[,
sigma]]]]]) -> <xfeatures2d_SIFT object>

    SURF(...)
        SURF([hessianThreshold[, nOctaves[, nOctaveLayers[, extended[, upright]]
]]]) -> <xfeatures2d_SURF object>

    StarDetector(...)
        StarDetector([, _maxSize[, _responseThreshold[, _lineThresholdProjected[
, _lineThresholdBinarized[, _suppressNonmaxSize]]]]]) -> <xfeatures2d_StarDetect
or object>

DATA
    FREAK_NB_ORIENPAIRS = 45
    FREAK_NB_PAIRS = 512
    FREAK_NB_SCALES = 64


>>> surf = cv2.xfeatures2d.SURF(300)

回答by Jprog

Another possibility (and the easiest one I found!) is to install the 2.4.9 release which already include SIFT and SURF algorithm. You just have to do then

另一种可能性(也是我发现的最简单的一种!)是安装已经包含 SIFT 和 SURF 算法的 2.4.9 版本。你只需要这样做

import cv2
sift = cv2.SIFT()
(...)

回答by gtzinos

Install it from pip

从 pip 安装

Python 2.x

蟒蛇 2.x

pip install opencv-contrib-python

Python 3.x

蟒蛇 3.x

pip3 install opencv-contrib-python

Use sudo if a permsision error occurred.

如果发生权限错误,请使用 sudo。