-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.py
68 lines (53 loc) · 2.04 KB
/
scan.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
--------------------------
Document Scanner
--------------------------
An OpenCV based document scanner which scans documents based on four vertices
"""
# Import the necessary packages
from skimage.filters import threshold_local
import argparse
import cv2
from utils import *
# Construct argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image to be scanned")
args = vars(ap.parse_args())
image_PATH = args['image']
# STEP 1: Edge Detection
# Read the image and detect edges
image = cv2.imread(image_PATH)
image_copy = image.copy()
image = cv2.resize(image, (1500, 800))
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_blurred = cv2.GaussianBlur(image_gray, (5, 5), 0)
image_edge = cv2.Canny(image_gray, 75, 200)
# Display the original image and the edge detected image
cv2.imshow("Image", image)
cv2.imshow("Edged", image_edge)
# STEP 2: Estimate the contour of the edges
cnts = cv2.findContours(image_edge.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[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:
screenCnt = approx
break
# Step 3: Draw the contours and display them
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)
# Step 4: Get the perspective transformation
warped_image = four_point_perspective_transform(image, screenCnt.reshape(4,2))
# Step 5: Convert to grayscale and apply threshold to give it a scanned effect
warped_image = cv2.cvtColor(warped_image, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped_image, 11, offset=10, method="gaussian")
warped_image = (warped_image > T).astype("uint8") * 255
# Step 6: Display the original and scanned images
cv2.imshow("Original", cv2.resize(image_copy, (1500, 800)))
cv2.imshow("Scanned", warped_image)
# Step 7: Save the image
cv2.imwrite('./images/scanned.png', warped_image)
print("Image scanned and saved")