Skip to content

Instantly share code, notes, and snippets.

@phondanai
Created May 28, 2018 03:38
Show Gist options
  • Save phondanai/de9bde4a7062c79732df6a56156f8324 to your computer and use it in GitHub Desktop.
Save phondanai/de9bde4a7062c79732df6a56156f8324 to your computer and use it in GitHub Desktop.
Extract SIFT from image using OpenCV
import cv2 # pip install opencv-contrib-python
import numpy as np # pip install numpy
# load image
img = cv2.imread('/path/to/img.jpg')
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# extract SIFT feature from grayscale image
sift = cv2.xfeatures2d.SIFT_create()
# sift.detect() function finds the keypoint in the images
kp = sift.detect(gray, None)
# draws the small circles on the locations of keypoints
draw_kp = cv2.drawKeypoints(gray, kp, img, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# save image to file
cv2.imwrite('/path/to/img_with_sift.jpg', draw_kp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment