-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.py
More file actions
70 lines (55 loc) · 1.68 KB
/
Scanner.py
File metadata and controls
70 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Created on Fri May 29 20:34:15 2020
@author: Karan Shetty
"""
import numpy as np
import cv2
import imutils
from transform import four_point_transform
#img = cv2.imread('page.jpg')
img = cv2.imread('notebook2.jpg')
scan = True
print(img.shape)
ratio = img.shape[0] / 500.0
orig = img.copy()
img = imutils.resize(img, height = 500)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5,5), 0)
edge = cv2.Canny(gray, 75, 200)
cv2.imshow("edge", edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
cnts = cv2.findContours(edge, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[1]
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:
print("Document Found.")
ScreenCnt = approx
scan = True
break
else:
print("No Documents Found.")
scan = False
break
if scan :
print(ScreenCnt)
print("STEP 2: Find contours of paper")
cv2.drawContours(img, [ScreenCnt], -1, (255, 0, 0), 2)
cv2.imshow("Outline", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
warped = four_point_transform(orig, ScreenCnt.reshape(4,2) * ratio)
#warped = imutils.resize(warped, height = 500)
cv2.imshow("Doc", warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = cv2.adaptiveThreshold(warped,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,21,10)
cv2.imshow("Documeny", warped)
cv2.imwrite("Document.jpg", warped)
cv2.waitKey(0)
cv2.destroyAllWindows()