/
imager.py
58 lines (44 loc) · 1.48 KB
/
imager.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
import argparse
import cv2
from matplotlib import pyplot as plt
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="input image's path")
args = vars(ap.parse_args())
# load the image
image = cv2.imread(args["image"])
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# blur, to remove sharp edges
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# adaptively thresholding to make binary
thresholded = cv2.adaptiveThreshold(
blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# OPTIONAL - use edge detection
edged = cv2.Canny(thresholded, 30, 200)
# find countours in the image
im2, contours, hierarchy = cv2.findContours(
thresholded, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
rects = []
peris = []
for c in contours:
# approximating the contour
peri = cv2.arcLength(c, True)
peris.append(peri)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# check if the approximated contour has four points
# if so, it can be labelled as a rectangle
if len(approx) == 4:
rects.append(approx)
# drawing the countours
countered = cv2.drawContours(image.copy(), rects, -1, (255, 0, 0), 3)
# plotting the images
images = [image, thresholded, edged, countered]
titles = ["original", "thresholded", "edge detection", "result"]
for i in range(4):
plt.subplot(2, 2, i+1)
plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()