Python2.7下opencv3.X版本不能使用sift,surf算法问题解决

问题没解决前环境配置:Anaconda2(Python 2.7.15)、opencv3.20

问题1AttributeError: 'module' object has no attribute 'SIFT'或'SURF'

原因:opencv3.x将SIFT等算法整合到xfeatures2d集合里面了

解决sift = cv2.SIFT()     更写成:sift = cv2.xfeatures2d.SIFT_create()    //相同方法调用SURF

问题2'module' object has no attribute 'xfeatures2d'

原因:没有配置opencv_contrib,需要导入opencv_contrib

解决:命令提示符下:pip install opencv-contrib-python

再查看OpenCV版本:

import cv2
cv2.__version__
Out[3]: '3.4.2'

注:opencv-contrib-python3.4.2以上的版本用不了

会报错:

cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207: 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 ‘cv::xfeatures2d::SIFT::create’

# -*-coding:utf-8-*-
import cv2
import numpy as np

img=cv2.imread('ZED.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift=cv2.xfeatures2d.SIFT_create()
kp=sift.detect(gray,None)
img=cv2.drawKeypoints(gray,kp,img)
cv2.imwrite('sift_keypoints.jpg',img)

效果图:Python2.7下opencv3.X版本不能使用sift,surf算法问题解决_第1张图片

 

你可能感兴趣的:(Python2.7下opencv3.X版本不能使用sift,surf算法问题解决)