-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaculate_ejection_fraction.py
114 lines (83 loc) · 2.87 KB
/
caculate_ejection_fraction.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
# @author Yisong Zhen
# @since 2018-10-08
# @source
# magnetic resonance (MR) imaging data
# Sunnybrook Cardiac Data
# http://www.cardiacatlas.org/studies/sunnybrook-cardiac-data/
#---
import imageio
import numpy as np
import scipy.ndimage as ndi
import matplotlib.pyplot as plt
# Smooth intensity values
im_filt = ndi.median_filter(im, size = 3)
# Select high-intensity pixels
mask_start = np.where(im_filt > 60, 1, 0)
mask = ndi.binary_closing(mask_start)
# Label the objects in "mask"
labels, nlabels = ndi.label(mask)
print('Num. Labels:',nlabels)
# Label the image "mask"
labels, nlabels = ndi.label(mask)
# Select left ventricle pixels
lv_val = labels[128, 128]
lv_mask = np.where(labels == lv_val,1,np.nan)
# Overlay selected label
plt.imshow(lv_mask, cmap='rainbow')
plt.show()
# Create left ventricle mask
labels, nlabels = ndi.label(mask)
lv_val = labels[128,128]
lv_mask = np.where(lv_val == labels, 1, 0)
# Variance for all pixels
var_all = ndi.variance(vol, labels=None, index=None)
print('All pixels:', var_all)
# Variance for labeled pixels
var_labels = ndi.variance(vol, labels = labels , index = None)
print('Labeled pixels:', var_labels)
# Variance for each object
var_objects = ndi.variance(vol, labels = labels, index = [1,2])
print('Left ventricle:', var_objects[0])
print('Other tissue:', var_objects[1])
# Create histograms for selected pixels
hist1 = ndi.histogram(vol, min=0, max=255, bins=256)
hist2 = ndi.histogram(vol, 0, 255, 256, labels=labels)
hist3 = ndi.histogram(vol, 0, 255, 256, labels=labels, index= 1)
# Calculate left ventricle distances
lv = np.where(labels == 1, 1, 0)
dists = ndi.distance_transform_edt(lv,sampling=vol.meta['sampling'])
# Report on distances
print('Max distance (mm):', ndi.maximum(dists))
print('Max location:', ndi.maximum_position(dists))
# Plot overlay of distances
overlay = np.where(dists[5] > 0, dists[5], np.nan)
plt.imshow(overlay, cmap='hot')
format_and_render_plot()
# Extract centers of mass for objects 1 and 2
coms = ndi.center_of_mass(vol,labels, index = [1,2])
print('Label 1 center:', coms[0])
print('Label 2 center:', coms[1])
# Add marks to plot
for c0, c1, c2 in coms:
plt.scatter(c2, c1, s=100, marker='o')
plt.show()
# Create an empty time series
ts = np.zeros(20)
# Calculate volume at each voxel
d0, d1, d2, d3 = vol_ts.meta['sampling']
dvoxel = d1 * d2 * d3
# Loop over the labeled arrays
for t in range(20):
nvoxels = ndi.sum(1,labels[t],index = 1)
ts[t] = nvoxels * dvoxel
# Plot the data
plt.plot(ts)
format_and_render_plot()
# Get index of max and min volumes
tmax = np.argmax(ts)
tmin = np.argmin(ts)
# Plot the largest and smallest volumes
fig, axes = plt.subplots(2,1)
axes[0].imshow(vol_ts[tmax,4], vmax=160)
axes[1].imshow(vol_ts[tmin,4], vmax=160)
format_and_render_plots()