-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost_processing_gwps.py
187 lines (134 loc) · 5.42 KB
/
post_processing_gwps.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import cv2
import numpy as np
import os
import argparse
from skimage.morphology import skeletonize
from multiprocessing import Pool
def connectivity_number_8(mat):
POS = [(1, 2), (2, 2), (2, 1), (2, 0), (1, 0), (0, 0), (0, 1), (0, 2)]
op = np.copy(mat)
op[op > 0] = 1
result = 0
for k in [0, 2, 4, 6]:
elem_k = op[POS[k]]
elem_k_1 = op[POS[(k + 1) % 8]]
elem_k_2 = op[POS[(k + 2) % 8]]
x_k = 1 - elem_k
x_k_1 = 1 - elem_k_1
x_k_2 = 1 - elem_k_2
result += x_k - (x_k * x_k_1 * x_k_2)
return result
def load_image(path, threshold):
im = cv2.imread(path, 0)
im[im < threshold] = 0
return im
def connected_components(mat):
ret, labels = cv2.connectedComponents(mat.astype(np.uint8))
component = np.zeros_like(mat)
comp_labels = set(labels[labels > 0])
# print(len(comp_labels))
for label in comp_labels:
component[:] = 0
if len(mat[labels == label]) > 50:
component[labels == label] = mat[labels == label]
yield component
def iteratively_remove(mat):
result = np.copy(mat)
unique_values = list(set(mat[mat > 0]))
sorted(unique_values)
for value in unique_values:
indices_to_check = [tuple(x) for x in list(np.argwhere(result == value))]
nb_mat = np.copy(result)
indices_list = np.array(indices_to_check)
nb_mat[indices_list[:, 0], indices_list[:, 1]] = 0
for index in indices_to_check:
nb_mat[index] = 1
nb_pixels = get_neighbourhood(nb_mat, index)
if np.sum(nb_pixels) > 1 and connectivity_number_8(nb_pixels) == 1:
result[index] = 0
return result
def get_neighbourhood(mat, index):
DIRS = [(-1, 0), (0, -1), (1, 0), (0, 1), (-1, 1), (1, -1), (-1, -1), (1, 1)]
max_y, max_x = mat.shape
result = np.zeros((3,3))
for dir in DIRS:
nb_index = (index[0] + dir[0], index[1] + dir[1])
if not exists(nb_index, max_y, max_x):
continue
if mat[nb_index] > 0:
result[(1 + dir[0], 1 + dir[1])] = 1
return result
def exists(index, ny, nx):
return (0 <= index[0] < ny) and (0 <= index[1] < nx)
def has_inf(mat):
return np.isinf(mat).any()
def calc_gwdt(mat):
DIRS = [(-1, 0), (0, -1), (1, 0), (0, 1)]
#DIRS = [(-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
result = np.zeros_like(mat)
result[mat > 0] = np.inf
max_y, max_x = result.shape
neighbours = np.zeros(len(DIRS))
candidate_indices = np.argwhere(np.isinf(result))
while len(candidate_indices):
round_result = np.copy(result)
next_round_candidates = []
for index in candidate_indices:
index = tuple(index)
if round_result[index] != np.inf:
continue
new_indices = []
for i, dir in enumerate(DIRS):
nb = (index[0] + dir[0], index[1] + dir[1])
if not exists(nb, max_y, max_x):
neighbours[i] = np.inf
continue
if result[nb] == np.inf:
new_indices.append(nb)
neighbours[i] = result[nb]
nb_min_arg = np.argmin(neighbours)
if neighbours[nb_min_arg] == np.inf:
continue
next_round_candidates.extend(new_indices)
target_dir = DIRS[nb_min_arg]
nb_index = (index[0] + target_dir[0], index[1] + target_dir[1])
round_result[index] = mat[index] + result[nb_index]
candidate_indices = next_round_candidates
result = round_result
return result
def collect_image_names(path):
files = os.listdir(path)
files = list(filter(lambda x: x.endswith(".png"), files))
return files
def process_image(params):
image_file, args = params
im = load_image(os.path.join(args.input, image_file), args.threshold)
im_result = np.zeros_like(im)
for connected_component in connected_components(im):
gwdt = calc_gwdt(im.astype(np.float)).astype(np.int)
edge = iteratively_remove(gwdt)
im_result[edge > 0] = 255
cv2.imwrite(os.path.join(args.output, image_file), im_result)
def main(args):
image_files = collect_image_names(args.input)
if os.path.exists(args.output):
already_processed_image_files = collect_image_names(args.output)
for image in already_processed_image_files:
image_files.remove(image)
else:
os.makedirs(args.output)
if len(image_files) > 0:
print(f"Processing {len(image_files)} files")
thread_args = zip(image_files, [args] * len(image_files))
p = Pool(args.num_threads)
p.map(process_image, thread_args)
else:
print("Already processed all files")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", type=str, help="Folder containing images to convert.")
parser.add_argument("-o", "--output", type=str, help="Destination folder for postprocessed images.")
parser.add_argument("-t", "--threshold", type=int, help="Filter threshold.")
parser.add_argument("--num_threads", type=int, help="Number of preocessing threads.")
args = parser.parse_args()
main(args)