Created
May 10, 2015 12:58
-
-
Save kmod-midori/6b210650ac7b4ee9827a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import os, math | |
| import numpy as np | |
| #from matplotlib import pyplot as plt | |
| import cv2 | |
| img = cv2.resize(cv2.imread('img.jpg',1), (0,0), fx=0.5, fy=0.5) | |
| def sort_cw(points): | |
| points = sorted(points,key=lambda cnt: cnt[0][0]) | |
| left = points[:2] | |
| left_sorted = sorted(left,key=lambda cnt: cnt[0][1]) | |
| top_left = left_sorted[0] | |
| bottom_left = left_sorted[1] | |
| right = points[-2:] | |
| right_sorted = sorted(right,key=lambda cnt: cnt[0][1]) | |
| top_right = right_sorted[0] | |
| bottom_right = right_sorted[1] | |
| return np.float32([top_left,top_right,bottom_right,bottom_left]) | |
| blured = cv2.GaussianBlur(img,(3,3),0) | |
| canny = cv2.Canny(img, 50, 150) | |
| contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
| cnt = sorted(contours,key=cv2.contourArea)[-1] | |
| epsilon = 0.1*cv2.arcLength(cnt,True) | |
| approx = cv2.approxPolyDP(cnt,epsilon,True) | |
| newImgSize = np.float32([[0,0],[840,0],[840,594],[0,594]]) | |
| M = cv2.getPerspectiveTransform(sort_cw(approx),newImgSize) | |
| processImage = cv2.warpPerspective(img,M,(840,594)) | |
| cv2.imwrite('out.jpg',processImage) | |
| def showImage(showImg = img): | |
| cv2.imshow('img', img) | |
| cv2.waitKey(0) | |
| cv2.destroyAllWindows() | |
| def drawPoint(img, x, y): | |
| point = x, y | |
| cv2.circle(img, point, 2, (0,255,0),3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment