Python 不能在 OpenCV 中使用 SURF、SIFT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18561910/
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-19 11:06:23  来源:igfitidea点击:

Can't use SURF, SIFT in OpenCV

pythonopencvsiftsurf

提问by Linda

I'm trying a simple thing like

我正在尝试一个简单的事情,比如

detector = cv2.SIFT()

and get this bad error

并得到这个糟糕的错误

detector = cv2.SIFT()
AttributeError: 'module' object has no attribute 'SIFT'

I do not understand that because cv2 is installed.

我不明白,因为安装了 cv2。

cv2.__version__is

cv2.__version__

$Rev: 4557 $

My system is Ubuntu 12.04.

我的系统是 Ubuntu 12.04。

Maybe someone has got the same problem and could help me.

也许有人遇到了同样的问题,可以帮助我。

EDIT:

编辑:

Long story short, testypypypy.py:

长话短说,testypypypy.py

import cv2

detector = cv2.SIFT()

ERROR:

错误:

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

If I take SURFit works because SURFis in dir(cv2)but if I also take cv2.BFMatcher()I get the same error... So it's missing and I have to add it but I don't know how.

如果我接受SURF它是有效的,因为SURF在,dir(cv2)但如果我也接受,cv2.BFMatcher()我会得到同样的错误......所以它丢失了,我必须添加它,但我不知道如何。

采纳答案by M?rten W

I think this is far from the "correct" way to do it (the "correct" way on Ubuntu seems to be to stick to a broken and/or outdated OpenCV), but for me building opencv-2.4.6.1 from source brings back cv2.SIFT and cv2.SURF.

我认为这远不是“正确”的方法(Ubuntu 上的“正确”方法似乎是坚持使用损坏和/或过时的 OpenCV),但对我来说,从源代码构建 opencv-2.4.6.1 带回来了cv2.SIFT 和 cv2.SURF。

Steps:

脚步:

  1. Download opencv-2.4.6.1.tar.gz from opencv.org.
  2. Extract the source:

    tar -xf opencv-2.4.6.1.tar.gz -C /tmp
    
  3. Configure the source. This will tell OpenCV to install into .opencv-2.4.6.1 in your home directory:

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D BUILD_PYTHON_SUPPORT=ON \
          -D WITH_XINE=ON \
          -D WITH_OPENGL=ON \
          -D WITH_TBB=ON \
          -D BUILD_EXAMPLES=ON \
          -D BUILD_NEW_PYTHON_SUPPORT=ON \
          -D WITH_V4L=ON \
          -D CMAKE_INSTALL_PREFIX=~/.opencv-2.4.6.1 \
          /tmp/opencv-2.4.6.1
    
  4. Build and install:

    cd /tmp/opencv-2.4.6.1
    make -j4
    make install
    
  5. Set PYTHONPATH (this works in bash, I have no clue about other shells):

    export PYTHONPATH=~/.opencv-2.4.6.1/lib/python2.7/dist-packages
    
  1. opencv.org下载 opencv- 2.4.6.1.tar.gz
  2. 提取源码:

    tar -xf opencv-2.4.6.1.tar.gz -C /tmp
    
  3. 配置源。这将告诉 OpenCV 安装到主目录中的 .opencv-2.4.6.1 中:

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D BUILD_PYTHON_SUPPORT=ON \
          -D WITH_XINE=ON \
          -D WITH_OPENGL=ON \
          -D WITH_TBB=ON \
          -D BUILD_EXAMPLES=ON \
          -D BUILD_NEW_PYTHON_SUPPORT=ON \
          -D WITH_V4L=ON \
          -D CMAKE_INSTALL_PREFIX=~/.opencv-2.4.6.1 \
          /tmp/opencv-2.4.6.1
    
  4. 构建和安装:

    cd /tmp/opencv-2.4.6.1
    make -j4
    make install
    
  5. 设置 PYTHONPATH(这在 bash 中有效,我对其他 shell 一无所知):

    export PYTHONPATH=~/.opencv-2.4.6.1/lib/python2.7/dist-packages
    

Now if I start python and import cv2 (for me, this produces a gnome-keyring warning), I have cv2.SIFT and cv2.SURF available.

现在,如果我启动 python 并导入 cv2(对我来说,这会产生 gnome-keyring 警告),我有 cv2.SIFT 和 cv2.SURF 可用。

回答by madan ram

Follow this installation operation

按照这个安装操作

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local
    -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON 
    -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON 
    -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

using this command will install library to your /usr/local/lib.

使用此命令会将库安装到您的 /usr/local/lib。

回答by Spechal

FYI, as of 3.0.0 SIFT and friends are in a contrib repo located at https://github.com/Itseez/opencv_contriband are not included with opencv by default.

仅供参考,从 3.0.0 开始,SIFT 和朋友位于https://github.com/Itseez/opencv_contrib的 contrib 存储库中,默认情况下不包含在 opencv 中。

回答by nmz787

since I had already compiled opencv when I discovered this problem, all I had to do was (from my opencv build directory):

由于我在发现这个问题时已经编译了 opencv,所以我所要做的就是(从我的 opencv 构建目录中):

make opencv_nonfree
sudo make install

回答by vizzy

for debian users its 'easy' to create their own libopencv-nonfree package.

对于 debian 用户来说,创建他们自己的 libopencv-nonfree 包很容易。

i followed the opencv tutorial for python, but in my debian the SIFT and SURF modules were missing. And there is no non-free package available for debian including SIFT and SURF etc.

我遵循了 python 的 opencv 教程,但是在我的 debian 中缺少 SIFT 和 SURF 模块。并且没有可用于 debian 的非免费软件包,包括 SIFT 和 SURF 等。

They were stripped from the package due to license issues....

由于许可证问题,它们被从包装中剥离了......

i never created a package for debian before (adding a new module etc) but i followed some small steps in the debian tutorials and tried and guessed around a bit, and after 1 day, voila... i got working a libopencv-nonfree2.4 deb package and a python module with correct bindings.

我之前从未为 debian 创建过一个包(添加新模块等),但我按照 debian 教程中的一些小步骤进行了尝试和猜测,1 天后,瞧……我开始使用 libopencv-nonfree2。 4 deb 包和一个具有正确绑定的 python 模块。

(i dont know if i also needed to install the newly built python-opencv package or only the nonfree... i re-installed both and got a working python opencv library with all necessary nonfree modules!)

(我不知道我是否还需要安装新构建的 python-opencv 包或只安装非自由包……我重新安装了两者并得到了一个包含所有必需非自由模块的工作 python opencv 库!)

ok, here it is:

好的,这里是:

!this is for libopencv 2.4!

!这是针对 libopencv 2.4 的!

!you can do all steps except installing as a normal user!

!除了作为普通用户安装之外,您可以执行所有步骤!

we need the built essesntials and some tools from debian repository to compile and create a new package:

我们需要构建的 essntials 和 debian 存储库中的一些工具来编译和创建一个新包:

sudo apt-get install build-essential fakeroot devscripts

create a directory in your home and change to that directory:

在您的家中创建一个目录并切换到该目录:

cd ~ && mkdir opencv-debian
cd opencv-debian

download the needed packages:

下载所需的软件包:

apt-get source libopencv-core2.4

and download all needed dependency packages to build the new opencv

并下载所有需要的依赖包来构建新的 opencv

apt-get build-dep libopencv-core2.4

this will download the neeeded sources and create a directory called "opencv-2.4.9.1+dfsg"

这将下载所需的源并创建一个名为“opencv-2.4.9.1+dfsg”的目录

change to that directory:

切换到该目录:

cd opencv-2.4.9.1+dfsg

now you can test if the package will built without modifications by typing:

现在您可以通过键入以下内容来测试包是否无需修改即可构建:

fakeroot debian/rules binary

this will take a long time! this step should finish without errors you now have a lot of .deb packages in your opencv-debian directory

这将需要很长时间!这一步应该没有错误地完成你现在在你的 opencv-debian 目录中有很多 .deb 包

now we make some modifications to the package definition to let debian buld the nonfree modules and package!

现在我们对包定义进行一些修改,让 debian 构建非自由模块和包!

change to the opencv-debian directory and download the correct opencv source.. in my case opencv 2.4.9 or so

切换到 opencv-debian 目录并下载正确的 opencv 源.. 在我的情况下是 opencv 2.4.9 左右

i got mine from https://github.com/Itseez/opencv/releases

我从https://github.com/Itseez/opencv/releases得到我的

wget https://codeload.github.com/Itseez/opencv/tar.gz/2.4.9

this will download opencv-2.4.9.tar.gz

这将下载 opencv-2.4.9.tar.gz

extract the archive:

提取存档:

tar -xzvf opencv-2.4.9.tar.gz

this will unpack the original source to a directory called opencv-2.4.9

这会将原始源代码解压到一个名为 opencv-2.4.9 的目录中

now copy the nonfree modules from original source to the debian source:

现在将非自由模块从原始源复制到 debian 源:

cp -rv opencv-2.4.9/modules/nonfree opencv-2.4.9.1+dfsg/modules/

ok, now we have the source of the nonfree modules, but thats not enough for debian... we need to modify 1 file and create a new one

好的,现在我们有了非自由模块的源代码,但这对 debian 来说还不够……我们需要修改 1 个文件并创建一个新文件

we have to edit the debian control file and add a new section at end of file: (i use mcedit as an editor here)

我们必须编辑 debian 控制文件并在文件末尾添加一个新部分:(我在这里使用 mcedit 作为编辑器)

mcedit opencv-2.4.9.1+dfsg/debian/control

or use any other editor of your choice

或使用您选择的任何其他编辑器

and add this section:

并添加此部分:

Package: libopencv-nonfree2.4
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: OpenCV Nonfree Modules like SIFT and SURF
 This package contains nonfree modules for the OpenCV (Open Computer Vision)
 library.
 .
 The Open Computer Vision Library is a collection of algorithms and sample
 code for various computer vision problems. The library is compatible with
 IPL (Intel's Image Processing Library) and, if available, can use IPP
 (Intel's Integrated Performance Primitives) for better performance.
 .
 OpenCV provides low level portable data types and operators, and a set
 of high level functionalities for video acquisition, image processing and
 analysis, structural analysis, motion analysis and object tracking, object
 recognition, camera calibration and 3D reconstruction.

now we create a new file called libopencv-nonfree2.4.install

现在我们创建一个名为 libopencv-nonfree2.4.install 的新文件

touch opencv-2.4.9.1+dfsg/debian/libopencv-nonfree2.4.install

and edit:

并编辑:

mcedit opencv-2.4.9.1+dfsg/debian/libopencv-nonfree2.4.install

and add the following content:

并添加以下内容:

usr/lib/*/libopencv_nonfree.so.*

ok, thats it, now create the packages again:

好的,就是这样,现在再次创建包:

cd opencv-2.4.9.1+dfsg

first a clean up:

首先清理:

fakeroot debian/rules clean

and build:

并构建:

fakeroot debian/rules binary

et voila... after a while you have a fresh built and a new package libopencv-nonfree2.4.deb!

等等……过了一会儿,你就有了一个全新的构建和一个新的包 libopencv-nonfree2.4.deb!

now install as root:

现在以 root 身份安装:

dpkg -i libopencv-nonfree2.4.deb
dpkg -i python-opencv.deb

and test!

和测试!

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('test.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

sift = cv2.SIFT()
kp = sift.detect(gray,None)
img=cv2.drawKeypoints(gray,kp)

corners = cv2.goodFeaturesToTrack(gray,16,0.05,10)
corners = np.int0(corners)

for i in corners:
    x,y = i.ravel()
    cv2.circle(img,(x,y),90,255,3)

plt.imshow(img),plt.show()

have fun!

玩得开心!

回答by Joe Futrelle

The approach suggested by vizzy also works with OpenCV 2.4.8, as when building the non-free package under Ubuntu 14.04 LTS.

vizzy 建议的方法也适用于 OpenCV 2.4.8,就像在 Ubuntu 14.04 LTS 下构建非自由包一样。

This dependency issue may prevent installation of the non-free package:

此依赖性问题可能会阻止安装非自由软件包:

 libopencv-nonfree2.4 depends on libopencv-ocl2.4; however:
  Package libopencv-ocl2.4 is not installed.

Easily fixable because the missing package can be installed from the ones just built:

很容易修复,因为可以从刚刚构建的包中安装丢失的包:

dpkg -i libopencv-ocl2.4_2.4.8+dfsg1-2ubuntu1_amd64.deb

After that the install proceeds as explained in vizzy's answer.

之后,安装将按照 vizzy 的回答中的说明进行。

回答by Stefan Savev

For recent information on this issue (as of Sept 2015) consult this page.

有关此问题的最新信息(截至 2015 年 9 月),请参阅此页面

Most information on this question here is obsolete.

此处有关此问题的大多数信息已过时。

What pyimagesearch is saying is that SURF/SIFT were moved to opencv_contribbecause of patent issues.

pyimagesearch 所说的是opencv_contrib由于专利问题,SURF/SIFT 被转移到了。

For installation there is also a nice pagethat tells you how to install opencv with opencv_contriband Python support so you get SURF/SIFT.

对于安装,还有一个很好的页面,它告诉您如何使用opencv_contribPython 支持安装 opencv,以便您获得 SURF/SIFT。

Notice that the API also changed. Now it's like this:

请注意,API 也发生了变化。现在是这样的:

sift = cv2.xfeatures2d.SIFT_create()

Before I found the above pages, I also suffered quite a bit. But the pages listed do a very good job of helping with installation and explaining what's wrong.

在找到以上几页之前,我也吃了不少苦头。但是列出的页面在帮助安装和解释错误方面做得非常好。

回答by Tripp Cannella

There is a pip source that makes this very easy.

有一个 pip 源可以使这变得非常容易。

  1. If you have another version of opencv-python installed use this command to remove it to avoid conflicts:

    pip uninstall opencv-python
    
  2. Then install the contrib version with this:

    pip install opencv-contrib-python
    
  3. SIFT usage:

    import cv2
    sift = cv2.xfeatures2d.SIFT_create()
    
  1. 如果您安装了另一个版本的 opencv-python,请使用此命令将其删除以避免冲突:

    pip uninstall opencv-python
    
  2. 然后用这个安装 contrib 版本:

    pip install opencv-contrib-python
    
  3. SIFT 用法:

    import cv2
    sift = cv2.xfeatures2d.SIFT_create()
    

回答by Tripp Cannella

  1. Install OpenCV-Contrib

  2. import cv2 sift = cv2.xfeatures2d.SIFT_create()

  3. sift.something()

  1. 安装 OpenCV-Contrib

  2. import cv2 sift = cv2.xfeatures2d.SIFT_create()

  3. sift.something()

This is the easy way to install the Contrib.

这是安装 Contrib 的简单方法。

回答by A.Ametov

As an Anaconda user, I wanted to find one or two appropriate commands to solve the problem. Fortunately, this answerhelped. For conda 4.5.11 (use conda -Vto check Anaconda version) I have performed next steps:

作为 Anaconda 用户,我想找到一两个合适的命令来解决问题。幸运的是,这个答案有所帮助。对于 conda 4.5.11(用于conda -V检查 Anaconda 版本),我执行了以下步骤:

# Python version does not matter, most likely, check yourself
conda create -n myenv python=3.6     
conda activate myenv
conda install -c menpo opencv

That will install OpenCV 2.4.11. Anaconda's another commandconda install -c menpo opencv3will install OpenCV3, but Python has to be downgraded to 2.7. To install OpenCV3 with Python3 use next (due to the first link):

这将安装 OpenCV 2.4.11。Anaconda 的另一个命令conda install -c menpo opencv3将安装 OpenCV3,但 Python 必须降级到 2.7。要使用 Python3 安装 OpenCV3,请使用 next(由于第一个链接):

conda create -n myenv python  
pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16

Check the SIFT:

检查 SIFT:

conda activate myenv
python
>>> cv2.xfeatures2d.SIFT_create()
<xfeatures2d_SIFT 000002A3478655B0>