-
Notifications
You must be signed in to change notification settings - Fork 8
/
process.py
169 lines (130 loc) · 4.53 KB
/
process.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#! /usr/bin/env python3
from copy import deepcopy
import numpy as np
import cv2
def get_most_frequent_vicinity_value(mat, x, y, xyrange):
ymax, xmax = mat.shape
vicinity_values = mat[max(y - xyrange, 0):min(y + xyrange, ymax),
max(x - xyrange, 0):min(x + xyrange, xmax)].flatten()
counts = np.bincount(vicinity_values)
return np.argmax(counts)
def smoothen(mat, filter_size=4):
ymax, xmax = mat.shape
flat_mat = np.array([
get_most_frequent_vicinity_value(mat, x, y, filter_size)
for y in range(0, ymax)
for x in range(0, xmax)
])
return flat_mat.reshape(mat.shape)
def are_neighbors_same(mat, x, y):
width = len(mat[0])
height = len(mat)
val = mat[y][x]
xRel = [1, 0]
yRel = [0, 1]
for i in range(0, len(xRel)):
xx = x + xRel[i]
yy = y + yRel[i]
if xx >= 0 and xx < width and yy >= 0 and yy < height:
if (mat[yy][xx] != val).all():
return False
return True
def outline(mat):
ymax, xmax, _ = mat.shape
line_mat = np.array([
255 if are_neighbors_same(mat, x, y) else 0
for y in range(0, ymax)
for x in range(0, xmax)
],
dtype=np.uint8)
return line_mat.reshape((ymax, xmax))
def getRegion(mat, cov, x, y):
covered = deepcopy(cov)
region = {'value': mat[y][x], 'x': [], 'y': []}
value = mat[y][x]
queue = [[x, y]]
while (len(queue) > 0):
coord = queue.pop()
if covered[coord[1]][coord[0]] == False and mat[coord[1]][
coord[0]] == value:
region['x'].append(coord[0])
region['y'].append(coord[1])
covered[coord[1]][coord[0]] = True
if coord[0] > 0:
queue.append([coord[0] - 1, coord[1]])
if coord[0] < len(mat[0]) - 1:
queue.append([coord[0] + 1, coord[1]])
if coord[1] > 0:
queue.append([coord[0], coord[1] - 1])
if coord[1] < len(mat) - 1:
queue.append([coord[0], coord[1] + 1])
return region
def coverRegion(covered, region):
for i in range(0, len(region['x'])):
covered[region['y'][i]][region['x'][i]] = True
def sameCount(mat, x, y, incX, incY):
value = mat[y][x]
count = -1
while x >= 0 and x < len(
mat[0]) and y >= 0 and y < len(mat) and mat[y][x] == value:
count += 1
x += incX
y += incY
return count
def getLabelLoc(mat, region):
bestI = 0
best = 0
for i in range(0, len(region['x'])):
goodness = sameCount(
mat, region['x'][i], region['y'][i], -1, 0) * sameCount(
mat, region['x'][i], region['y'][i], 1, 0) * sameCount(
mat, region['x'][i], region['y'][i], 0, -1) * sameCount(
mat, region['x'][i], region['y'][i], 0, 1)
if goodness > best:
best = goodness
bestI = i
return {
'value': region['value'],
'x': region['x'][bestI],
'y': region['y'][bestI]
}
def getBelowValue(mat, region):
x = region['x'][0]
y = region['y'][0]
print(region)
while mat[y][x] == region['value']:
print(mat[y][x])
y += 1
return mat[y][x]
def removeRegion(mat, region):
if region['y'][0] > 0:
newValue = mat[region['y'][0] - 1][region['x'][0]]
else:
newValue = getBelowValue(mat, region)
for i in range(0, len(region['x'])):
mat[region['y'][i]][region['x'][i]] = newValue
def getLabelLocs(mat):
width = len(mat[0])
height = len(mat)
covered = [[False] * width] * height
labelLocs = []
for y in range(0, height):
for x in range(0, width):
if covered[y][x] == False:
region = getRegion(mat, covered, x, y)
coverRegion(covered, region)
if len(region['x']) > 100:
labelLocs.append(getLabelLoc(mat, region))
else:
removeRegion(mat, region)
return labelLocs
def edge_mask(image, line_size=3, blur_value=9):
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
gray_blur = cv2.medianBlur(gray, blur_value)
edges = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, line_size, blur_value)
return edges
def merge_mask(image, mask):
return cv2.bitwise_and(image, image, mask=mask)
def blur_image(image, blur_d=5):
return cv2.bilateralFilter(image, d=blur_d, sigmaColor=200, sigmaSpace=200)