-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.py
71 lines (56 loc) · 2.25 KB
/
core.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
# import the necessary packages
from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import imutils
import cv2
def rescale_frame(frame, percent=50):
width = int(frame.shape[1] * percent / 100)
height = int(frame.shape[0] * percent / 100)
dim = (width, height)
return cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
def crop_by_edges(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)
# find contours in the edge map, then initialize
# the contour that corresponds to the document
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
docCnt = None
# ensure that at least one contour was found
if len(cnts) > 0:
# sort the contours according to their size in
# descending order
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
# loop over the sorted contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# if our approximated contour has four points,
# then we can assume we have found the paper
if len(approx) == 4:
docCnt = approx
break
# apply a four point perspective transform to both the
# original image and grayscale image to obtain a top-down
# birds eye view of the paper
return four_point_transform(image, docCnt.reshape(4, 2))
# define the answer key which maps the question number
# to the correct answer
KEYs = {}
# load the image, convert it to grayscale, blur it
# slightly, then find edges
image = cv2.imread("dataset200/dataset200_002.BMP")
paper = crop_by_edges(image)
grayImage = cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY)
# apply Otsu's thresholding method to binarize the warped
# piece of paper
thresh = cv2.threshold(grayImage, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv2.imshow("Result color", paper)
cv2.imshow("Result grey", thresh)
cv2.imwrite("dataset200/dataset200_002_black.BMP", thresh)
cv2.waitKey(0)