-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6. Collagen segmentation in HT.py
More file actions
112 lines (89 loc) · 6.03 KB
/
6. Collagen segmentation in HT.py
File metadata and controls
112 lines (89 loc) · 6.03 KB
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
# %% Import modules
import sys
sys.path.append('/home/choiyoonho/Documents/HT/3D tissue visualization/') # <- Working directory where the Utils.py is located.
from Utils import *
type_colour = {
"0" : 0,
"1" : 1,
"2" : 2,
"3" : 3,
"4" : 1,
"5" : 1
}
# %% Load data
root = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped'
bf_paths = natsort.natsorted(glob(root+'/BF/*'))
ht_paths = natsort.natsorted(glob(root+'/HT/*'))
he_paths = natsort.natsorted(glob(root+'/HE/*'))
jsons = natsort.natsorted((glob('/home/choiyoonho/Documents/HT/HE/Final_data/cropped/Cell_instance/json/*')))
bf_paths = pop_insert(bf_paths, 0, 2)
ht_paths = pop_insert(ht_paths, 0, 2)
focuses = {0:[0,1,2,3,4], 1:[2,3,4,5,6], 2:[2,3,4,5,6], 3:[0,1,2,3,4], 4:[0,1,2,3,4]} # From 7 FSS slices, I manually checked 5 good slides.
integrated_collagen_mask = []
integrated_tumor_mask = []
integrated_lymphocyte_mask = []
integrated_connective_cell_mask = []
for i, [he_path, ht_path, json_path] in enumerate(zip(he_paths, ht_paths, jsons)):
he = cv2.imread(he_path)
he = cv2.cvtColor(he, cv2.COLOR_BGR2RGB)
name = he_path.split('/')[-1].split('.')[0].split('_')[-1]
ht, _ = load_hdf5_with_attributes(ht_path) # HT scale
cell_mask = np.zeros((he.shape[0], he.shape[1]))
bbox_list, centroid_list, contour_list, type_list = read_json(json_path)
coords_xmin, coords_ymin, coords_xmax, coords_ymax = 0, 0, he.shape[1], he.shape[0]
info_dict = get_info_dict(contour_list, coords_xmin, coords_ymin, coords_xmax, coords_ymax)
for idx, [inst_id, inst_info] in enumerate(info_dict.items()):
inst_contour = inst_info["contour"]
if "type" in inst_info and type_colour is not None:
inst_colour = type_colour[inst_info["type"]]
cv2.fillPoly(cell_mask, [np.array(inst_contour)], color=inst_colour)
# show(cell_mask)
tumor_mask = np.stack([cell_mask==1, cell_mask==1, cell_mask==1, cell_mask==1, cell_mask==1], axis=2).astype(np.uint8)
lympho_mask = np.stack([cell_mask==2, cell_mask==2, cell_mask==2, cell_mask==2, cell_mask==2], axis=2).astype(np.uint8)
connect_mask = np.stack([cell_mask==3, cell_mask==3, cell_mask==3, cell_mask==3, cell_mask==3], axis=2).astype(np.uint8)
img = normalization(ht)
img = histogram_equalization(img)
net = hvnet((256, 256), 1)
net.load_weights('/home/choiyoonho/Documents/HT/model_42_5.5830.hdf5')
x_stride = 128
y_stride = 128
denominators = np.zeros((ht.shape[0], ht.shape[1], 5), dtype=np.uint8)
collagen_mask = np.zeros((ht.shape[0], ht.shape[1], 5), dtype=np.float32)
for j,z in enumerate(focuses[i]):
for x in range(0, coords_xmax-128, x_stride):
for y in range(0, coords_ymax-128, y_stride):
temp = img[x:x+256,y:y+256, z]
temp_img = np.expand_dims(temp, -1)
temp_img = np.expand_dims(temp_img, 0)
NP, HV, CLG = net.predict(temp_img)
mask = 1 * (CLG > 0.8) & (~(NP > 0))
collagen = (temp > 0.3) * mask[0, :, :, 0]
collagen_mask[x:x+256,y:y+256, j] = collagen_mask[x:x+256,y:y+256, j] + collagen
denominators[x:x+256,y:y+256, j] = denominators[x:x+256,y:y+256, j] + np.ones((256,256))
final_collagen_mask = collagen_mask / denominators
integrated_collagen_mask.append(final_collagen_mask)
save_to_nii(final_collagen_mask, path='/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/raw/' + name + '_collagen.nii')
# Save Tumor/Lympho/Connect cell mask
tumor_mask = post_process_cell_mask(tumor_mask, final_collagen_mask)
lympho_mask = post_process_cell_mask(lympho_mask, final_collagen_mask)
connect_mask = post_process_cell_mask(connect_mask, final_collagen_mask)
integrated_tumor_mask.append(tumor_mask)
integrated_lymphocyte_mask.append(lympho_mask)
integrated_connective_cell_mask.append(connect_mask)
save_to_nii(tumor_mask,path='/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/raw/' + name + '_tumor.nii')
save_to_nii(lympho_mask, path='/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/raw/' + name + '_lympho.nii')
save_to_nii(connect_mask, path='/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/raw/' + name + '_connect.nii')
final_integrated_collagen_mask = np.concatenate(integrated_collagen_mask, axis=2)
final_integrated_tumor_mask = np.concatenate(integrated_tumor_mask, axis=2)
final_integrated_lymphocyte_mask = np.concatenate(integrated_lymphocyte_mask, axis=2)
final_integrated_connective_mask = np.concatenate(integrated_connective_cell_mask, axis=2)
# %% For entire FOV
save_to_nii(final_integrated_collagen_mask, path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/integrated/integrated_collagen.nii')
save_to_nii(final_integrated_tumor_mask, path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/integrated/integrated_tumor.nii')
save_to_nii(final_integrated_lymphocyte_mask, path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/integrated/integrated_lymphocyte.nii')
save_to_nii(final_integrated_connective_mask, path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/Full_view/integrated/integrated_connective.nii')
# %% For manually selected FOV
save_to_nii(final_integrated_collagen_mask[1000:3000, 1000:3000, :], path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/crop_view/1000-3000/crop_integrated_collagen.nii')
save_to_nii(final_integrated_tumor_mask[1000:3000, 1000:3000, :], path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/crop_view/1000-3000/crop_integrated_tumor.nii')
save_to_nii(final_integrated_lymphocyte_mask[1000:3000, 1000:3000, :], path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/crop_view/1000-3000/crop_integrated_lymphocyte.nii')
save_to_nii(final_integrated_connective_mask[1000:3000, 1000:3000, :], path = '/home/choiyoonho/Documents/HT/HE/Final_data/cropped/3D_masks/crop_view/1000-3000/crop_integrated_connective.nii')