问题
I am using OpenCV v4.20 and PyCharm IDE. I want to use SIFT algorithm. But I get this error. I looked for solutions of this error on the internet but none of them did help me. Do you know the solution of this error? (With pip i can install at least 3.4.2.16 version of OpenCV)
Here is my error:
Traceback (most recent call last): File "C:/Users/HP/PycharmProjects/features/featuredetect.py", line 7, in sift = cv.xfeatures2d_SIFT.create()
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1210: 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'
Here is my code:
import cv2 as cv
image = cv.imread("the_book_thief.jpg")
gray_image = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
sift = cv.xfeatures2d_SIFT.create()
keyPoints = sift.detect(image,None)
output = cv.drawKeypoints(image,keyPoints,None)
cv.imshow("FEATURES DETECTED",output)
cv.imshow("NORMAL",image)
cv.waitKey(0)
cv.destroyAllWindows()
回答1:
Unfortunately, according to this Github issue, SIFT no longer available in opencv > 3.4.2. Since you're using OpenCV v4.2.0, it's not included even if you have installed pip install opencv-contrib-python
as shown in this post. A work around is to downgrade to any previous OpenCV version that includes SIFT (I believe any version below 3.4.3
). I've been successful when downgrading to v3.4.2.16
.
pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16
Using your code with v3.4.2.16
, SIFT seems to work
回答2:
The solution to your problem should be installing opencv-python-nonfree (available via pip).
As the error states SIFT is patented and therefore not included into OpenCV for license reasons. It's included in the nonfree part.
来源:https://stackoverflow.com/questions/60065707/cant-use-sift-in-opencv-v4-20