Skip to content

Instantly share code, notes, and snippets.

@saikatbsk
Created August 11, 2018 18:09
Show Gist options
  • Save saikatbsk/42617b06add82f20d5eea50cccd9547b to your computer and use it in GitHub Desktop.
Save saikatbsk/42617b06add82f20d5eea50cccd9547b to your computer and use it in GitHub Desktop.
from __future__ import print_function
import cv2
import math
import numpy as np
PI = math.pi
MAX_FEATURES = 500
GOOD_MATCH_PERCENT = 0.15
def alignImages(im1, im2, windowName):
# Convert images to grayscale
im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
# Detect ORB features and compute descriptors.
orb = cv2.ORB_create(MAX_FEATURES)
keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)
keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)
# Match features.
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors1, descriptors2, None)
# Sort matches by score
matches.sort(key=lambda x: x.distance, reverse=False)
# Remove not so good matches
numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]
# Draw top matches
imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)
cv2.imshow(windowName, imMatches)
# Extract location of good matches
points1 = np.zeros((len(matches), 2), dtype=np.float32)
points2 = np.zeros((len(matches), 2), dtype=np.float32)
for i, match in enumerate(matches):
points1[i, :] = keypoints1[match.queryIdx].pt
points2[i, :] = keypoints2[match.trainIdx].pt
# Find homography
h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
# Use homography
height, width, channels = im2.shape
im1Reg = cv2.warpPerspective(im1, h, (width, height))
return im1Reg, h
def drawInclination(theta):
canvasWidth = 200
canvas = np.zeros((canvasWidth, canvasWidth, 3), np.uint8)
theta = theta * PI / 180;
cv2.line(canvas, (0, canvasWidth//2), (canvasWidth, canvasWidth//2), (255, 255, 255));
cv2.line(canvas,
(canvasWidth//2, canvasWidth//2),
(canvasWidth//2 + int(canvasWidth*math.cos(theta)), canvasWidth//2 + int(canvasWidth*math.sin(theta))),
(0, 255, 0), 2);
cv2.imshow('Inclunacion', canvas);
if __name__ == '__main__':
# Read reference image
refFilename = "spaceinvader.jpg"
imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)
cap = cv2.VideoCapture(0)
while(True):
try:
ret, frame = cap.read()
im = cv2.flip(frame, 1)
# Registered image will be resotred in imReg.
# The estimated homography will be stored in H.
imReg, H = alignImages(im, imReference, "Matches")
cv2.imshow('Aligned image', imReg)
a = H[0, 0]
b = H[0, 1]
theta = math.atan2(b, a) * (180 / PI)
drawInclination(theta)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
pass
cap.release()
cv2.destroyAllWindows()
@Papaass
Copy link

Papaass commented Jul 19, 2019

Hello,
How can i check is the homography is good or not by interpreting the matrix homograph ?

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