|
a |
|
b/heart_segmentation.py |
|
|
1 |
#!/usr/bin/python3 |
|
|
2 |
|
|
|
3 |
import os |
|
|
4 |
import platipy # https://pyplati.github.io/platipy/index.html |
|
|
5 |
|
|
|
6 |
from matplotlib import pyplot as plt |
|
|
7 |
|
|
|
8 |
import SimpleITK as sitk |
|
|
9 |
|
|
|
10 |
from platipy.imaging.projects.cardiac.run import run_hybrid_segmentation |
|
|
11 |
from platipy.imaging import ImageVisualiser |
|
|
12 |
from platipy.imaging.label.utils import get_com |
|
|
13 |
|
|
|
14 |
### Change this ### |
|
|
15 |
filename = f'volume_{1}' |
|
|
16 |
### Change this ### |
|
|
17 |
|
|
|
18 |
os.makedirs(f"./output/{filename}", exist_ok=True) |
|
|
19 |
test_image = sitk.ReadImage(f"./input/volumes/{filename}.nii.gz") |
|
|
20 |
auto_structures, _ = run_hybrid_segmentation(test_image) |
|
|
21 |
|
|
|
22 |
output_directory = './output' |
|
|
23 |
|
|
|
24 |
for struct_name in list(auto_structures.keys()): |
|
|
25 |
sitk.WriteImage(auto_structures[struct_name], str(f"{output_directory}/{filename}/{filename}_{struct_name}.nii.gz")) |
|
|
26 |
|
|
|
27 |
print(f"Segmentations saved to: {output_directory}") |
|
|
28 |
|
|
|
29 |
vis = ImageVisualiser(test_image, cut=get_com(auto_structures["Heart"])) |
|
|
30 |
vis.add_contour({struct: auto_structures[struct] for struct in auto_structures.keys()}) |
|
|
31 |
fig = vis.show() |
|
|
32 |
|
|
|
33 |
plt.savefig(f"{output_directory}/{filename}/{filename}_heart_visualization.png") |
|
|
34 |
plt.close(fig) |
|
|
35 |
print(f"Visualization saved to: {output_directory}/{filename}/{filename}_heart_visualization.png") |