From ad5dadba08ed3d7528bc7a3e3713c12d133ffb86 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Tue, 7 May 2024 11:21:58 -0700 Subject: [PATCH] Add homography --- OCR/image-alignment/run-homography.py | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 OCR/image-alignment/run-homography.py diff --git a/OCR/image-alignment/run-homography.py b/OCR/image-alignment/run-homography.py new file mode 100755 index 00000000..e67e583a --- /dev/null +++ b/OCR/image-alignment/run-homography.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +""" +Uses quadrilaterial edge detection and executes a four-point perspective transform on a source image. +""" + +import numpy as np +import cv2 as cv + +img1 = cv.imread('pertussis-template.png',cv.IMREAD_GRAYSCALE) # queryImage +img2 = cv.imread('output/pertussis-filled-2.png',cv.IMREAD_GRAYSCALE) # trainImage + +ratio = 0.5 + +img1 = cv.resize(img1, None, fx=ratio, fy=ratio, interpolation=cv.INTER_LANCZOS4) +img2 = cv.resize(img2, None, fx=ratio, fy=ratio, interpolation=cv.INTER_LANCZOS4) +cv.imwrite('s1.png', img1) +cv.imwrite('s2.png', img2) + +sift = cv.SIFT_create() + +# find the keypoints and descriptors with SIFT +kp1, descriptors1 = sift.detectAndCompute(img1,None) +kp2, descriptors2 = sift.detectAndCompute(img2,None) + +matcher = cv.DescriptorMatcher_create(cv.DescriptorMatcher_FLANNBASED) +knn_matches = matcher.knnMatch(descriptors1, descriptors2, 2) + +# import pdb; pdb.set_trace() + +# Filter matches using the Lowe's ratio test +# use an aggressive threshold here- the larger the image the more aggresively this should be filtered +ratio_thresh = 0.3 +good_matches = [] +for m,n in knn_matches: + if m.distance < ratio_thresh * n.distance: + good_matches.append(m) + +img3 = cv.drawMatches(img1, kp1, img2, kp2, good_matches, None, flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) +cv.imwrite('test-match.png', img3) + +src_pts = np.float32([ kp1[m.queryIdx].pt for m in good_matches ]).reshape(-1,1,2) +dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good_matches ]).reshape(-1,1,2) + +M, mask = cv.findHomography(dst_pts, src_pts, cv.RANSAC,5.0) +matchesMask = mask.ravel().tolist() + +h,w = img1.shape +pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) +dst = cv.perspectiveTransform(pts,M) +out = cv.warpPerspective(img2, M, (w, h)) + +cv.imwrite('test-transform.png', out)