<module>() 中的 AttributeError Traceback最近一次调用<ipython-input-17-fd3d890ad0ef>

问题描述

我已经在 colab 上应用了 SIFT,通过使用以下代码获取旧的 Opencv 库来获取特征检测和描述:

!pip install opencv-python==3.4.2.16
!pip install opencv-contrib-python==3.4.2.16

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread(path+ 'Gibraltar1.jpg') # queryImage
img2 = cv.imread(path+ 'Gibraltar1.90.jpg') # trainImage

# Initiate SIFT detector
sift = cv.SIFT_create()

但是,我在使用 SIFT 之前尝试运行这些代码,它显示在下面错误消息:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-fd3d890ad0ef> in <module>()
      1 # Initiate SIFT detector
----> 2 sift = cv.SIFT_create()

AttributeError: module 'cv2.cv2' has no attribute 'SIFT_create'

解决方法

SIFT 位于 xfeatures2d 模块中,可以使用以下方法实例化:

import cv2
sift = cv2.xfeatures2d.SIFT_create(...)

有关移动的更多详细信息,请参阅 this reference

注意:这对 3.x 和 4.5.x 之间的 OpenCV 版本有效,但可能不适用于较新版本——SIFT 将移回主存储库 (details)。