在Google Colaboratory中调用cv2.xfeatures2d_SURF报错,原因何在?
Hey there, let's break down why this error is popping up even though you've confirmed opencv-python and opencv-contrib-python are installed in Google Colab:
问题根源
The SURF algorithm (along with similar ones like SIFT) is patent-protected. Starting from OpenCV 4.x, the default opencv-contrib-python package no longer includes these patented computer vision algorithms to avoid licensing conflicts.
Google Colab typically pre-installs newer OpenCV versions (4.x+), so even though you see "Requirement already satisfied" for opencv-contrib-python, it's the free, restricted version that excludes xfeatures2d_SURF.
解决步骤
You have two reliable fixes to get SURF working:
方法1:降级到包含SURF的OpenCV 3.x版本
First uninstall the current OpenCV packages, then install specific 3.x versions that still include the patented algorithms:
!pip uninstall -y opencv-python opencv-contrib-python !pip install opencv-python==3.4.2.16 opencv-contrib-python==3.4.2.16
方法2:安装非免费的contrib包(适配OpenCV 4.x)
If you want to stick with OpenCV 4.x, you can install the non-free variant that includes these patented modules:
!pip uninstall -y opencv-python opencv-contrib-python !pip install opencv-contrib-python-nonfree
验证修复
After installation, test if SURF loads correctly by running this snippet:
import cv2 surf = cv2.xfeatures2d.SURF_create() print("SURF loaded successfully!")
内容的提问来源于stack exchange,提问作者user2963717

