Skip to content

Beginners Opencv · Tutorials

Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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()

18 comments

    1. 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.

  1. 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'”

  2. “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

  3. 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!

    1. 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.

  4. 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

Join the discussion