Basic SIFT in Python

I’ve been having a quick play with Scale-Invariant Feature Transform (SIFT) in Python. I had a few problems installing it (see here). In short SIFT finds the features of an image, a more detailed explanation can be seen here.

In this example we take a picture of the City Hall in Hull and run it through a simple implementation of SIFT to extract the features of the image.

We’ll use this image, mine is called ‘Proj4_img00000021.jpg’. Click on the image to get the full size version

Proj4_img00000021

First lets bring in the packages we need. We don’t really need Numpy or Pandas but its just habbit.

Next we need to load the image and display it. Remember, the file should be in the same folder as your Python or Notebook script.

This should show this…

OpenCV_Original

Now lets converting it to gray scale.

Giving us this (colors are from the plt.imshow(), trust me its grayscale).

OpenCV_gray

Now the SIFT bits….

Notes

Some examples online say cv2.SIFT() instead of cv2.xfeatures2d.SIFT_create(). Apparently SIFT_create() was moved to xfeatures2d because of a patent issue, I don’t know if this is true or not, I just know I had to use cv2.xfeatures2d.SIFT_create(). Also cv2.drawKeypoints(gray, kp, None, ….) insisted on a 3rd parameter, so I just passed it None which in python language is the same as Null.

When you open up the image you should see this…

OpenCV_SIFT_KP

As you can see, all of the key points have been detected.