We’re going to learn in this tutorial how to find features on an image.
We have thre different algorythms that we can use:
- SIFT
- SURF
- ORB
Each one of them as pros and cons, it depends on the type of images some algorithm will detect more features than another.
SIFT and SURF are patented so not free for commercial use, while ORB is free.SIFT and SURF detect more features then ORB, but ORB is faster.
First we import the libraries and load the image:
import cv2
import numpy as np
img = cv2.imread("the_book_thief.jpg", cv2.IMREAD_GRAYSCALE)
We then load one by one the three algorythms.
sift = cv2.xfeatures2d.SIFT_create() surf = cv2.xfeatures2d.SURF_create() orb = cv2.ORB_create(nfeatures=1500)
We find the keypoints and descriptors of each spefic algorythm.
A keypoint is the position where the feature has been detected, while the descriptor is an array containing numbers to describe that feature.
When the descriptors are similar, it means that also the feature is similar. You can see this tutorial to understand more about feature matching.
keypoints_sift, descriptors = sift.detectAndCompute(img, None) keypoints_surf, descriptors = surf.detectAndCompute(img, None) keypoints_orb, descriptors = orb.detectAndCompute(img, None)
We finally draw the keypoints on the image. In this case we are drawing only the keypoints detected from the orb algorythm.
img = cv2.drawKeypoints(img, keypoints, None)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
Could this work with mechanical parts such as screws or nut bolts? Will this detect the difference in length if all other features are the same?
It could only work it the screws are in the exact same position and also the camera is always the same, otherwise It won’t, you should use more advanced method to make such detection.
Hello, How can I apply this on a person inside a video?
How to get SIFT and SURF features in opencv 4.1.0 (linux)? I get this error while using: cv2.xfeatures2d.SIFT_create()
“This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function ‘create'”
https://stackoverflow.com/a/52514095 – Build OpenCV with that flag on in the cmake file or use the version of OpenCV where they had the NONFREE flag defined wrong for the SIFT algorithm:
pip install opencv-python==3.4.2.17
pip install opencv-contrib-python==3.4.2.17
You need to have opencv with contrib compiled by cmake for example
a lot of thanks
“This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function ‘create’”
Solution:
In Command line(pycharm ,anaconda or any terminal),
$ pip uninstall opencv-python
$ pip uninstall opencv-contrib-python
then,
$ pip install opencv-contrib-python==3.4.2.16
$ pip install opencv-python==3.4.2.16
and this may require(I am not sure)
CMake -DOPENCV_ENABLE_NONFREE:BOOL=ON -DOPENCV_EXTRA_MODULES_PATH=.your_path/opencv_contrib/modules
Hello, how do I detect the contour of a car license plate? Do you have to train many and / or different types of license plates? Regards
thanks a lot
hello i tried to use this code for sift,surf and orb but it’s not working
I tried a many things to get the app run , but I always get to the same error:
sift = cv2.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’
I checked the OPENCV_ENABLE_NONFREE true and compiled it several times, no success. Too bad!
Hi Hans,
You can solve this problem by installing an olter version of Opencv which has the SIFT enabled.
You can install Opencv contrib 3.4.2.16 and it will work.
Thanks a lot for the quick response. I let you know if this works for my project.
What python version goes along with this Opencv contrib the best?
It works! I am using python 3.7.7. everything goes well!
Thanks again !
i tried to execute the code in colab , but getting error at sift = cv2.xfeatures2d.SIFT_create(), that create() not found
Colleagues good afternoon, the same problem with me.
error: OpenCV(4.1.2) /io/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 ‘create’ – on google colab. What the solution? Thanks