-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathscan.py
90 lines (69 loc) · 2.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
########################################
# FOR USAGE: RUN BELOW COMMAND #
# python scan.py -i images/m1.jpg #
########################################
#From other file to make a rectangle after finding four points
from pyimagesearch.transform import four_point_transform
import numpy as np
import argparse
import cv2
import imutils
### construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image to be scanned")
args = vars(ap.parse_args())
#loading image
image = cv2.imread(args["image"])
# Compute the ratio of the old height to the new height, clone it,
# and resize it easier for compute and viewing
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)
### convert the image to grayscale, blur it, and find edges in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Gaussian Blurring to remove high frequency noise helping in
# Contour Detection
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# Canny Edge Detection
edged = cv2.Canny(gray, 75, 200)
print("STEP 1: Edge Detection")
# cv2.imshow("Image", image)
cv2.imshow("Edged", edged)
# finding the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
## What are Contours ?
## Contours can be explained simply as a curve joining all the continuous
## points (along the boundary), having same color or intensity.
## The contours are a useful tool for shape analysis and object detection
## and recognition.
# Handling due to different version of OpenCV
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# Taking only the top 5 contours by Area
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
### Heuristic & Assumption
# A document scanner simply scans in a piece of paper.
# A piece of paper is assumed to be a rectangle.
# And a rectangle has four edges.
# Therefore use a heuristic like : we’ll assume that the largest
# contour in the image with exactly four points is our piece of paper to
# be scanned.
# looping over the contours
for c in cnts:
### Approximating the contour
#Calculates a contour perimeter or a curve length
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.01 * peri, True)#0.02
# if our approximated contour has four points, then we
# can assume that we have found our screen
screenCnt = approx
if len(approx) == 4:
screenCnt = approx
break
# show the contour (outline)
print("STEP 2: Finding Boundary")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Boundary", image)
cv2.waitKey(0)
cv2.destroyAllWindows()