-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
84 lines (67 loc) · 2.48 KB
/
scanner.py
File metadata and controls
84 lines (67 loc) · 2.48 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# import packages
import numpy as np
import cv2
import imutils
import argparse
from skimage.filters import threshold_local
from transformations import *
import matplotlib.pyplot as plt
# construct argument parser and parse args
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', default=r'C:\Users\estde\Documents\Object Detection\Out.jpg')
args = vars(ap.parse_args())
# load image, compute the ratio of old height
# to the new height, clone and resize
image = cv2.imread(r'C:\Users\estde\Documents\Object Detection\20221024_214522.jpg')
ratio = image.shape[0] / 500.0
original = image.copy()
image = imutils.resize(image, height=500)
# convert image to gray, blur and apply edge detection
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5,5), 0)
edged = cv2.Canny(image, 150, 200)
# show original image and edge detected image
print('applying edge detection')
cv2.imshow('Original Image', image)
cv2.imshow('Edged', edged)
cv2.waitKey(0)
#cv2.destroyAllWindows()
# find contours and keep largest one
# initialize screen contour
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]
# loop through contours
for c in cnts:
# approximate contours
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02*peri, True)
# check if approximated contour has four points
if len(approx) == 4:
screen_contours = approx
break
else:
print('Contours not detected')
break
# show contour outline of document
print('finding contours of document')
cv2.drawContours(image, [screen_contours], -1, (255,0,0), 2)
image = imutils.resize(image, width=image.shape[0])
cv2.imshow('Document Outline', image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
# apply transform
warped = perspective_transform(original, screen_contours.reshape(4,2)* ratio)
# convert to grayscale then threshold it
#warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
#T = threshold_local(warped, 11, offset=10, method='gaussian')
#warped = (warped>T).astype('uint8') * 255
# show original image and scanned images
print('applying perspective transform')
cv2.imshow('Original', imutils.resize(original, height=650))
cv2.imshow('Scanned Document', imutils.resize(warped, height=650))
cv2.waitKey(1)
name = args['image'][:len(args['image'])-4]
filename = name+'_scanned.jpg'
cv2.imwrite(filename, warped)
print('saved scan: {}'.format(filename))