-
Notifications
You must be signed in to change notification settings - Fork 44
/
Scanner.py
45 lines (29 loc) · 1.35 KB
/
Scanner.py
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
import cv2
import numpy as np
import mapper
image=cv2.imread("test_img.jpg") #read in the image
image=cv2.resize(image,(1300,800)) #resizing because opencv does not work well with bigger images
orig=image.copy()
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #RGB To Gray Scale
cv2.imshow("Title",gray)
blurred=cv2.GaussianBlur(gray,(5,5),0) #(5,5) is the kernel size and 0 is sigma that determines the amount of blur
cv2.imshow("Blur",blurred)
edged=cv2.Canny(blurred,30,50) #30 MinThreshold and 50 is the MaxThreshold
cv2.imshow("Canny",edged)
contours,hierarchy=cv2.findContours(edged,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) #retrieve the contours as a list, with simple apprximation model
contours=sorted(contours,key=cv2.contourArea,reverse=True)
#the loop extracts the boundary contours of the page
for c in contours:
p=cv2.arcLength(c,True)
approx=cv2.approxPolyDP(c,0.02*p,True)
if len(approx)==4:
target=approx
break
approx=mapper.mapp(target) #find endpoints of the sheet
pts=np.float32([[0,0],[800,0],[800,800],[0,800]]) #map to 800*800 target window
op=cv2.getPerspectiveTransform(approx,pts) #get the top or bird eye view effect
dst=cv2.warpPerspective(orig,op,(800,800))
cv2.imshow("Scanned",dst)
# press q or Esc to close
cv2.waitKey(0)
cv2.destroyAllWindows()