Skip to content

Instantly share code, notes, and snippets.

@soruly
Last active June 15, 2023 14:38
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save soruly/bd02a218690fe4e19295de3f5bede242 to your computer and use it in GitHub Desktop.
Save soruly/bd02a218690fe4e19295de3f5bede242 to your computer and use it in GitHub Desktop.
Compare two images using OpenCV and SIFT in python
import cv2
import sys
import os.path
import numpy as np
def drawMatches(img1, kp1, img2, kp2, matches):
rows1 = img1.shape[0]
cols1 = img1.shape[1]
rows2 = img2.shape[0]
cols2 = img2.shape[1]
out = np.zeros((max([rows1,rows2]),cols1+cols2,3), dtype='uint8')
out[:rows1,:cols1] = np.dstack([img1])
out[:rows2,cols1:] = np.dstack([img2])
for mat in matches:
img1_idx = mat.queryIdx
img2_idx = mat.trainIdx
(x1,y1) = kp1[img1_idx].pt
(x2,y2) = kp2[img2_idx].pt
cv2.circle(out, (int(x1),int(y1)), 4, (255, 0, 0, 1), 1)
cv2.circle(out, (int(x2)+cols1,int(y2)), 4, (255, 0, 0, 1), 1)
cv2.line(out, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), (255, 0, 0, 1), 1)
return out
def compare(filename1, filename2):
img1 = cv2.imread(filename1) # queryImage
img2 = cv2.imread(filename2) # trainImage
# Initiate SIFT detector
sift = cv2.SIFT()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.match(des1,des2)
matches = sorted(matches, key=lambda val: val.distance)
img3 = drawMatches(img1,kp1,img2,kp2,matches[:25])
# Show the image
cv2.imshow('Matched Features', img3)
cv2.waitKey(0)
cv2.destroyWindow('Matched Features')
if len(sys.argv) != 3:
sys.stderr.write("usage: compare.py <queryImageFile> <sourceImageFile>\n")
sys.exit(-1)
compare(sys.argv[1], sys.argv[2])
@amrithmmh
Copy link

is it possible to check if say a screenshot taken by webcam is same as the sample image/images using SIFT (i amreally new to image processing so i dont really know what is best for what)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment