PyCharm环境下OpenCV非免费模块(SIFT、SURF)无法使用求助
Hey there, let's troubleshoot why you can't access SIFT/SURF functions even after installing those OpenCV packages. Here are targeted fixes to try step by step:
opencv-python and opencv-contrib-python match exactly in version The most common issue here is mismatched versions between the main OpenCV package and its contrib extension. Since you're targeting OpenCV 3.4.1, you need to install the exact corresponding pip package versions (note the trailing .15 suffix—this is the correct package version for 3.4.1 on PyPI):
- First, uninstall any existing mismatched versions via PyCharm's「文件-设置-项目解释器」, or run this in your terminal (if using a virtual environment):
pip uninstall opencv-python opencv-contrib-python -y - Then install the correct 3.4.1-compatible packages:
pip install opencv-python==3.4.1.15 opencv-contrib-python==3.4.1.15
These patented features live in the cv2.xfeatures2d submodule (not directly under cv2). Your code should look like this:
import cv2 # Initialize SIFT detector sift_detector = cv2.xfeatures2d.SIFT_create() # Initialize SURF detector (note: SURF is patented—ensure your use case allows this) surf_detector = cv2.xfeatures2d.SURF_create()
It's easy to accidentally use a different Python environment than the one where you installed the packages:
- Go back to「文件-设置-项目解释器」and verify that both
opencv-python==3.4.1.15andopencv-contrib-python==3.4.1.15appear in the installed packages list. - If you're using a virtual environment, confirm that PyCharm is pointing to that environment's Python executable, not the system-wide one.
Ubuntu 16.04 might lack some low-level libraries needed for OpenCV's contrib modules to load properly. Install these via terminal:
sudo apt-get update && sudo apt-get install libopencv-dev libopencv-contrib-dev -y
If you still hit issues, test directly in PyCharm's terminal to debug:
import cv2 print(cv2.__version__) # Should output 3.4.1 print(dir(cv2.xfeatures2d)) # Look for SIFT_create/SURF_create in the output
If those functions aren't listed, recheck your package versions—they're likely still mismatched.
内容的提问来源于stack exchange,提问作者Winbuntu

