无法在MacO上使用opencv运行Sift [英] Cannot run Sift with opencv on MacOs

查看:182
本文介绍了无法在MacO上使用opencv运行Sift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行cv2.xfeatures2d.SIFT_create()

我收到此错误消息:

error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv_contrib/modules/xfeatures2d/src/sift.cpp:1210: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'

说要设置OPENCV_ENABLE_NONFREE,但我已使用已经具有该选项的自制软件安装了opencv 已启用.

saying to set OPENCV_ENABLE_NONFREE but I installed opencv with homebrew that has that option already enabled.

我尝试使用pip install opencv-contrib-python-nonfree并收到此错误

edit: I tried with pip install opencv-contrib-python-nonfree and I get this error

ERROR: Could not find a version that satisfies the requirement opencv-contrib-python-nonfree (from versions: none)
ERROR: No matching distribution found for opencv-contrib-python-nonfree

推荐答案

我能够重建您的错误,并且能够解决此问题.请仔细查看安装OpenCV时使用的Homebrew的Python公式: https://formulae.brew.sh /formula/opencv .在撰写本文时,它使用的是python@3.8,这意味着它使用Homebrew的Python 3.8而不是默认的Python 3.7.7(在撰写本文时),如果您使用Homebrew可以将其放置在/usr/local/bin/python3中安装Python的标准公式.因为从Homebrew安装OpenCV后找不到它,所以您尝试使用pip来安装它. opencv-contrib-python公式未启用非自由模块,并且您在上面使用的试图获取非自由模块的公式已过时.

I was able to reconstruct your error and I was able to fix this. Have a careful look at the Python formula for Homebrew used when installing OpenCV: https://formulae.brew.sh/formula/opencv. At the time of this writing, it is using python@3.8, meaning that it uses Homebrew's Python 3.8 instead of the default Python 3.7.7 (at the time of this writing) that would be located in /usr/local/bin/python3 if you used Homebrew to install the standard formula for Python. Because you could not find OpenCV after you installed it from Homebrew, you tried to use pip to install it. The opencv-contrib-python formula does not have the non-free modules enabled and the formula you are using above to try and get the non-free modules is obsolete.

因此,当您使用Homebrew安装OpenCV时,会出现上述错误,因为即使通过Homebrew使用contrib软件包和Python包装程序安装了OpenCV,您仍在使用系统提供的pip来安装OpenCV.但您没有使用为您安装的Homebrew.具体来说,您并没有使用与OpenCV一起安装的实际Python版本.

Therefore, when you use Homebrew to install OpenCV, you are getting the above error because even though you installed OpenCV with the contrib packages and with the Python wrappers through Homebrew, you are using pip provided from your system to install OpenCV but you're not using what Homebrew installed for you. Specifically, you're not using the actual version of Python installed for use with OpenCV.

在我的系统上,可以在以下位置找到此版本的Python 3.8:

On my system, this version of Python 3.8 can be found here:

/usr/local/Cellar/python@3.8/3.8.2/bin

在导入OpenCV之前,我还必须确保已安装numpy.导航到上面的目录,然后在此目录中本地运行pip:

I also had to make sure numpy was installed prior to importing OpenCV. Navigate to the above directory, then run pip locally in this directory:

$ cd /usr/local/Cellar/python@3.8/3.8.2/bin
$ ./pip install numpy

完成后,运行此本地版本的python3,然后尝试导入OpenCV和SIFT模块:

Once you're done, run this local version of python3, then try importing OpenCV and the SIFT module:

$ ./python3
Python 3.8.2 (default, Mar 11 2020, 00:29:50)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.xfeatures2d.SIFT_create()
<xfeatures2d_SIFT 0x10b045550>
>>>


如果您决定从Homebrew使用非免费模块,则必须继续使用Homebrew的Python 3.8,以便将非免费模块用于OpenCV.如果要进一步开发并确保系统使用的是此版本的Python,而不是/usr/local/bin/python3中的Python 3.7,则必须更新位于主目录中的.bashrc文件,以便该版本的使用Python代替那里的Python:


Moving forward, you'll have to use Python 3.8 from Homebrew in order to use the non-free modules for OpenCV if you decide to use this from Homebrew. If you want to go further and make it so that your system is using this version of Python and not Python 3.7 found in /usr/local/bin/python3, you'll have to update your .bashrc file located in your home directory so that this version of Python is used instead of the one there:

export PATH="/usr/local/Cellar/python@3.8/3.8.2/bin:$PATH"

如果您更喜欢将pip公式用于opencv-contrib-python,并坚持使用/usr/local/bin/python3中的Python版本,那么非免费模块将不附带此功能,因此您必须为该工作克隆存储库并在启用了这些功能的情况下重建包装器(来源: https://github. com/skvark/opencv-python/issues/126#issuecomment-596689259 ).

If you prefer to use the pip formula for opencv-contrib-python and stick with the Python version in /usr/local/bin/python3, the non-free modules are not accompanied with this so you'll have to clone the repo for this work and rebuild the wrapper with these enabled (source: https://github.com/skvark/opencv-python/issues/126#issuecomment-596689259).

需要注意的一点是,我必须手动指向计算机上Qt的安装位置.最初设置OpenCV在我的命令行路径中找不到Qt.我先做了brew install qt.您必须在CMAKE_PREFIX_PATH环境变量中指定此路径. 最后,假设您将存储库克隆到您的Downloads目录中:

A minor note that I had to manually point to where Qt was installed on my computer. Setting up OpenCV initially could not find Qt in my command-line path. I did brew install qt first. You'll have to specify the path to this in the CMAKE_PREFIX_PATH environment variable. Finally, assuming you will clone the repo into your Downloads directory:

brew install qt
cd ~/Downloads
git clone --recursive https://github.com/skvark/opencv-python.git
cd opencv-python
export CMAKE_ARGS="-DOPENCV_ENABLE_NONFREE=ON -DENABLE_CONTRIB=1 -DOPENCV_EXTRA_MODULES_PATH=/Users/<User>/Downloads/opencv-python/opencv_contrib/modules/ -DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt/5.14.2/"
python3 setup.py build
python3 setup.py install

上面的

5.14.2是我在计算机上安装的Qt的版本.确保将其更改为计算机上安装的任何版本. 同样,将<User>替换为您在其下登录的计算机的用户名.您可以通过在终端中执行cd ~来找到它,并通过执行pwd来查看目录分隔符之后的最终字符串是什么.

5.14.2 above is the version of Qt that I have on my computer. Make sure to change this to whatever version of it is installed on your computer. Also replace <User> with the user name of your machine that you've logged in under. You can find this by doing cd ~ in the Terminal, and seeing what the final string is after the directory separator by doing pwd.

由于SIFT的专利现已过期,所以我希望将其从非免费软件包中删除,并将成为OpenCV主库的一部分!

Since SIFT's patent has now expired, I'm hoping that this will be removed from the nonfree package and will be part of the main OpenCV library!

这篇关于无法在MacO上使用opencv运行Sift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆