Last active
October 2, 2017 04:06
-
-
Save alanbernstein/0ff61f3a3ae5e62278090a7ec9223826 to your computer and use it in GitHub Desktop.
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
""" | |
simple tool for performing perspective correction | |
useful for "flattening" internet photos of things that i want to print or cut | |
""" | |
import os | |
import cv2 | |
import numpy as np | |
def correct_image(fin, fout, pin, pout, dim=None, show=True): | |
""" | |
correct_image(fin, fout, pin, pout, dim, show) | |
fin: input filename | |
fout: output filename | |
pin: Nx2 array of input points | |
pout: Nx2 array of output points | |
dim: dimensions of output image (computed if omitted) | |
""" | |
# http://www.learnopencv.com/homography-examples-using-opencv-python-c/ | |
if dim is None: | |
dim = tuple(np.max(pout, axis=0).astype(int)) | |
im_src = cv2.imread(fin) | |
h, status = cv2.findHomography(pin, pout) | |
im_dst = cv2.warpPerspective(im_src, h, dim) | |
cv2.imwrite(fout, im_dst) | |
if show: | |
cv2.imshow("Source Image", im_src) | |
cv2.imshow("Warped Image", im_dst) | |
cv2.waitKey(0) | |
def correct_ambigram(): | |
fin = 'original.jpg' | |
fout = 'transformed.jpg' | |
# manually chosen keypoints (source) | |
pts_src = np.array([[95.0, 322], [366, 198], [315, 7], [11, 114]]) | |
# rectified keypoints (destination) | |
W, H = 8, 5 | |
scale = 200 | |
pts_dst = np.array([[0., H], [W, H], [W, 0], [0, 0]]) * scale | |
correct_image(fin, fout, pts_src, pts_dst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment