You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Google Colaboratory中调用cv2.xfeatures2d_SURF报错,原因何在?

解决AttributeError: module 'cv2.cv2' has no attribute '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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:55:57