This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
niproc.py
84 lines (62 loc) · 2.84 KB
/
niproc.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
import cv2 as cv
import numpy as np
import collections
"""
processimage
returns a 'processed image' named tuple.
The named tuple contains the image with an overlay, and counts of shapes
in 'squareCount', 'linecount', etc.
"""
def processimage(image):
image_tuple = collections.namedtuple('processedImage', ['cleanImage', 'squareCount', 'lineCount', 'circleCount', 'triangleCount'])
# Pull out only the black parts of the image
# TODO: Should HSV be used here (Currently using BGR)
range_lower = np.array([0, 0, 0])
range_upper = np.array([15, 15, 15])
mask = cv.inRange(image, range_lower, range_upper)
image_tuple.cleanImage = image
image_tuple.processedImage = image
image_tuple = detectsquares(image_tuple)
image_tuple = detectlines(image_tuple)
image_tuple = detectcircles(image_tuple)
image_tuple = detecttriangles(image_tuple)
return addoverlay(image_tuple)
def addoverlay(image_tuple):
height, width, channels = image_tuple.cleanImage.shape
font = cv.FONT_HERSHEY_PLAIN
x = int(width / 4)
y = int(height / 5)
fontsize = 15
color = (0,0,255)
linetype = cv.LINE_AA
scale = 6 * fontsize # allows symbols to grow/shrink based on text size
squareCount = str(processed_img.squareCount)
lineCount = str(processed_img.lineCount)
circleCount = str(processed_img.circleCount)
triangleCount = str(processed_img.triangleCount)
cv.putText(image_tuple.processedImage, squareCount, (x, y), font, fontsize, color, linetype)
cv.putText(image_tuple.processedImage, lineCount, (x, 2 * y), font, fontsize, color, linetype)
cv.putText(image_tuple.processedImage, circleCount, (x, 3 * y), font, fontsize, color, linetype)
cv.putText(image_tuple.processedImage, triangleCount, (x, 4 * y), font, fontsize, color, linetype)
cv.rectangle(image_tuple.processedImage, (2 * x, y - 2 * scale), (2 * x + 2 * scale, y), color, -linetype)
cv.line(image_tuple.processedImage, (2 * x + scale, 2 * y - 2 * scale), (2 * x + scale, 2 * y), color, linetype)
cv.circle(image_tuple.processedImage, (2 * x + scale, 3 * y - scale), scale, color, -linetype)
pts = np.array([[2 * x + scale, 4 * y - 2 * scale], [2 * x, 4 * y], [2 * x + 2 * scale, 4 * y]], np.int32)
cv.fillPoly(image_tuple.processedImage, [pts], color)
return image_tuple
def detectsquares(processed_img):
#TODO
print('Detected ', processed_img.squareCount, ' squares.')
return processed_img
def detectlines(processed_img):
# TODO
print('Detected ', processed_img.lineCount, ' lines.')
return processed_img
def detectcircles(processed_img):
# TODO
print('Detected ', processed_img.circleCount, ' circles.')
return processed_img
def detecttriangles(processed_img):
# TODO
print('Detected ', processed_img.triangleCount, ' triangles.')
return processed_img