-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.py
executable file
·300 lines (281 loc) · 13.1 KB
/
filter.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import numpy as np
import cv2
import Global_Params
import algorithm
import threading
def RgbMedianFilter(img_set, size):
"""
对RGB图片集做中值滤波(暂时只考虑了奇数大小的窗口,如3x3、5x5)(but好像作用不大)
:param img_set: 图片集, shape = [num, height, width, 3]
:param size: 中值滤波窗口大小 (size, size)
:return: 中值滤波后的图片集
"""
num = img_set.shape[0]
height = img_set.shape[1]
width = img_set.shape[2]
filted_img_set = np.zeros((num, height, width, 3))
edge = (int)(size/2)
idx = 0
for img in img_set:
for i in range(height):
for j in range(width):
if i < edge or i > height-edge-1 or j < edge or j > width-edge-1:
filted_img_set[idx, i, j, :] = img[i, j, :]
else:
for color in range(3):
filted_img_set[idx, i, j, color] = np.median(img[i-edge:i+edge, j-edge:j+edge, color])
cv2.imshow("img", img/255.0)
cv2.imshow("filted_img", filted_img_set[idx]/255.0)
cv2.waitKey(0)
cv2.destroyAllWindows()
idx += 1
return filted_img_set
def RedBlackBoost_Original(img_set, IMAGE_SHOW_OR_NOT, IMAGE_SHOW_FREQUENCY):
'''
增强图片中的红黑色(红色棋子增强红色,黑色棋子增强黑色)
:param img_set: 图片集, shape = [num, height, width, 3]
:return: 处理后的图片集
'''
num = img_set.shape[0]
height = img_set.shape[1]
width = img_set.shape[2]
boosted_img_set = np.zeros((num, height, width, 3))
hsv_img = np.zeros((height, width, 3))
threshold = 0.02
idx = 0
count = 0
radius = Global_Params.M_norm_size/2
mid =Global_Params.M_norm_size/2.0
mid = int(mid)
for img in img_set:
count += 1
red_cnt = 0
red_flag = False
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
for i in range(height):
for j in range(width):
if ((0 <= hsv_img[i, j, 0] <= 10) or (310 <= hsv_img[i, j, 0] <= 360)) and (70 <= hsv_img[i, j, 1]*255 <= 255) and (50 <= hsv_img[i, j, 2] <= 255):
red_cnt += 1
if red_cnt >= threshold * height * width:
red_flag = True
break
if red_flag is True:
break
if red_flag is True: # 红棋子红色增强
for i in range(height):
for j in range(width):
if algorithm.outOfRadius(j, i):
boosted_img_set[idx, i, j, :] = [255, 255, 255]
continue
if ((0 <= hsv_img[i, j, 0] <= 20) or (100 <= hsv_img[i, j, 0] <= 360)) and \
(66.0 <= hsv_img[i, j, 1]*255 <= 255) and (33.0 <= hsv_img[i, j, 2] <= 255):
boosted_img_set[idx, i, j, :] = [0, 0, 255]
else:
boosted_img_set[idx, i, j, :] = [255, 255, 255]
else: # 黑棋子黑色增强
for i in range(height):
for j in range(width):
if algorithm.outOfRadius(j, i):
boosted_img_set[idx, i, j, :] = [255, 255, 255]
continue
if (0 <= hsv_img[i, j, 0] <= 360) and \
(0 <= hsv_img[i, j, 1]*255 <= 255) and (0 <= hsv_img[i, j, 2] <= 70):
boosted_img_set[idx, i, j, :] = [0, 0, 0]
else:
boosted_img_set[idx, i, j, :] = [255, 255, 255]
if idx/30 == int(idx/30):
print("RedBlackBoost Processing... <cur idx =", idx, ">")
if IMAGE_SHOW_OR_NOT and idx/IMAGE_SHOW_FREQUENCY == int(idx/IMAGE_SHOW_FREQUENCY):
print("hsv_img[mid, mid, :] -> ", hsv_img[mid, mid, :])
cv2.imshow("img_" + str(idx), img/255.0)
cv2.imshow("boosted_img_" + str(idx), boosted_img_set[idx]/255.0)
cv2.waitKey(0)
cv2.destroyAllWindows()
idx += 1
del img_set
return boosted_img_set
# def RedBlackBoost_SingleThread(img_set, IMAGE_SHOW_OR_NOT, IMAGE_SHOW_FREQUENCY, thread_idx):
# '''
# 增强图片中的红黑色(红色棋子增强红色,黑色棋子增强黑色)
# :param img_set: 图片集, shape = [num, height, width, 3]
# :return: 处理后的图片集
# '''
# num = img_set.shape[0]
# print("num =", num, "\t\tthread_idx =", thread_idx)
# height = img_set.shape[1]
# width = img_set.shape[2]
# boosted_img_set = np.zeros((num, height, width, 3))
# hsv_img = np.zeros((height, width, 3))
# threshold = 0.02
# idx = 0
# count = 0
# radius = Global_Params.M_norm_size/2
# mid =Global_Params.M_norm_size/2.0
# mid = int(mid)
# for img in img_set:
# count += 1
# red_cnt = 0
# red_flag = False
# hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# for i in range(height):
# for j in range(width):
# if ((0 <= hsv_img[i, j, 0] <= 10) or (310 <= hsv_img[i, j, 0] <= 360)) and (70 <= hsv_img[i, j, 1]*255 <= 255) and (50 <= hsv_img[i, j, 2] <= 255):
# red_cnt += 1
# if red_cnt >= threshold * height * width:
# red_flag = True
# break
# if red_flag is True:
# break
# if red_flag is True: # 红棋子红色增强
# for i in range(height):
# for j in range(width):
# if algorithm.outOfRadius(j, i):
# boosted_img_set[idx, i, j, :] = [255, 255, 255]
# continue
# if ((0 <= hsv_img[i, j, 0] <= 20) or (100 <= hsv_img[i, j, 0] <= 360)) and \
# (66.0 <= hsv_img[i, j, 1]*255 <= 255) and (33.0 <= hsv_img[i, j, 2] <= 255):
# boosted_img_set[idx, i, j, :] = [0, 0, 255]
# else:
# boosted_img_set[idx, i, j, :] = [255, 255, 255]
# else: # 黑棋子黑色增强
# for i in range(height):
# for j in range(width):
# if algorithm.outOfRadius(j, i):
# boosted_img_set[idx, i, j, :] = [255, 255, 255]
# continue
# if (0 <= hsv_img[i, j, 0] <= 360) and \
# (0 <= hsv_img[i, j, 1]*255 <= 255) and (0 <= hsv_img[i, j, 2] <= 70):
# boosted_img_set[idx, i, j, :] = [0, 0, 0]
# else:
# boosted_img_set[idx, i, j, :] = [255, 255, 255]
# if idx/30 == int(idx/30):
# print("RedBlackBoost Processing... <cur idx =", idx, ">, <thread idx =", thread_idx, ">")
# if IMAGE_SHOW_OR_NOT and idx/IMAGE_SHOW_FREQUENCY == int(idx/IMAGE_SHOW_FREQUENCY):
# print("hsv_img[mid, mid, :] -> ", hsv_img[mid, mid, :])
# cv2.imshow("img_" + str(idx), img/255.0)
# cv2.imshow("boosted_img_" + str(idx), boosted_img_set[idx]/255.0)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# idx += 1
# del img_set
# return boosted_img_set
# print("RedBlackBoost thread #", thread_idx, "finished!")
def distribution(NUM_IMG, THREAD):
print("num =", NUM_IMG, "\t\tthread =", THREAD)
reminder = NUM_IMG%THREAD
base = round((NUM_IMG - reminder)/THREAD)
print("base =", base, "\t\treminder =", reminder)
dis_list = [base for _ in range(THREAD)]
idx_list = [0 for _ in range(THREAD)]
for idx in range(reminder):
dis_list[idx] += 1
for idx in range(THREAD):
if idx > 0:
idx_list[idx] += dis_list[idx - 1] + idx_list[idx - 1]
else:
idx_list[idx] = 0
print("distribution list =", dis_list)
print("dis index list =", idx_list)
return dis_list, idx_list
def RedBlackBoost(img_set, IMAGE_SHOW_OR_NOT, IMAGE_SHOW_FREQUENCY, THREAD=12):
num = img_set.shape[0]
height = img_set.shape[1]
width = img_set.shape[2]
boosted_img_set_total = np.zeros((num, height, width, 3))
distribution_list, dis_index_list = distribution(num, THREAD)
# return
def RedBlackBoost_SingleThread(img_set, IMAGE_SHOW_OR_NOT, IMAGE_SHOW_FREQUENCY, thread_idx):
'''
增强图片中的红黑色(红色棋子增强红色,黑色棋子增强黑色)
:param img_set: 图片集, shape = [num, height, width, 3]
:return: 处理后的图片集
'''
num = img_set.shape[0]
print("num =", num, "\t\tthread_idx =", thread_idx, "\t\tdistribution_list[thread_idx] =", distribution_list[thread_idx])
height = img_set.shape[1]
width = img_set.shape[2]
boosted_img_set = np.zeros((num, height, width, 3))
hsv_img = np.zeros((height, width, 3))
threshold = 0.02
idx = 0
count = 0
radius = Global_Params.M_norm_size/2
mid =Global_Params.M_norm_size/2.0
mid = int(mid)
for img in img_set:
count += 1
red_cnt = 0
red_flag = False
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
for i in range(height):
for j in range(width):
if ((0 <= hsv_img[i, j, 0] <= 10) or (310 <= hsv_img[i, j, 0] <= 360)) and (70 <= hsv_img[i, j, 1]*255 <= 255) and (50 <= hsv_img[i, j, 2] <= 255):
red_cnt += 1
if red_cnt >= threshold * height * width:
red_flag = True
break
if red_flag is True:
break
if red_flag is True: # 红棋子红色增强
for i in range(height):
for j in range(width):
if algorithm.outOfRadius(j, i):
boosted_img_set[idx, i, j, :] = [255, 255, 255]
continue
if ((0 <= hsv_img[i, j, 0] <= 20) or (100 <= hsv_img[i, j, 0] <= 360)) and \
(66.0 <= hsv_img[i, j, 1]*255 <= 255) and (33.0 <= hsv_img[i, j, 2] <= 255):
boosted_img_set[idx, i, j, :] = [0, 0, 255]
else:
boosted_img_set[idx, i, j, :] = [255, 255, 255]
else: # 黑棋子黑色增强
for i in range(height):
for j in range(width):
if algorithm.outOfRadius(j, i):
boosted_img_set[idx, i, j, :] = [255, 255, 255]
continue
if (0 <= hsv_img[i, j, 0] <= 360) and \
(0 <= hsv_img[i, j, 1]*255 <= 255) and (0 <= hsv_img[i, j, 2] <= 70):
boosted_img_set[idx, i, j, :] = [0, 0, 0]
else:
boosted_img_set[idx, i, j, :] = [255, 255, 255]
if idx/30 == int(idx/30):
print("RedBlackBoost Processing... <cur idx =", idx, ">, <thread idx =", thread_idx, ">")
if IMAGE_SHOW_OR_NOT and idx/IMAGE_SHOW_FREQUENCY == int(idx/IMAGE_SHOW_FREQUENCY):
print("hsv_img[mid, mid, :] -> ", hsv_img[mid, mid, :])
cv2.imshow("img_" + str(idx), img/255.0)
cv2.imshow("boosted_img_" + str(idx), boosted_img_set[idx]/255.0)
cv2.waitKey(0)
cv2.destroyAllWindows()
idx += 1
del img_set
# return boosted_img_set
boosted_img_set_total[dis_index_list[thread_idx]:dis_index_list[thread_idx] + distribution_list[thread_idx]] = boosted_img_set
print("RedBlackBoost thread #", thread_idx, "finished!")
threads = [threading.Thread(target=RedBlackBoost_SingleThread, \
args=(img_set[dis_index_list[i]:dis_index_list[i] + distribution_list[i]], \
IMAGE_SHOW_OR_NOT, IMAGE_SHOW_FREQUENCY, i)) \
for i in range(THREAD)]
for i in range(THREAD):
threads[i].start()
for i in range(THREAD):
threads[i].join()
# cv2.imshow("img_" + str(99), img_set[99]/255.0)
# cv2.imshow("boosted_img_" + str(99), boosted_img_set_total[99]/255.0)
# flag = cv2.waitKey(0)
# if flag == 27:
# cv2.destroyAllWindows()
# return
return boosted_img_set_total
"""
num = img_set.shape[0]
height = img_set.shape[1]
width = img_set.shape[2]
boosted_img_set = np.zeros((num, height, width, 3))
hsv_img = np.zeros((height, width, 3))
threshold = 0.02
idx = 0
count = 0
radius = Global_Params.M_norm_size/2
mid =Global_Params.M_norm_size/2.0
mid = int(mid)
"""