SIFT

Open In Colab

cv2 sift function

We need a special variant of cv2 to run sift- because it's protected by copyrights (free for research purposes...)

pip uninstall opencv-python
pip install -U opencv-contrib-python==3.4.0.12

in this notebook the fix is done automatically in the next cell

In [1]:
# to run in google colab
import sys
if 'google.colab' in sys.modules:
    import subprocess 
    subprocess.call('apt-get install subversion'.split())
    subprocess.call('svn export https://github.com/YoniChechik/AI_is_Math/trunk/c_08_features/left.jpg'.split())
    subprocess.call('svn export https://github.com/YoniChechik/AI_is_Math/trunk/c_08_features/right.jpg'.split())
    subprocess.call('pip uninstall opencv-python'.split())
    subprocess.call('pip install -U opencv-contrib-python==3.4.0.12'.split())
In [2]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from random import randrange
figsize = (10,10)
In [3]:
rgb_l = cv2.cvtColor(cv2.imread("left.jpg"),cv2.COLOR_BGR2RGB)
gray_l = cv2.cvtColor(rgb_l,cv2.COLOR_RGB2GRAY)
rgb_r = cv2.cvtColor(cv2.imread("right.jpg"),cv2.COLOR_BGR2RGB)
gray_r = cv2.cvtColor(rgb_r,cv2.COLOR_RGB2GRAY)

SIFT feature detection and description

In [4]:
# use orb if sift is not installed
try:
    feature_extractor = cv2.xfeatures2d.SIFT_create()
except:
    feature_extractor = cv2.ORB()
 

# find the keypoints and descriptors with chosen feature_extractor
kp_l, desc_l = feature_extractor.detectAndCompute(gray_l,None)
kp_r, desc_r = feature_extractor.detectAndCompute(gray_r,None)

test=cv2.drawKeypoints(rgb_l,kp_l,None,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

plt.figure(figsize=figsize)
plt.imshow(test)
plt.title("keypoints")
plt.show()

take only unique features

In [5]:
bf = cv2.BFMatcher()
matches = bf.knnMatch(desc_l,desc_r, k=2)

# Apply ratio test
good_match = []
for m in matches:
    if m[0].distance/m[1].distance < 0.5:
        good_match.append(m)
good_match_arr = np.asarray(good_match)

# show only 30 matches
im_matches = cv2.drawMatchesKnn(rgb_l,kp_l,rgb_r,kp_r,good_match[0:30],None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

plt.figure(figsize=(20,20))
plt.imshow(im_matches)
plt.title("keypoints matches")
plt.show()