Skip to content

Instantly share code, notes, and snippets.

@ChongyeWang
Created March 17, 2019 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChongyeWang/8b84ae0c65346b4d34df9f7c01ab9af7 to your computer and use it in GitHub Desktop.
Save ChongyeWang/8b84ae0c65346b4d34df9f7c01ab9af7 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('images/scan.jpg')
cv2.imshow('Original', image)
cv2.waitKey(0)
# Cordinates of the 4 corners of the original image
points_A = np.float32([[320,15], [700,215], [85,610], [530,780]])
# Cordinates of the 4 corners of the desired output
# We use a ratio of an A4 Paper 1 : 1.41
points_B = np.float32([[0,0], [420,0], [0,594], [420,594]])
# Use the two sets of four points to compute
# the Perspective Transformation matrix, M
M = cv2.getPerspectiveTransform(points_A, points_B)
warped = cv2.warpPerspective(image, M, (420,594))
cv2.imshow('warpPerspective', warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('images/ex2.jpg')
rows,cols,ch = image.shape
cv2.imshow('Original', image)
cv2.waitKey(0)
# Cordinates of the 4 corners of the original image
points_A = np.float32([[320,15], [700,215], [85,610]])
# Cordinates of the 4 corners of the desired output
# We use a ratio of an A4 Paper 1 : 1.41
points_B = np.float32([[0,0], [420,0], [0,594]])
# Use the two sets of four points to compute
# the Perspective Transformation matrix, M
M = cv2.getAffineTransform(points_A, points_B)
warped = cv2.warpAffine(image, M, (cols, rows))
cv2.imshow('warpPerspective', warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment