简体   繁体   English

不能在 OpenCV 中使用 SURF、SIFT

[英]Can't use SURF, SIFT in OpenCV

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 SURF it works because SURF is 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它会起作用,因为SURFdir(cv2)但如果我也使用cv2.BFMatcher()我会得到同样的错误......所以它丢失了,我必须添加它,但我不知道如何添加。

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:如果您安装了另一个版本的 opencv-python,请使用此命令将其删除以避免冲突:

     pip uninstall opencv-python
  2. Then install the contrib version with this:然后用这个安装 contrib 版本:

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

     import cv2 sift = cv2.xfeatures2d.SIFT_create()

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_contrib because of patent issues. pyimagesearch 所说的是由于专利问题,SURF/SIFT 被转移到了opencv_contrib

For installation there is also a nice page that tells you how to install opencv with opencv_contrib and Python support so you get SURF/SIFT.对于安装,还有一个很好的页面,它告诉您如何使用opencv_contrib和 Python 支持安装 opencv,以便您获得 SURF/SIFT。

Notice that the API also changed.请注意,API 也发生了变化。 Now it's like this:现在是这样的:

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.但是列出的页面在帮助安装和解释错误方面做得非常好。

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

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 .opencv.org下载 opencv- 2.4.6.1.tar.gz
  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:这将告诉 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. 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):设置 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 可用。

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.我遵循了 python 的 opencv 教程,但是在我的 debian 中缺少 SIFT 和 SURF 模块。 And there is no non-free package available for debian including SIFT and SURF etc.并且没有可用于 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!玩得开心!

IN opencv3.x SIFT() & SURF() are no longer exist .for this在 opencv3.x 中,SIFT() 和 SURF() 不再存在。为此

uninstall all the opencv versions卸载所有 opencv 版本

python -m pip uninstall opencv-python

python -m pip uninstall opencv-contrib-python

after that install opencv-contrib to include sift() and surf() using below given command with python(3.x)之后安装 opencv-contrib 以包含 sift() 和 surf() 使用下面给定的命令与 python(3.x)

python -m pip install opencv-contrib-python==3.4.2.16

then you can use然后你可以使用

sift = cv2.xfeatures2d.SIFT_create()

As an Anaconda user, I wanted to find one or two appropriate commands to solve the problem.作为 Anaconda 用户,我想找到一两个合适的命令来解决问题。 Fortunately, this answer helped.幸运的是,这个答案有所帮助。 For conda 4.5.11 (use conda -V to check Anaconda version) I have performed next steps:对于 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.这将安装 OpenCV 2.4.11。 Anaconda's another command conda install -c menpo opencv3 will install OpenCV3, but Python has to be downgraded to 2.7. Anaconda 的另一个命令conda install -c menpo opencv3将安装 OpenCV3,但 Python 必须降级到 2.7。 To install OpenCV3 with Python3 use next (due to the first link):要使用 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>

just change SHIFT to ORB , I think it make occur because of non-relevant version, ORB is efficient and better alternative of SHIFT or SURF .只需将SHIFT更改为ORB ,我认为它会因为不相关的版本而发生, ORBSHIFTSURF 的有效替代品。

As I also face same problem when i was used cv2.SHIFT()因为我在使用 cv2.SHIFT() 时也面临同样的问题

ERROR: AttributeError: 'module' object has no attribute 'SIFT'错误:AttributeError:“模块”对象没有属性“SIFT”

Now its completely working for me please try this:现在它完全适合我,请试试这个:

ORB = cv2.ORB()

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

Install Python opencv安装 Python opencv

pip install opencv-python

and instead of using ..而不是使用 ..

cv2.SIFT() cv2.SIFT()

Use

cv2.SIFT_create() cv2.SIFT_create()

working code using opencv-python below下面使用opencv-python 的工作代码

import cv2
img1 = cv2.imread('yourimg.png',0)
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1,None) #keypoint and descriptors
...

you can also install "opencv-contrib-python" and use "cv2.xfeatures2d.SIFT_create()" but that is secondary and up to you.. working code using the python package opencv-contrib-python您还可以安装“opencv-contrib-python”并使用“cv2.xfeatures2d.SIFT_create()”但这是次要的,由您决定..使用python包opencv-contrib-python的工作代码

import cv2
img1 = cv2.imread('yourimg.png',0)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1,None) #keypoint and descriptors

Thanks谢谢

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。

  1. Install OpenCV-Contrib安装 OpenCV-Contrib

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

  3. sift.something()

This is the easy way to install the Contrib.这是安装 Contrib 的简单方法。

只需将 opencv 的版本更改为 3.4.2.16 。由于它已获得专利,因此在较新版本中不可用。

You can proceed in this way.您可以通过这种方式进行。 It must work for you as well!它也必须为你工作!

Step 1:第1步:

virtualenv venv 
source venv/bin/activate

Step 2:第2步:

sudo python3 -m pip install opencv-python==3.4.2.16 
sudo python3 -m pip install opencv-contrib-python==3.4.2.16

Step 3:第 3 步:

import cv2
sift = cv2.xfeatures2d.SIFT_create()

Don't use cv2.SIFT() .不要使用 cv2.SIFT() 。 It will raise an exception.它会引发异常。

This is what worked for me in September 2020:这就是 2020 年 9 月对我有用的方法:

  1. Remove previous versions:删除以前的版本:
     pip uninstall opencv-python pip uninstall opencv-contrib-python
  2. Install this particular version (3.4.2.16 was no longer available)安装此特定版本(3.4.2.16 不再可用)
     pip install opencv-python==3.4.2.17 pip install opencv-contrib-python==3.4.2.17
  3. Create the detectors with:使用以下命令创建检测器:
     import cv2 sift = cv2.xfeatures2d.SIFT_create() surf = cv2.xfeatures2d.SURF_create()

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 的回答中的说明进行。

None of the above suggested solutions worked for me.以上建议的解决方案都不适合我。 I use Anaconda and found that opencv version 3.3.1 still had Sift enabled.我使用 Anaconda,发现 opencv 3.3.1 版仍然启用了 Sift。 If you want to test in isolated conda environment, try the following inspired from @A.Ametov's answer above如果您想在隔离的 conda 环境中进行测试,请尝试以下灵感来自上面@A.Ametov 的回答

conda create -n testenv opencv=3.3.1     
conda activate testenv

conda activate myenv
python
#Check version of opencv being used
>>> import cv2
>>> cv2.__version__
#Check if Sift is available
>>> cv2.xfeatures2d.SIFT_create()
<xfeatures2d_SIFT 000002A3478655B0>

Change this :改变这一点

sift = cv2.xfeatures2d.SIFT_create()

By this :通过这个

cv2.ORB_create()

The following page provides a relatively good guide that requires few corrections : https://cv-tricks.com/how-to/installation-of-opencv-4-1-0-in-windows-10-from-source/以下页面提供了一个相对较好的指南,几乎不需要更正: https : //cv-tricks.com/how-to/installation-of-opencv-4-1-0-in-windows-10-from-source/

On step 8, when you are choosing generator for the project (tool used for building), don't forget to specify x64 in the second field if you need it.在第 8 步中,当您为项目选择生成器(用于构建的工具)时,如果需要,请不要忘记在第二个字段中指定 x64。 If you don't, you will get LNK1112 error, which is a linker error caused if one module is created with the x64 compiler, and another module is created with the x86 compiler.如果你不这样做,你会得到 LNK1112 错误,这是一个链接器错误,如果一个模块是用 x64 编译器创建的,另一个模块是用 x86 编译器创建的。

Next, when choosing flags in step 9, don't forget to tick the "OPENCV_ENABLE_NONFREE", and set the path for flag "OPENCV_EXTRA_MODULES_PATH" flag.接下来,在步骤 9 中选择标志时,不要忘记勾选“OPENCV_ENABLE_NONFREE”,并设置标志“OPENCV_EXTRA_MODULES_PATH”标志的路径。 The path must be set to "modules" folder in opencv-contrib-python.路径必须设置为 opencv-contrib-python 中的“modules”文件夹。

So you need both opencv, and opencv-contrib-python in order to use SIFT and SURF.所以你需要 opencv 和 opencv-contrib-python 才能使用 SIFT 和 SURF。

As per June 2020, I suppose this version ( pip install -U opencv-contrib-python==3.4.2.16 ) is still working.截至 2020 年 6 月,我认为此版本( pip install -U opencv-contrib-python==3.4.2.16 )仍在运行。 so install it and enjoy.所以安装它并享受。

It is now back to normal opencv, thanks to the expiration of the patent!现在已经恢复正常opencv了,感谢专利到期! Check this: https://docs.opencv.org/4.5.4/da/df5/tutorial_py_sift_intro.html As long as your opencv version is 4.4.0+!检查这个: https ://docs.opencv.org/4.5.4/da/df5/tutorial_py_sift_intro.html 只要你的opencv版本是4.4.0+!

try this尝试这个

!pip install opencv-contrib-python==4.4.0.44 sift = cv2.SIFT_create() !pip install opencv-contrib-python==4.4.0.44 sift = cv2.SIFT_create()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM