diff options
Diffstat (limited to 'scan.py')
-rw-r--r-- | scan.py | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -0,0 +1,65 @@ +import cv2 +import imutils +import numpy as np + + +def order_points(pts): + rect = np.zeros((4, 2), dtype="float32") + s = pts.sum(axis=1) + rect[0] = pts[np.argmin(s)] + rect[2] = pts[np.argmax(s)] + diff = np.diff(pts, axis=1) + rect[1] = pts[np.argmin(diff)] + rect[3] = pts[np.argmax(diff)] + return rect + + +def four_point_transform(image, pts): + rect = order_points(pts) + (tl, tr, br, bl) = rect + widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2)) + widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2)) + maxWidth = max(int(widthA), int(widthB)) + + heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2)) + heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2)) + maxHeight = max(int(heightA), int(heightB)) + + dst = np.array([ + [0, 0], + [maxWidth - 1, 0], + [maxWidth - 1, maxHeight - 1], + [0, maxHeight - 1]], dtype="float32") + + M = cv2.getPerspectiveTransform(rect, dst) + warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)) + + return warped + + +image = cv2.imread("example.jpg") +ratio = image.shape[0] / 500.0 +orig = image.copy() + +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +gray = cv2.GaussianBlur(gray, (5, 5), 0) +edged = cv2.Canny(gray, 75, 200) + +cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) +cnts = imutils.grab_contours(cnts) +cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] + +for c in cnts: + peri = cv2.arcLength(c, True) + approx = cv2.approxPolyDP(c, 0.02 * peri, True) + if len(approx) == 4: + screenCnt = approx + break + +cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) + +warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio) + +cv2.imshow("Original", imutils.resize(orig, height=650)) +cv2.imshow("Scanned", imutils.resize(warped, height=650)) +cv2.waitKey(0) |