-
Notifications
You must be signed in to change notification settings - Fork 0
/
bignephroqpdata_regions.py
1467 lines (1242 loc) · 55.2 KB
/
bignephroqpdata_regions.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import glob
import os
from shutil import copyfile
import json
from shapely.geometry import mapping, shape
import numpy as np
from PIL import Image, ImageDraw
from matplotlib import pyplot as plt
from matplotlib import cm as colormaps
import cv2
from scipy import ndimage
import copy
import time
import collections
import yaml
import csv
Image.MAX_IMAGE_PIXELS = None
global_counters = []
global_lists = []
global_reshape_w = 0
# proj_name='IgAN-PAS'
proj_name = 'IgAN-regions'
main_folder = 'Bignefro'
def perform_on_whole_dataset(funct, ids=None):
if ids is None:
ids = list(range(1000, 2220))
for i in ids:
if i % 250 == 0:
print(f'now processing image #{i}')
try:
funct(i)
except FileNotFoundError:
# print(f"Error for id {i}:")
# print(e)
# print()
continue
except Exception as e:
print(f"Error for id {i}:")
print(e)
print()
def debug_plot(img, cmap=None):
img = np.array(img)
plt.figure()
plt.imshow(img, cmap=cmap)
plt.show(block=False)
def copy_files():
path = f'E:\{main_folder}\IgAN_annotated (QuPath)\QuPath projects\{proj_name}\data\\'
new_path = f'E:\{main_folder}\\porcherie\\'
files = glob.glob(path + '*\\data.qpdata')
for f in files:
new_fname = f'{os.path.basename(os.path.dirname(f))}.qpdata'
print(f'copying {f} into {new_path}\\{new_fname}')
copyfile(f, new_path + "\\" + new_fname)
def parse_geojsn(file_path, big_size):
labels_dict = {'Glomerulus': 1,
'Artery': 2,
'hilus': 3,
'crescent': 4,
'Vein': 5,
'Medulla': 6,
'Capsule': 7,
'unknown': 8}
img = Image.new('L', big_size, 0)
with open(file_path) as json_file:
data = json.load(json_file)
for polygon in data:
try:
name = polygon["properties"]["name"]
except:
try:
name = polygon["properties"]["classification"]["name"]
except KeyError as e:
print(f'could not get the name of a polygon in {os.path.basename(file_path).split(".")[0]}')
print()
name = 'unknown'
if name in ['discarded', 'mesangial']:
continue
try:
class_name = polygon['properties']['classification']['name']
except KeyError:
class_name = 'unknown'
# POLLO DO WE WANT TO ONLY PARSE SOME CLASSES??
# if class_name not in ['Glomerulus']:
# continue
fill_value = labels_dict.get(class_name)
polygon_shape = shape(polygon['geometry'])
for closed_figure in mapping(polygon_shape)['coordinates']:
# POL: the case len == 2 if handcrafted after seeing 2 single failure cases on arteries in images 3623_pas_Regione 1 and 2891_pas_Regione 0.
if len(closed_figure) in [1, 2]:
closed_figure = closed_figure[0]
try:
ImageDraw.Draw(img).polygon(xy=closed_figure, outline=fill_value, fill=fill_value)
except TypeError:
print(f'could not draw polygon {name} in {os.path.basename(file_path).split(".")[0]}')
return img
def get_region_from_id(data_id):
if data_id > 1000:
data_id -= 1000
proj_name = 'IgAN-regions'
else:
proj_name = 'IgAN-PAS'
path = f'E:/{main_folder}/IgAN_annotated (QuPath)/QuPath projects/{proj_name}/data/{data_id}/'
with open(path + 'server.json') as json_file:
return json.load(json_file)['metadata']['name'].split('.')[0]
def segment_region(data_id):
read_data_id = data_id
if data_id > 1000:
read_data_id = data_id - 1000
path = f'E:/{main_folder}/IgAN_annotated (QuPath)/QuPath projects/{proj_name}/data/{read_data_id}/'
region_name = get_region_from_id(data_id)
imgname_root = f'E:/{main_folder}/thumbnails_processing/id{data_id}_{region_name}'
img = cv2.imread(path + 'thumbnail.jpg', 0)
cv2.imwrite(imgname_root + '_0thumb.png', img)
thresh, bin_img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cv2.imwrite(imgname_root + '_1Otsu.png', bin_img)
try:
bin_img = remove_futile(data_id, region_name, bin_img)
except FileNotFoundError:
get_small_annotation_npy(data_id)
bin_img = remove_futile(data_id, region_name, bin_img)
cv2.imwrite(imgname_root + '_2RemoveFutiles.png', bin_img)
bin_img = cv2.dilate(bin_img, np.ones((11, 11))) # this initial closing step was added later
bin_img = cv2.erode(bin_img, np.ones((11, 11))) # this initial closing step was added later
bin_img = cv2.dilate(bin_img, np.ones((11, 11))) # this initial closing step was added later
bin_img = cv2.erode(bin_img, np.ones((11, 11))) # this initial closing step was added later
cv2.imwrite(imgname_root + '_3TwoRoundsOfClosing.png', bin_img)
bin_img = cv2.erode(bin_img, np.ones((11, 11)))
bin_img = cv2.dilate(bin_img, np.ones((11, 11)))
bin_img = cv2.erode(bin_img, np.ones((11, 11)))
bin_img = cv2.dilate(bin_img, np.ones((11, 11)))
cv2.imwrite(imgname_root + '_4TwoRoundsOfOpening.png', bin_img)
bin_img = cv2.dilate(bin_img, np.ones((11, 11)))
bin_img = cv2.dilate(bin_img, np.ones((11, 11)))
bin_img = cv2.erode(bin_img, np.ones((11, 11)))
cv2.imwrite(imgname_root + '_5DilationPlusClosing.png', bin_img)
bin_img, final_img = merge_otsu_and_annotations(data_id, region_name, bin_img)
cv2.imwrite(imgname_root + '_6MergeGlomeruliPlusFillHoles.png', bin_img)
cv2.imwrite(imgname_root + '_7CCL.png', final_img)
def processandresize_big_image(data_id):
img_name = get_region_from_id(data_id)
nbio = img_name.split('_')[0]
bigimg_path = f'E:/{main_folder}/IgAN_acquired_images_(master)/{nbio}/{img_name}.tif'
big_img = Image.open(bigimg_path)
thumb_path = f'E:/{main_folder}/thumbnails_processing/id{data_id}_{img_name}_0thumb.png'
thumb_img = Image.open(thumb_path)
small_img = big_img.resize(thumb_img.size, resample=Image.BILINEAR)
json_path = f'E:/{main_folder}/json_annotations/{img_name}.txt'
annotations = parse_geojsn(json_path, big_img.size)
small_annotations = annotations.resize(thumb_img.size, resample=Image.NEAREST)
cm_bin = colormaps.get_cmap('viridis')
printable_annotations = np.array(small_annotations)
printable_annotations = printable_annotations * 51
printable_annotations = cm_bin(printable_annotations)[:, :, :-1]
printable_annotations = np.uint8(255 * printable_annotations)
printable_annotations = Image.fromarray(printable_annotations)
# debug_plot(printable_annotations)
# debug_plot(small_img)
printable_annotations.save(f'E:/{main_folder}/tmp/id{data_id}_{img_name}_annotations.png')
small_img.save(f'E:/{main_folder}/tmp/id{data_id}_{img_name}_resized.png')
def fix_json_file(data_id):
img_name = get_region_from_id(data_id)
json_path = f'E:/{main_folder}/json_annotations/{img_name}.txt'
try:
with open(json_path, 'r') as json_file:
json.load(json_file)
except json.decoder.JSONDecodeError:
print(data_id)
with open(json_path, 'r') as json_file:
temp = json_file.read().replace("[,{", "[{")
with open(json_path, 'w') as json_file:
json_file.write(temp)
with open(json_path, 'r') as json_file:
json.load(json_file)
def get_small_annotation_npy(data_id):
img_name = get_region_from_id(data_id)
nbio = img_name.split('_')[0]
bigimg_path = f'E:/{main_folder}/IgAN_acquired_images_(master)/{nbio}/{img_name}.tif'
big_img = Image.open(bigimg_path)
thumb_path = f'E:/{main_folder}/thumbnails_processing/id{data_id}_{img_name}_0thumb.png'
thumb_img = Image.open(thumb_path)
# small_img = big_img.resize(thumb_img.size, resample=Image.BILINEAR)
json_path = f'E:/{main_folder}/json_annotations/{img_name}.txt'
annotations = parse_geojsn(json_path, big_img.size)
small_annotations = annotations.resize(thumb_img.size, resample=Image.NEAREST)
small_annotation_npy = np.asarray(small_annotations)
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{img_name}_small.npy'
# debug_plot(small_annotation_npy)
np.save(np_path, small_annotation_npy)
def merge_otsu_and_annotations(data_id, img_name, img):
global global_counters
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{img_name}_small.npy'
small_annotation_npy = np.load(np_path)
glomeruli = (small_annotation_npy == 1).astype(np.uint8)
img //= 255
glomeruli_check = np.sum(glomeruli) - np.sum(img * glomeruli)
# debug_plot(small_otsu, cmap='gray')
# debug_plot(mask, cmap='gray')
if glomeruli_check > 0:
global_counters[-2] += 1
global_counters[-1] += glomeruli_check
print(f'{glomeruli_check} pixels from glomeruli were dropped in image {data_id}_{img_name}')
# debug_plot(mask, cmap='gray')
# debug_plot(glomeruli, cmap='gray')
# debug_plot(glomeruli - mask * glomeruli, cmap='gray')
bin_img = (np.logical_or(img, glomeruli) * 255).astype(np.uint8)
bin_img[ndimage.binary_fill_holes(bin_img)] = 255
final_img = bin_img.copy()
final_img = remove_small_CCL(data_id, img_name, final_img, glomeruli)
# debug_plot(bin_img, 'gray')
# debug_plot(final_img, 'gray')
return bin_img, final_img
def remove_futile(data_id, img_name, img):
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{img_name}_small.npy'
small_annotation_npy = np.load(np_path)
small_otsu = img // 255
futile = (small_annotation_npy == 6).astype(np.uint8) + (small_annotation_npy == 7).astype(np.uint8)
return (small_otsu * np.logical_not(futile) * 255).astype(np.uint8)
def remove_files(folder, str):
global global_counters
global_counters.append(0)
files_2remove = glob.glob(folder + f'*{str}*')
for f in files_2remove:
if str in f:
global_counters[-1] += 1
os.remove(f)
def rename_files(folder, str, replace_str):
global global_counters
global_counters.append(0)
files_2remove = glob.glob(folder + f'*{str}*')
for f in files_2remove:
if str in f:
global_counters[0] += 1
os.rename(f, f.replace(str, replace_str))
# print(f, f.replace(str, replace_str))
def remove_small_CCL(*args, **kwargs):
if proj_name == 'IgAN-PAS':
return remove_small_CCL_w_annotations(*args, **kwargs)
else:
return remove_small_CCL_NO_annotations(*args, **kwargs)
def remove_small_CCL_w_annotations(data_id, img_name, img, glomeruli):
global global_counters
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8)
# just taking out the background
sizes = stats[1:, -1]
nb_components = nb_components - 1
# minimum size of particles we want to keep (number of pixels)
# here, it's a fixed value, but you can set it as you want, eg the mean of the sizes or whatever
min_size = sizes.max() // 5
# for every component in the image, you keep it only if it's above min_size
for i in range(0, nb_components):
single_CC = np.zeros((output.shape))
single_CC[output == i + 1] = 1
if not np.sum(single_CC * glomeruli):
img[output == i + 1] = 0
global_counters[-2] += 1
if sizes[i] >= min_size:
print(f'image id{data_id}_{img_name} contains a big glomerulusless CC')
# else:
# print(f'image id{data_id}_{img_name} contains a detatched glomerulus')
return img
def remove_small_CCL_NO_annotations(data_id, img_name, img, glomeruli):
global global_counters
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8)
# just taking out the background
sizes = stats[1:, -1]
nb_components = nb_components - 1
for i in range(0, nb_components):
single_CC = np.zeros((output.shape))
single_CC[output == i + 1] = 1
if np.sum(single_CC) < (single_CC.size // 20):
img[output == i + 1] = 0
global_counters[-2] += 1
# else:
# print(f'image id{data_id}_{img_name} contains a detatched glomerulus')
return img
def get_polygon_from_WTK(file_lines, glname):
for i in range(len(file_lines)):
stuff = file_lines[i].split(' ')
if stuff[0] == glname:
type = stuff[1].replace('(', '').replace(')', '')
obj_name = stuff[2].replace('(', '').replace(')\n', '')
if type != 'Polygon' or obj_name != 'Glomerulus':
raise KeyError("SOMETHING WENT WRONG")
line = file_lines[i + 1]
break
line_start = 'POLYGON (('
line_end = '))\n'
if not line.startswith(line_start) or not line.endswith(line_end):
raise KeyError("SOMETHING WENT WRONG")
line = line.replace(line_start, '').replace(line_end, '')
points = line.split(', ')
json_points = [[int(el) for el in couple.split(' ')] for couple in points]
d = {"type": type,
"coordinates": [json_points]}
return d
def add_single_glomerulus(filename, glname):
print(f'filename: {filename} | glname: {glname}')
data_root = f'E:/{main_folder}/'
jsons_root = data_root + 'json_annotations/'
new_jsons_root = data_root + 'new_json_annotations/'
WTK_root = data_root + 'tmp_annotations/'
with open(WTK_root + filename) as WTK_file:
WTK_lines = WTK_file.readlines()
with open(jsons_root + filename) as json_file:
data = json.load(json_file)
for el in data:
try:
obj_name = el['properties']['classification']['name']
except Exception:
continue
if obj_name == 'Glomerulus':
new_el = el
break
new_el['properties']['name'] = glname
new_el['geometry'] = get_polygon_from_WTK(WTK_lines, glname)
data.append(new_el)
with open(new_jsons_root + filename, 'w') as new_json_file:
json.dump(data, new_json_file)
def WTK2Json():
data_root = f'E:/{main_folder}/'
with open(data_root + 'valid_jsons_logs.txt', 'r') as logsfile:
lines = logsfile.readlines()
lines = [line for line in lines if line != '\n']
line_start = "INFO: ERROR in "
i = 0
while i < len(lines):
if lines[i].startswith(line_start):
filename = lines[i].replace(line_start, '')[:-1]
i += 1
glname = lines[i].split(' ')[4]
add_single_glomerulus(filename, glname)
i += 1
return
def check_jsons(path):
files = glob.glob(path + '*.txt')
for f in files:
with open(f) as jsonfile:
data = json.load(jsonfile)
print(f'file {os.path.basename(f)} contains {len(data)} shapes')
def get_rotation_angle(img):
gray_thresh = img.astype(np.double)
col_mask = np.arange(gray_thresh.shape[1])
col_mask = np.expand_dims(col_mask, 0)
col_mask = np.repeat(col_mask, repeats=gray_thresh.shape[0], axis=0)
col_mask = col_mask.astype(np.double)
row_mask = np.arange(gray_thresh.shape[0])
row_mask = np.expand_dims(row_mask, 1)
row_mask = np.repeat(row_mask, repeats=gray_thresh.shape[1], axis=1)
row_mask = row_mask.astype(np.double)
M00 = np.sum(gray_thresh)
M01 = np.sum((col_mask ** 0) * (row_mask ** 1) * gray_thresh)
M10 = np.sum((col_mask ** 1) * (row_mask ** 0) * gray_thresh)
M11 = np.sum((col_mask ** 1) * (row_mask ** 1) * gray_thresh)
M02 = np.sum((col_mask ** 0) * (row_mask ** 2) * gray_thresh)
M20 = np.sum((col_mask ** 2) * (row_mask ** 0) * gray_thresh)
xm = M10 / M00
ym = M01 / M00
u20 = M20 / M00 - (xm ** 2)
u02 = M02 / M00 - (ym ** 2)
u11 = M11 / M00 - (xm * ym)
lambda1 = (u20 + u02) / 2 + np.sqrt(4 * u11 ** 2 + (u20 - u02) ** 2) / 2
lambda2 = (u20 + u02) / 2 - np.sqrt(4 * u11 ** 2 + (u20 - u02) ** 2) / 2
d = np.sqrt(M00 * np.sqrt(lambda1 * lambda2) / np.pi)
dmin = d / np.sqrt(lambda1)
dmag = d / np.sqrt(lambda2)
angle = 0.5 * np.arctan2(2 * u11, (u20 - u02))
angle = angle * 180 / np.pi
return angle, xm, ym
def custom_rotate(img, angle, cx, cy):
# image_center = cx, cy
cx = int(cx)
cy = int(cy)
h, w = img.shape[:2]
if len(img.shape) == 3:
assert img.shape[2] == 3
fill_value = (255, 255, 255)
resample_mode = Image.BILINEAR
else:
fill_value = 0
resample_mode = Image.NEAREST
# pad to center image
pad_dist = int(np.abs(cx - w / 2) * 2)
if cx - w / 2 > 0:
right = pad_dist
left = 0
else:
left = pad_dist
right = 0
pad_dist = int(np.abs(cy - h / 2) * 2)
if cy - h / 2 > 0:
bot = pad_dist
top = 0
else:
bot = pad_dist
top = 0
result = cv2.copyMakeBorder(img, top, bot, left, right, cv2.BORDER_CONSTANT, value=fill_value)
# pad to not crop during rotate
diag = np.sqrt(h ** 2 + w ** 2)
if diag > result.shape[1]:
left = right = int(diag - img.shape[1]) // 2
result = cv2.copyMakeBorder(result, 0, 0, left, right, cv2.BORDER_CONSTANT, value=fill_value)
# rotate with opencv fails for very big images
# rot_mat = cv2.getRotationMatrix2D((result.shape[1] / 2, result.shape[0] / 2), angle, 1.0)
# result = cv2.warpAffine(result, rot_mat, result.shape[1::-1], flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=fill_value)
# therefore, rotate image with PIL
try:
result = Image.fromarray(result)
except ValueError:
result = create_big_PIL_image(result, 4)
result = result.rotate(angle, resample=resample_mode, fillcolor=fill_value)
# result = np.array(result)
return result
def invert_custom_rotate(img, angle, cx, cy, patch_annotation):
# image_center = cx, cy
cx = int(cx)
cy = int(cy)
h, w = img.shape[:2]
if len(patch_annotation.shape) == 3:
assert patch_annotation.shape[2] == 3
fill_value = (255, 255, 255)
resample_mode = Image.BILINEAR
else:
fill_value = 0
resample_mode = Image.NEAREST
# pad to center image
pad_dist = int(np.abs(cx - w / 2) * 2)
if cx - w / 2 > 0:
right = pad_dist
left = 0
else:
left = pad_dist
right = 0
pad_dist = int(np.abs(cy - h / 2) * 2)
if cy - h / 2 > 0:
bot = pad_dist
top = 0
else:
bot = pad_dist
top = 0
result = cv2.copyMakeBorder(img, top, bot, left, right, cv2.BORDER_CONSTANT, value=fill_value)
# pad to not crop during rotate
diag = np.sqrt(h ** 2 + w ** 2)
if diag > result.shape[1]:
left += int(diag - img.shape[1]) // 2
right += int(diag - img.shape[1]) // 2
# rotate with opencv fails for very big images
# rot_mat = cv2.getRotationMatrix2D((result.shape[1] / 2, result.shape[0] / 2), angle, 1.0)
# result = cv2.warpAffine(result, rot_mat, result.shape[1::-1], flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=fill_value)
# therefore, rotate image with PIL
try:
patch_annotation = Image.fromarray(patch_annotation)
except ValueError:
patch_annotation = create_big_PIL_image(patch_annotation, 4)
patch_annotation = patch_annotation.rotate(-angle, resample=resample_mode, fillcolor=fill_value)
w, h = patch_annotation.size
patch_annotation = patch_annotation.crop((left, top, w - right, h - bot))
# result = np.array(result)
return patch_annotation
def merge_annotations_frompatches(data_id):
global_lists = [[], [], [], []]
region_name = get_region_from_id(data_id)
nbio = region_name.split('_')[0]
mask_path = f'E:/{main_folder}/thumbnails_processing/id{data_id}_{region_name}_7CCL.png'
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{region_name}_small.npy'
img_path = f'E:/{main_folder}/tmp/id{data_id}_{region_name}_resized.png'
bigimg_path = f'E:/{main_folder}/IgAN_acquired_images_(master)/{nbio}/{region_name}.tif'
big_img_size = Image.open(bigimg_path).size
mask = cv2.imread(mask_path, 0)
# pick current CC only
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(mask, connectivity=8)
# just taking out the background
nb_components = nb_components - 1
orig_img = cv2.imread(img_path)
orig_img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB)
inverted_gt = np.zeros(shape=orig_img.shape[:-1])
for i in range(0, nb_components):
img = orig_img.copy()
glomeruli = np.where(np.load(np_path) == 1, 1, 0)
ratio_h, ratio_w = np.array([big_img_size[1], big_img_size[0]]) / np.array(img.shape[:2])
patches = [] # patches (bounding boxes) are saved as [x0, y0, w, h]
single_CC_mask = np.zeros((output.shape))
single_CC_mask[output == i + 1] = 1
# rotate over momentum axis
angle, center_x, center_y = get_rotation_angle(single_CC_mask)
single_CC_mask = np.array(custom_rotate(single_CC_mask, angle, center_x, center_y)).astype(np.uint8)
img = np.array(custom_rotate(img, angle, center_x, center_y))
glomeruli = np.array(custom_rotate(glomeruli, angle, center_x, center_y))
single_CC_glomeruli = (glomeruli * single_CC_mask).astype(np.uint8)
# get bounding box of current ROTATED connected component
_, rotated_output, rotated_stats, rotated_centroids = cv2.connectedComponentsWithStats(single_CC_mask, connectivity=8)
cc_stats = rotated_stats[1]
bb_l, bb_t, bb_w, bb_h = cc_stats[:4]
if single_CC_glomeruli.max() == 0:
gl_mean_diameter = 14
gl_big_diameter = 28
elif proj_name != 'IgAN-PAS':
print("oooops")
else:
# get biggest glomerulus
_, _, gl_stats, _ = cv2.connectedComponentsWithStats(single_CC_glomeruli, connectivity=8)
gl_big_diameter = 2 * np.sqrt(gl_stats[1:, -1].max() / np.pi).astype(int)
gl_mean_diameter = 2 * np.sqrt(gl_stats[1:, -1].mean() / np.pi).astype(int)
# check if CC is too small to make a patch for it
if 10 * gl_big_diameter ** 2 > bb_h * bb_w:
continue
w = int(12.5 * gl_mean_diameter)
h = min(w, bb_h)
if bb_h < 1.5 * w and bb_h > w:
w = h = bb_h
global_lists[0].append(h * ratio_h)
global_lists[1].append(w * ratio_w)
global_lists[2].append(h * ratio_h * w * ratio_w)
# print(f'one bounding box would be {w * h / bb_w / bb_h} of the whole foreground')
# TODO add check on bb size given the original image size
if bb_w * bb_h < 1.75 * w * h:
patches.append([bb_l, bb_t, bb_w, bb_h])
else:
# make grid
# find cool d
reps_w = bb_w // w
d_w = 0
while d_w < gl_big_diameter:
reps_w += 1
d_w = int((w * reps_w - bb_w) / (reps_w - 1))
reps_h = bb_h // h
d_h = 0
while d_h < gl_big_diameter and bb_h != h:
reps_h += 1
d_h = int((h * reps_h - bb_h) / (reps_h - 1))
c_x, c_y = bb_l, bb_t # find current x and y
# patches.append([c_x, c_y, w, h])
w_step_counter = 0
while w_step_counter < reps_w: # move along x
c_y = cc_stats[1]
h_step_counter = 0
while h_step_counter < reps_h: # move along y
current_patch = single_CC_mask[c_y:c_y + h - d_h, c_x:c_x + w - d_w]
# debug_plot(current_patch, cmap='gray')
fullness = np.mean(current_patch)
if fullness > 0.3:
patches.append([c_x, c_y, w, h])
c_y = c_y + h - d_h
h_step_counter += 1
# if c_y > bb_t + bb_h or bb_h == h:
# break
c_x = c_x + w - d_w
w_step_counter += 1
# create whole_slide annotation
glomeruli_root_path = f'E:/{main_folder}/patches_dataset/detected_glomeruli/id{data_id}_{region_name}_CC{i}'
full_glomeruli_annotation = np.zeros(shape=img.shape[:-1])
for j, bbox in enumerate(patches):
tmp_glomeruli_annotation = np.zeros_like(full_glomeruli_annotation)
# cv2.imwrite(patch_root_path + f'_patch_{j}.png', img_patch)
gt_patch = np.load(glomeruli_root_path + f'_patch_{j}.npy')
x, y, w, h = bbox
gt_patch = cv2.resize(gt_patch, dsize=(w, h), interpolation=cv2.INTER_NEAREST)
try:
tmp_glomeruli_annotation[y:y + h, x: x + w] = gt_patch
except ValueError:
lim_h, lim_w = tmp_glomeruli_annotation[y:y + h, x: x + w].shape
tmp_glomeruli_annotation[y:y + h, x: x + w] = gt_patch[:lim_h, :lim_w]
full_glomeruli_annotation += tmp_glomeruli_annotation
cc_annotation = invert_custom_rotate(orig_img, angle, center_x, center_y, full_glomeruli_annotation)
inverted_gt += cc_annotation
inverted_gt = np.clip(inverted_gt, 0, 1)
# debug_plot(inverted_gt)
# debug_plot(orig_img)
return inverted_gt
def draw_patches(bboxes, img):
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)]
for i, bbox in enumerate(bboxes):
x, y, w, h = bbox
color = colors[i % len(colors)]
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
return img
def process_patches(data_id):
global_lists = [[], [], [], []]
region_name = get_region_from_id(data_id)
nbio = region_name.split('_')[0]
mask_path = f'E:/{main_folder}/thumbnails_processing/id{data_id}_{region_name}_7CCL.png'
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{region_name}_small.npy'
img_path = f'E:/{main_folder}/tmp/id{data_id}_{region_name}_resized.png'
bigimg_path = f'E:/{main_folder}/IgAN_acquired_images_(master)/{nbio}/{region_name}.tif'
big_img_size = Image.open(bigimg_path).size
mask = cv2.imread(mask_path, 0)
# pick current CC only
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(mask, connectivity=8)
# just taking out the background
nb_components = nb_components - 1
for i in range(0, nb_components):
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
glomeruli = np.where(np.load(np_path) == 1, 1, 0)
ratio_h, ratio_w = np.array([big_img_size[1], big_img_size[0]]) / np.array(img.shape[:2])
patches = [] # patches (bounding boxes) are saved as [x0, y0, w, h]
single_CC_mask = np.zeros((output.shape))
single_CC_mask[output == i + 1] = 1
# rotate over momentum axis
angle, center_x, center_y = get_rotation_angle(single_CC_mask)
single_CC_mask = np.array(custom_rotate(single_CC_mask, angle, center_x, center_y)).astype(np.uint8)
img = np.array(custom_rotate(img, angle, center_x, center_y))
glomeruli = np.array(custom_rotate(glomeruli, angle, center_x, center_y))
single_CC_glomeruli = (glomeruli * single_CC_mask).astype(np.uint8)
# get bounding box of current ROTATED connected component
_, rotated_output, rotated_stats, rotated_centroids = cv2.connectedComponentsWithStats(single_CC_mask, connectivity=8)
cc_stats = rotated_stats[1]
bb_l, bb_t, bb_w, bb_h = cc_stats[:4]
if single_CC_glomeruli.max() == 0:
gl_mean_diameter = 14
gl_big_diameter = 28
elif proj_name != 'IgAN-PAS':
print("oooops")
else:
# get biggest glomerulus
_, _, gl_stats, _ = cv2.connectedComponentsWithStats(single_CC_glomeruli, connectivity=8)
gl_big_diameter = 2 * np.sqrt(gl_stats[1:, -1].max() / np.pi).astype(int)
gl_mean_diameter = 2 * np.sqrt(gl_stats[1:, -1].mean() / np.pi).astype(int)
# check if CC is too small to make a patch for it
if 10 * gl_big_diameter ** 2 > bb_h * bb_w:
continue
w = int(12.5 * gl_mean_diameter)
h = min(w, bb_h)
if bb_h < 1.5 * w and bb_h > w:
w = h = bb_h
global_lists[0].append(h * ratio_h)
global_lists[1].append(w * ratio_w)
global_lists[2].append(h * ratio_h * w * ratio_w)
# print(f'one bounding box would be {w * h / bb_w / bb_h} of the whole foreground')
# TODO add check on bb size given the original image size
if bb_w * bb_h < 1.75 * w * h:
patches.append([bb_l, bb_t, bb_w, bb_h])
else:
# make grid
# find cool d
reps_w = bb_w // w
d_w = 0
while d_w < gl_big_diameter:
reps_w += 1
d_w = int((w * reps_w - bb_w) / (reps_w - 1))
reps_h = bb_h // h
d_h = 0
while d_h < gl_big_diameter and bb_h != h:
reps_h += 1
d_h = int((h * reps_h - bb_h) / (reps_h - 1))
c_x, c_y = bb_l, bb_t # find current x and y
# patches.append([c_x, c_y, w, h])
w_step_counter = 0
while w_step_counter < reps_w: # move along x
c_y = cc_stats[1]
h_step_counter = 0
while h_step_counter < reps_h: # move along y
current_patch = single_CC_mask[c_y:c_y + h - d_h, c_x:c_x + w - d_w]
# debug_plot(current_patch, cmap='gray')
fullness = np.mean(current_patch)
if fullness > 0.3:
patches.append([c_x, c_y, w, h])
c_y = c_y + h - d_h
h_step_counter += 1
# if c_y > bb_t + bb_h or bb_h == h:
# break
c_x = c_x + w - d_w
w_step_counter += 1
img = draw_patches(patches, img)
angle = -angle
# single_CC_mask = np.array(custom_rotate(single_CC_mask, angle, img.shape[1] / 2, img.shape[0] / 2))
img = np.array(custom_rotate(img, angle, img.shape[1] / 2, img.shape[0] / 2))
# glomeruli = np.array(custom_rotate(glomeruli, angle, img.shape[1] / 2, img.shape[0] / 2))
writable_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite(f'E:/{main_folder}/tmp/id{data_id}_{region_name}_8BBS_{i}.png', writable_img)
def create_patches_dataset(data_id):
global_lists = [[], [], []]
global reshape_w
region_name = get_region_from_id(data_id)
nbio = region_name.split('_')[0]
mask_path = f'E:/{main_folder}/thumbnails_processing/id{data_id}_{region_name}_7CCL.png'
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{region_name}_small.npy'
json_path = f'E:/{main_folder}/json_annotations/{region_name}.txt'
img_path = f'E:/{main_folder}/tmp/id{data_id}_{region_name}_resized.png'
bigimg_path = f'E:/{main_folder}/IgAN_acquired_images_(master)/{nbio}/{region_name}.tif'
original_big_img = Image.open(bigimg_path)
big_img_size = original_big_img.size
mask = cv2.imread(mask_path, 0)
# pick current CC only
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(mask, connectivity=8)
# just taking out the background
nb_components = nb_components - 1
# original_big_glomeruli = parse_geojsn(json_path, big_img_size)
#
# original_big_img = np.array(original_big_img)
# original_big_img = cv2.cvtColor(original_big_img, cv2.COLOR_RGB2BGR)
# original_big_glomeruli = np.array(original_big_glomeruli)
for i in range(0, nb_components):
# if i == nb_components - 1:
# big_img = original_big_img
# big_glomeruli = original_big_glomeruli
# else:
# big_img = copy.deepcopy(original_big_img)
# big_glomeruli = copy.deepcopy(original_big_glomeruli)
big_img = Image.open(bigimg_path)
big_glomeruli = parse_geojsn(json_path, big_img_size)
big_img = np.array(big_img)
big_img = cv2.cvtColor(big_img, cv2.COLOR_RGB2BGR)
big_glomeruli = np.where(np.array(big_glomeruli) == 1, 1, 0)
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
glomeruli = np.where(np.load(np_path) == 1, 1, 0)
ratio_h, ratio_w = np.array([big_img_size[1], big_img_size[0]]) / np.array(img.shape[:2])
patches = [] # patches (bounding boxes) are saved as [x0, y0, w, h]
single_CC_mask = np.zeros((output.shape))
single_CC_mask[output == i + 1] = 1
# crop single CC, added to handle very small CCs
_, _, small_cc_stats, _ = cv2.connectedComponentsWithStats(single_CC_mask.astype(np.uint8), connectivity=8)
cc_l, cc_t, cc_w, cc_h, _ = small_cc_stats[1]
# if cc_w * cc_h * 5 < single_CC_mask.size:
single_CC_mask = single_CC_mask[cc_t:cc_t + cc_h, cc_l:cc_l + cc_w]
img = img[cc_t:cc_t + cc_h, cc_l:cc_l + cc_w]
glomeruli = glomeruli[cc_t:cc_t + cc_h, cc_l:cc_l + cc_w]
cc_l, cc_w = int(cc_l * ratio_w), int(cc_w * ratio_w)
cc_t, cc_h = int(cc_t * ratio_h), int(cc_h * ratio_h)
big_glomeruli = big_glomeruli[cc_t:cc_t + cc_h, cc_l:cc_l + cc_w]
big_img = big_img[cc_t:cc_t + cc_h, cc_l:cc_l + cc_w]
# rotate over momentum axis
angle, center_x, center_y = get_rotation_angle(single_CC_mask)
single_CC_mask = np.array(custom_rotate(single_CC_mask, angle, center_x, center_y)).astype(np.uint8)
img = np.array(custom_rotate(img, angle, center_x, center_y))
glomeruli = np.array(custom_rotate(glomeruli, angle, center_x, center_y))
single_CC_glomeruli = (glomeruli * single_CC_mask).astype(np.uint8)
big_glomeruli = custom_rotate(big_glomeruli, angle, center_x * ratio_w, center_y * ratio_h)
big_img = custom_rotate(big_img, angle, center_x * ratio_w, center_y * ratio_h)
# get bounding box of current ROTATED connected component
_, rotated_output, rotated_stats, rotated_centroids = cv2.connectedComponentsWithStats(single_CC_mask, connectivity=8)
cc_stats = rotated_stats[1]
bb_l, bb_t, bb_w, bb_h = cc_stats[:4]
if single_CC_glomeruli.max() == 0:
gl_mean_diameter = 14
gl_big_diameter = 28
elif proj_name != 'IgAN-PAS':
print("oooops")
else:
# get biggest glomerulus
_, _, gl_stats, _ = cv2.connectedComponentsWithStats(single_CC_glomeruli, connectivity=8)
gl_big_diameter = 2 * np.sqrt(gl_stats[1:, -1].max() / np.pi).astype(int)
gl_mean_diameter = 2 * np.sqrt(gl_stats[1:, -1].mean() / np.pi).astype(int)
# check if CC is too small to make a patch for it
if 10 * gl_big_diameter ** 2 > bb_h * bb_w:
continue
w = int(12.5 * gl_mean_diameter)
h = min(w, bb_h)
if bb_h < 1.5 * w and bb_h > w:
w = h = bb_h
# global_lists[0].append(h * ratio_h)
# global_lists[1].append(w * ratio_w)
# global_lists[2].append(h * ratio_h * w * ratio_w)
# print(f'one bounding box would be {w * h / bb_w / bb_h} of the whole foreground')
# TODO add check on bb size given the original image size
if bb_w * bb_h < 1.75 * w * h:
patches.append([bb_l, bb_t, bb_w, bb_h])
else:
# make grid
# find cool d
reps_w = bb_w // w
d_w = 0
while d_w < gl_big_diameter:
reps_w += 1
d_w = int((w * reps_w - bb_w) / (reps_w - 1))
reps_h = bb_h // h
d_h = 0
while d_h < gl_big_diameter and bb_h != h:
reps_h += 1
d_h = int((h * reps_h - bb_h) / (reps_h - 1))
c_x, c_y = bb_l, bb_t # find current x and y
# patches.append([c_x, c_y, w, h])
w_step_counter = 0
while w_step_counter < reps_w: # move along x
c_y = cc_stats[1]
h_step_counter = 0
while h_step_counter < reps_h: # move along y
current_patch = single_CC_mask[c_y:c_y + h - d_h, c_x:c_x + w - d_w]
# debug_plot(current_patch, cmap='gray')
fullness = np.mean(current_patch)
if fullness > 0.3:
patches.append([c_x, c_y, w, h])
c_y = c_y + h - d_h
h_step_counter += 1
# if c_y > bb_t + bb_h or bb_h == h:
# break
c_x = c_x + w - d_w
w_step_counter += 1
patch_root_path = f'E:/{main_folder}/patches_dataset/images/id{data_id}_{region_name}_CC{i}'
gt_root_path = f'E:/{main_folder}/patches_dataset/gts/id{data_id}_{region_name}_CC{i}'
for j, bbox in enumerate(patches):
x, y, w, h = bbox
crop_rectangle = [x * ratio_w, y * ratio_h, x * ratio_w + w * ratio_w, y * ratio_h + h * ratio_h]
crop_rectangle = tuple([int(f) for f in crop_rectangle])
img_patch = np.array(big_img.crop(crop_rectangle), dtype=np.uint8)
# img_patch = cv2.cvtColor(img_patch, cv2.COLOR_BGR2RGB)
gt_patch = np.array(big_glomeruli.crop(crop_rectangle), dtype=np.uint8)
reshape_ratio = global_reshape_w / w / ratio_w
if reshape_ratio < 1:
img_patch = cv2.resize(img_patch, (0, 0), fx=reshape_ratio, fy=reshape_ratio)
gt_patch = cv2.resize(gt_patch, (0, 0), fx=reshape_ratio, fy=reshape_ratio)
# img_patch = cv2.cvtColor(img_patch, cv2.COLOR_RGB2BGR)
# debug_plot(img_patch)
# debug_plot(cv2.cvtColor(img_patch, cv2.COLOR_RGB2BGR))
# cv2.imwrite('RGB.png', img_patch)
# cv2.imwrite('BGR.png', cv2.cvtColor(img_patch, cv2.COLOR_RGB2BGR))
# return
# debug_plot(gt_patch)
cv2.imwrite(patch_root_path + f'_patch_{j}.png', img_patch)
np.save(gt_root_path + f'_patch_{j}_gt.npy', gt_patch)
# debug_plot(draw_patches(patches, img))
def add_glomeruli(data_id, glomeruli_gt):
img_name = get_region_from_id(data_id)
np_path = f'E:/{main_folder}/small_npys/id{data_id}_{img_name}_small.npy'
new_np_path = f'E:/{main_folder}/small_npys_wdetectedglom/id{data_id}_{img_name}_small_wgl.npy'
manual_gt = np.load(np_path)
full_annotation = np.where(glomeruli_gt == 1, 1, manual_gt)
np.save(new_np_path, full_annotation.astype(np.uint8))
def create_big_PIL_image(big_arr, n):
big_img = Image.new('RGB', (big_arr.shape[1], big_arr.shape[0]))
x_offset = 0
for im in np.array_split(big_arr, n, axis=1):
pimg = Image.fromarray(im)
temp_box = (x_offset, 0)
big_img.paste(pimg, temp_box)
x_offset += pimg.size[0]
return big_img
class Tree(collections.defaultdict):
def __init__(self, value=None):
super(Tree, self).__init__(Tree)
self.value = value
def _get_el(self, i):
return list(self.keys())[i], self[list(self.keys())[i]]
def get_node(self, i):
return self[list(self.keys())[i]]
def is_leaf(self, el):
if 'images' in el:
return True
return False
def get_first_leaf(self):
new_el = self.get_node(0)
if self.is_leaf(new_el):
return new_el
else:
return new_el.get_first_leaf()
def get_subtree(self, idxs):
subtree = Tree()
for idx in idxs:
k, v = self._get_el(idx)
subtree[k] = v