<jupyter_start><jupyter_text>Bronchus Segmentation Example Import Modules<jupyter_code>try:
    import platipy
except:
    !pip install git+https://github.com/pyplati/platipy.git
    import platipy

import SimpleITK as sitk

%matplotlib inline

from platipy.imaging.tests.data import get_lung_nifti
from platipy.imaging.projects.bronchus.run import run_bronchus_segmentation
from platipy.imaging import ImageVisualiser
from platipy.imaging.label.utils import get_com<jupyter_output><empty_output><jupyter_text>Download Test DataThis will download lung cancer patient CT scans, with contours of several structures.This only has to be done once - if it is ran again don't worry, the same data will not be downloaded again!<jupyter_code>data_path = get_lung_nifti()<jupyter_output><empty_output><jupyter_text>Load Test ImageRead in the image we want to automatically segment<jupyter_code>test_pat_path = data_path.joinpath("LCTSC-Test-S1-201")
test_image = sitk.ReadImage(str(test_pat_path.joinpath("IMAGES/LCTSC_TEST_S1_201_0_CT_0.nii.gz")))<jupyter_output><empty_output><jupyter_text>Run Auto-segmentationThis will take some time, and will print updates along the way<jupyter_code>auto_structures = run_bronchus_segmentation(test_image)<jupyter_output><empty_output><jupyter_text>Save segmentationsOptionally write the automatic segmentations to disk<jupyter_code>output_directory = test_pat_path.joinpath("SEGMENTATIONS")
output_directory.mkdir(exist_ok=True)

for struct_name in list(auto_structures.keys()):
    sitk.WriteImage(auto_structures[struct_name], str(output_directory.joinpath(f"{struct_name}.nii.gz")))

print(f"Segmentations saved to: {output_directory}")<jupyter_output><empty_output><jupyter_text>Visualise SegmentationsNext, we can generate a nice figure to check what the segmentation looks likeThe first step here is to convert SimpleITK images to NumPy arrays<jupyter_code>vis = ImageVisualiser(test_image, cut=get_com(auto_structures["Auto_Bronchus"]))

vis.add_contour({struct: auto_structures[struct] for struct in auto_structures.keys()})

fig = vis.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup