#!/bin/env python import sys import cv2 import math import numpy as np import matplotlib.pyplot as plt def correct(): img = cv2.imread(sys.argv[1]) rows,cols,ch = img.shape pts1 = np.float32([[98, 786],[20, 3268],[1800, 3268],[1798, 850]]) ratio=1.6 cardH=math.sqrt((pts1[2][0]-pts1[1][0])*(pts1[2][0]-pts1[1][0])+(pts1[2][1]-pts1[1][1])*(pts1[2][1]-pts1[1][1])) cardW=ratio*cardH; pts2 = np.float32([[pts1[0][0],pts1[0][1]], [pts1[0][0]+cardW, pts1[0][1]], [pts1[0][0]+cardW, pts1[0][1]+cardH], [pts1[0][0], pts1[0][1]+cardH]]) M = cv2.getPerspectiveTransform(pts1,pts2) offsetSize=500 transformed = np.zeros((int(cardW+offsetSize), int(cardH+offsetSize)), dtype=np.uint8); dst = cv2.warpPerspective(img, M, transformed.shape) plt.subplot(121),plt.imshow(img),plt.title('Input') plt.subplot(122),plt.imshow(dst),plt.title('Output') plt.show() if __name__ == "__main__": correct()