generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |