-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasure.py
458 lines (408 loc) · 17.3 KB
/
measure.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import os
import cv2
import base64
import statistics
import numpy as np
from PIL import Image
from tqdm import tqdm
from io import BytesIO
import multiprocessing
def draw_rectangle(A,x,y):
cv2.rectangle(A, (x - 1, y - 1), (x + 2, y + 2), (0, 0, 255), 1)
return A
def get_circle_x_y_color_list(A,xCenter, yCenter, radius):
xEdge = 0
yEdge = radius
p = 1 - radius
if len(A.shape)==3:
height,width, _ = A.shape
else :
height,width= A.shape
circle_x_y_color =[]
while(xEdge <= yEdge):
xEdge = xEdge + 1
if(p < 0):
p = p + 2 * xEdge + 1
else:
yEdge = yEdge - 1
p = p + 2 * (xEdge - yEdge) + 1
xEdge = round(xEdge)
yEdge = round(yEdge)
x_y_dict = {'x_x_right':xCenter + xEdge,'y_y_down':yCenter + yEdge,'x_x_left':xCenter - xEdge,
'y_y_top': yCenter - yEdge,'y_x_down': yCenter + xEdge,'x_y_right' : xCenter + yEdge,
'y_x_top' : yCenter - xEdge, 'x_y_left': xCenter - yEdge}
key = [['y_y_down','x_x_right'],['y_y_down', 'x_x_left'],['y_y_top','x_x_right'],
['y_y_top', 'x_x_left'],['y_x_down', 'x_y_right'],['y_x_top', 'x_y_right'],
['y_x_down', 'x_y_left'],['y_x_top', 'x_y_left']]
white = (255, 255, 255)
black = (0, 0, 0)
color = None
for idx,y_x in enumerate(key):
y_key=y_x[0]
x_key=y_x[1]
y,x=round(x_y_dict[y_key]),round(x_y_dict[x_key])
if x>0 and x<width and y>0 and y<height:
current=A[y,x]
if np.all(current==black):
color ='black'
if np.all(current!=black):
color = 'white'
circle_x_y_color.append([x, y,color])
*circle_x_y_color, = map(list, {*map(tuple, circle_x_y_color)}) # List de-duplication
return circle_x_y_color
def get_is_joint(A,xCenter,yCenter,radius):
circle_x_y_color=get_circle_x_y_color_list(A,xCenter,yCenter,radius)
prev = None
reverse_count = 0
if not circle_x_y_color is None:
median_x= int(statistics.median([x_y[0] for x_y in circle_x_y_color]))
median_y = int(statistics.median([x_y[1] for x_y in circle_x_y_color]))
one,two,three,four =[],[],[],[]
for x_y_color in circle_x_y_color:
x=x_y_color[0]
y=x_y_color[1]
color = x_y_color[2]
if x >= median_x and y < median_y: # 1사분면
one.append([x,y,color])
if x < median_x and y < median_y: # 2사분면
two.append([x, y, color])
if x < median_x and y >= median_y: # 3사분면
three.append([x, y, color])
if x >= median_x and y >= median_y: # 4사분면
four.append([x, y, color])
one = sorted(one, key=lambda x: x[1], reverse=True)
one = sorted(one, key=lambda x: x[0], reverse=True)
two = sorted(two, key=lambda x: x[1])
two = sorted(two, key=lambda x: x[0], reverse=True)
three= sorted(three, key=lambda x: x[1])
three = sorted(three, key=lambda x: x[0])
four = sorted(four, key=lambda x: x[1], reverse=True)
four = sorted(four, key=lambda x: x[0])
new_list=one+two+three+four
for i in new_list:
if i[0]==0 or i[0]==255: # Because of PatchSize is 256
prev = None
elif i[1]==0 or i[1]==255: # Because of PatchSize is 256
prev = None
cur = i[2]
if not prev is None and cur!=prev:
# draw_rectangle(A,i[0],i[1])
reverse_count+=1
prev = cur
# print('reverse count : ',reverse_count)
if reverse_count > 4:
is_joint = True
else :
is_joint = False
return A,is_joint,reverse_count
def get_max_width_x_max_width_y(A,max_width_dict):
if max_width_dict:
values = sorted(max_width_dict.values(),reverse=True)
for top_value in values:
for key,value in max_width_dict.items():
if value==top_value:
x_y=key.split('_')
x ,y = int(x_y[0]),int(x_y[1])
A,is_joint,reverse_count=get_is_joint(A,x,y,value)
# print(is_joint)
if not is_joint :
return x,y,value
for key, value in max_width_dict.items():
if value == values[0]:
x_y = key.split('_')
x, y = int(x_y[0]), int(x_y[1])
# print(x,y,value)
return x, y, value
else:
return 0,0,0
def count_length(arr,base_x,base_y,amount_of_change_x,amount_of_change_y,size_x,size_y):
count = 0
increase = 1
pick_x, pick_y = base_x,base_y
if abs(amount_of_change_x)==1 and abs(amount_of_change_y)==1:
increase = 1.4142
if len(arr.shape)==3:
while arr[pick_y,pick_x][0] != 0:
pick_x += amount_of_change_x
pick_y += amount_of_change_y
if pick_y >= size_y or pick_x >= size_x:
break
if abs(pick_x - base_x) > 30 or abs(pick_y - base_y) > 30:
break
count += increase
else :
while arr[pick_y,pick_x] != 0 :
pick_x +=amount_of_change_x
pick_y +=amount_of_change_y
if pick_y>=size_y or pick_x>=size_x :
break
if abs(pick_x - base_x)>30 or abs(pick_y - base_y)>30:
break
count+=increase
return count
def every_search_get_max_avg_width_of_crack(A):
"""
## INPUT : patch_img_path : 'A'
## OUTPUT : MAX WIDTH , AVERAGE WIDTH , MIN X, MIN Y, MAX X, MAX Y, MAX WIDTH X, MAX WIDTH Y
"""
max_width_dict = {}
avg_crack_width,minx,miny,maxx,maxy =0,0,0,0,0
A_size = A.shape
A_height = A_size[0]
A_width = A_size[1]
# 커널 생성
kernel = np.ones((6, 6), np.uint8)
# para1 : 이미지, para2 : 커널, para3 : erode 반복 횟수
erode = cv2.erode(A, kernel, iterations=1)
index= np.nonzero(erode>0)
index_y = index[0]
index_x = index[1]
nonzero = cv2.countNonZero(erode)
if len(index_x) == 0: ## there are no cracks
# print('there are no cracks.')
return A,avg_crack_width,minx,miny,maxx,maxy,max_width_dict,nonzero
minx, maxx, miny, maxy = min(index_x), max(index_x), min(index_y), max(index_y)
num_of_crack_pixel = 0
crack_width=[]
for x,y in zip(index_x,index_y):
# for x,y in zip([129,129,129,129,129,129,129,129,129,129,129,129],[184,186,188,190,192,194,196,198,200,202,204,206]):
num_of_crack_pixel+=1
top=count_length(A,x,y,0,-1,A_width,A_height)
top_right = count_length(A,x,y,1,-1,A_width,A_height)
right = count_length(A, x, y, 1, 0,A_width,A_height)
right_down = count_length(A, x, y, 1, 1,A_width,A_height)
down = count_length(A, x, y, 0, 1,A_width,A_height)
down_left = count_length(A, x, y, -1, 1,A_width,A_height)
left = count_length(A, x, y, -1, 0,A_width,A_height)
left_top = count_length(A, x, y, -1, -1,A_width,A_height)
horizontal = round(left+right, 2)
vertical = round(top+down,2)
diagonal = round(left_top+right_down,2)
reverse_diagonal = round(top_right+down_left,2)
# print('x : {}, y : {}, horizontal : {}, vertical : {}, diagonal : {}, reverse_diagonal : {} '.format(x,y,horizontal,vertical,diagonal,reverse_diagonal))
min_value=min(horizontal,vertical,diagonal,reverse_diagonal)
if num_of_crack_pixel == 1:
max_width_x = x
max_width_y = y
elif max(crack_width) < min_value:
max_width_x = x
max_width_y = y
max_width_dict['{}_{}'.format(x, y)] = min_value
crack_width.append(min_value)
avg_crack_width = sum(crack_width)/len(crack_width)
# print('max crack width dictionary : ',max_width_dict)
# print('avg crack width : ',avg_crack_width)
# print('number of crack pixels : ',num_of_crack_pixel)
# print('min x y , max x y : ',minx,miny,maxx,maxy)
return A,avg_crack_width,minx,miny,maxx,maxy,max_width_dict,nonzero
def visualize_extended_line(A,base_x,base_y,amount_of_change_x,amount_of_change_y,size_x,size_y,color):
pick_x, pick_y = base_x, base_y
while A[pick_y, pick_x][0] != 0 :
A[pick_y, pick_x] = color
# print(A[pick_y,pick_x])
pick_x += amount_of_change_x
pick_y += amount_of_change_y
if pick_y == size_y or pick_x == size_x:
break
if abs(pick_x - base_x) > 35 or abs(pick_y - base_y) > 35:
break
return A
def get_extended_line_x_y(A,base_x,base_y,amount_of_change_x,amount_of_change_y,size_x,size_y):
pick_x, pick_y = base_x, base_y
if len(A.shape)==3:
while A[pick_y, pick_x][0] != 0:
pick_x += amount_of_change_x
pick_y += amount_of_change_y
if pick_y == size_y:
return pick_x, pick_y - 1
if pick_x == size_x:
return pick_x - 1, pick_y
if abs(pick_x - base_x) > 35 or abs(pick_y - base_y) > 35:
return pick_x, pick_y
else :
while A[pick_y, pick_x]!= 0 :
pick_x += amount_of_change_x
pick_y += amount_of_change_y
if pick_y == size_y :
return pick_x, pick_y-1
if pick_x == size_x:
return pick_x-1, pick_y
if abs(pick_x - base_x) > 35 or abs(pick_y - base_y) > 35:
return pick_x,pick_y
return pick_x,pick_y
def save(A,output_path,fname):
cv2.imwrite(os.path.join(output_path, fname + '.png'), A)
def binaryize(A,thres):
A[A > thres] = 255
A[A <= thres] = 0
def find_min_direction(A,x,y,width,height):
top = count_length(A, x, y, 0, -1, width, height)
top_right = count_length(A, x, y, 1, -1, width,height)
right = count_length(A, x, y, 1, 0, width,height)
right_down = count_length(A, x, y, 1, 1, width,height)
down = count_length(A, x, y, 0, 1, width,height)
down_left = count_length(A, x, y, -1, 1, width,height)
left = count_length(A, x, y, -1, 0, width,height)
left_top = count_length(A, x, y, -1, -1, width,height)
horizontal = left + right
vertical = top + down
diagonal = left_top + right_down
reverse_diagonal = top_right + down_left
four_direction_distance = [horizontal, vertical, diagonal, reverse_diagonal]
min_value = min(four_direction_distance)
min_idx=four_direction_distance.index(min_value)
if min_idx == 0:
return 0 #'horizontal'
elif min_idx ==1:
return 1 #'vertical'
elif min_idx ==2:
return 2 #'diagonal'
elif min_idx ==3:
return 3 #'reverse_diagonal'
return 'idonknow'
def visualize_circle(A,x_y_color):
for xyc in x_y_color:
x=xyc[0]
y=xyc[1]
A[y,x]=(0,255,255)
return A
def full_process(A,count,return_list,patch_size,i_j,kernel_size):
i, j = i_j[0], i_j[1]
A, total_average_width, minx, miny, maxx, maxy, max_width_dict,nonzero = every_search_get_max_avg_width_of_crack(A,kernel_size=kernel_size) # 옛날 함수
if len(A.shape) == 3:
A_height, A_width, _ = A.shape
else :
A_height,A_width= A.shape
max_width_x,max_width_y,total_max_width = get_max_width_x_max_width_y(A,max_width_dict)
directions=[[-1,0,1,0],[0,-1,0,1],[-1,-1,1,1],[1,-1,-1,1]] ## [ 'horizontal', 'vertical', 'diagonal' , 'reverse_diagonal']
direction=find_min_direction(A,max_width_x,max_width_y,A_width,A_height)
line_x2, line_y2 = get_extended_line_x_y(A, max_width_x, max_width_y, directions[direction][2],directions[direction][3], A_width, A_height)
line_x1, line_y1 = get_extended_line_x_y(A, max_width_x, max_width_y, directions[direction][0], directions[direction][1], A_width, A_height)
# print('file name : ', fname)
# print('----------------------------------------------------------------------------')
return_list[count] = {
'x':patch_size*i,
'y':patch_size*j,
'w':patch_size,
'h':patch_size,
'total_max_width':total_max_width,
'total_average_width':total_average_width,
'minx':minx,
'miny':miny,
'maxx':maxx,
'maxy':maxy,
'max_width_x':max_width_x,
'max_width_y':max_width_y,
'line_x1':line_x1,
'line_y1':line_y1,
'line_x2':line_x2,
'line_y2':line_y2,
'nonzero':nonzero,
}
def reverse_binary(A):
uniq=np.unique(A)
A[A==uniq[0]]=128
A[A==uniq[1]]=0
A[A==128]=255
return A
def crack_width_analysis(seg_image, threshold, cls_result_data, patch_size=256,kernel_size=4):
# image = base64.b64decode(seg_image)
# input_img = Image.open(BytesIO(image)).convert('L')
input_img=Image.open(seg_image).convert('L')
display = np.asarray(input_img)
display.flags.writeable = True
display[display<threshold]=0
display[display>=threshold]=1
display=display.astype(np.uint8)
cv2.imwrite("test_" + str(threshold) + ".png", display)
width, height = input_img.size
patch_width = int(width / patch_size)
patch_height = int(height / patch_size)
patch_size = 256
patches = []
i_j = []
jobs = []
for j in tqdm(range(0, patch_height), desc="Divide by Patches"):
for i in range(0, patch_width):
y = patch_size * j
x = patch_size * i
patch_img = display[patch_size * j:patch_size * (j + 1), patch_size * i:patch_size * (i + 1)]
patches.append(patch_img)
i_j.append([i,j])
# cv2.imwrite('{}_{}.jpg'.format(x,y),patch_img)
##########################################################################################################
# for j in tqdm(range(0, patch_height), desc="Divide by Patches"):
# for i in range(0, patch_width):
# y = patch_size * j
# x = patch_size * i
#
# for k in range(len(cls_result_data)) :
# cls_position = cls_result_data[k]['position']
# crack = cls_result_data[k]
# labels = sorted(crack['label'], key=lambda label_list: (label_list['score']), reverse=True)
# if labels[0]['description'] == 'crack' :
# if cls_position['x'] == x and cls_position['y'] == y:
# patch_img = display[patch_size * j:patch_size * (j + 1), patch_size * i:patch_size * (i + 1)]
# patches.append(patch_img)
# i_j.append([i, j])
###########################################################################################################
### ##########################################
### Multi Processing
### ##########################################
manager = multiprocessing.Manager()
full_img_dict = manager.list()
for i in range(0,len(patches)):
full_img_dict.append(None)
for i in tqdm(range(0,len(patches)),desc="Calculating Severity(Multi-processing)"):
p = multiprocessing.Process(target=full_process, args=(patches[i],i,full_img_dict,patch_size,i_j[i],kernel_size))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
# for i in range(len(patches)):
# print(full_img_dict[i])
import csv
for i in tqdm(range(0,len(patches)), desc="Writing Severity Analysis Result"):
x = i_j[i][0]
y = i_j[i][1]
fname = seg_image.split('\\')[-1][:-4]
total_max_width=full_img_dict[i]['total_max_width']
total_average_width = full_img_dict[i]['total_average_width']
nonzero = full_img_dict[i]['nonzero']
f = open('{}_erode_{}.csv'.format(fname,kernel_size), 'a', encoding='utf-8', newline='')
wr = csv.writer(f)
wr.writerow([x,y,total_max_width,total_average_width,nonzero])
f.close()
### ##########################################
### Single Processing
### ##########################################
# full_img_dict = []
# for i in range(0,len(patches)):
# full_img_dict.append(None)
# for i in tqdm(range(0,len(patches)),desc='Calculating Severity(Base)'):
# full_process(patches[i],i,full_img_dict,patch_size,i_j[i])
#
# for dict in full_img_dict:
# for key,value in dict.items():
# if key=='total_max_width':
# if value!=0:
# print(key,value)
# elif key=='total_average_width':
# if value!=0:
# print(key,value)
# for i in range(0,500):
# print(full_img_dict[i])
def main():
from glob import glob
data_root = 'D:\\docker2\\hed\\Predict\\1011BEST_1026_test\\predict'
file_names = glob(os.path.join(data_root,'*.png'))
for file_name in file_names:
# file_name = 'D:\\docker2\\hed\\Predict\\1011BEST_1026_test\\predict\\test.png'
threshold = 239
patch_size= 256
cls_result_data = None
for kernel_size in range(1,7):
crack_width_analysis(file_name, threshold, cls_result_data, patch_size=256,kernel_size=kernel_size)
print('complete.')
if __name__ == '__main__':
main()