问题
When I try to run cv2.xfeatures2d.SIFT_create()
I get this error message:
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'
saying to set OPENCV_ENABLE_NONFREE but I installed opencv with homebrew that has that option already enabled.
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
回答1:
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.
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.
On my system, this version of Python 3.8 can be found here:
/usr/local/Cellar/python@3.8/3.8.2/bin
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
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>
>>>
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"
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).
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
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
.
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!
来源:https://stackoverflow.com/questions/61597130/cannot-run-sift-with-opencv-on-macos