-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_table1_thickness.py
78 lines (60 loc) · 2.89 KB
/
create_table1_thickness.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
import numpy as np
from skimage import filters
from skimage import measure
import scipy.spatial
import h5py
# Calculate thickness of a mesh
def mesh_thickness(verts, normals):
kdtree = scipy.spatial.KDTree(verts)
mindist = np.zeros(verts.shape[0])
# For every vertex, find the closest point more or less along the vertex normal
for i in range(verts.shape[0]):
# print(i)
nn = kdtree.query_ball_point(verts[i], 5)
nn_verts = verts[[x for x in nn if x != i]]
# Find angle between normal and vector pointing towards neighbour
direction = verts[i] - nn_verts
direction_norm = np.linalg.norm(direction,axis=1)
angle = np.arccos(np.clip((normals[[i]] * direction).sum(axis=1) / direction_norm, -1, 1))
mask = angle<np.pi/6
# If no points are found (within 5mm), set to nan
if mask.sum() == 0:
mindist[i] = np.nan
else:
mindist[i] = direction_norm[mask].min()
# Return thickness statistics
return mindist[~np.isnan(mindist)].mean(), mindist[~np.isnan(mindist)].std()
#%%
with open('./images/table1_thickness.csv', 'w') as fp:
fp.write('P,sequence,f_mean,t_mean\n')
f_dess_mean = 0
t_dess_mean = 0
f_mess_mean = 0
t_mess_mean = 0
for P in [1,2,3,4,5]:
print(P)
with h5py.File(f'./data/P{P}_seg.h5', 'r') as f:
segF_MESS = np.array(f['mess_femoral_cartilage'])
segT_MESS = np.array(f['mess_tibial_cartilage'])
segF_DESS = np.array(f['dess_femoral_cartilage'])
segT_DESS = np.array(f['dess_tibial_cartilage'])
segFs = filters.gaussian(segF_MESS, sigma=1)
verts, faces, normals, values = measure.marching_cubes(segFs, 0.5,spacing=(0.65,0.5,0.5))
tF_MESS, sF_MESS = mesh_thickness(verts, normals)
segFs = filters.gaussian(segF_DESS, sigma=1)
verts, faces, normals, values = measure.marching_cubes(segFs, 0.5,spacing=(0.65,0.5,0.5))
tF_DESS, sF_DESS = mesh_thickness(verts, normals)
segTs = filters.gaussian(segT_MESS, sigma=1)
verts, faces, normals, values = measure.marching_cubes(segTs, 0.5,spacing=(0.65,0.5,0.5))
tT_MESS, sT_MESS = mesh_thickness(verts, normals)
segTs = filters.gaussian(segT_DESS, sigma=1)
verts, faces, normals, values = measure.marching_cubes(segTs, 0.5,spacing=(0.65,0.5,0.5))
tT_DESS, sT_DESS = mesh_thickness(verts, normals)
f_dess_mean += tF_DESS/5
t_dess_mean += tT_DESS/5
f_mess_mean += tF_MESS/5
t_mess_mean += tT_MESS/5
fp.write(f'{P},DESS,{tF_DESS:.2f},{tT_DESS:.2f}\n')
fp.write(f'{P},MESS,{tF_MESS:.2f},{tT_MESS:.2f}\n')
fp.write(f'overall,DESS,{f_dess_mean:.2f},{t_dess_mean:.2f}\n')
fp.write(f'overall,MESS,{f_mess_mean:.2f},{t_mess_mean:.2f}\n')