How to Script the Import a Segmented NIfTI Volume as a Solid in Sim4Life?
-
Hello everyone,
I am currently working with a segmented volume in .nifti format and would like to import it as a solid in Sim4Life using scripting. I have heard that the iSeg segmentation tool within Sim4Life might be useful for this process. Could anyone provide guidance, scripts, or tips on how to achieve this?
Thank you in advance!
-
Hi
To import the nifti file as a labelfield, with label names and colors, it is helpful to have a descriptor file. Sim4Life supports the iSEG format (.txt) and Slicer 3D color table (.ctbl).
The iSEG "tissue list" format is very simple. For example, the following would describe a segmentation with three labels (N3). The V7 refers to a version of the format. Each line starting with "C" lists the labels in order 1, 2, 3, with RGBA colors in floating point values followed by the name, e.g., Bone has label=1, color=(0, 0, 255) and alpha=127.
V7 N3 C0.00 0.00 1.00 0.50 Bone C0.00 1.00 0.00 0.50 Fat C1.00 0.00 0.00 0.50 Skin
To import the labelfield and extract the solids, you can using following script.
# script to import the labelfield and extract surfaces from s4l_v1.model.image import ExtractSurface, ImportLabelField def import_as_solid(nii_path: str, tissue_list_path: str, min_edge_length: float = 0.5): labelfield = ImportLabelField(nii_path, tissue_list_path) solids = ExtractSurface(label_field=labelfield, min_edge_length=min_edge_length) return solids
Note, if you are looking for the "Import Voxels [as Solid]" dialog in the GUI, you cannot directly use nifti files. However, the proposed workflow produces smoother solids (closed triangle meshes) - I would argue for most purposes a better result.
-
Here is a simple implementation to write an iSEG tissue list file, given a dictionary mapping the label index to a name:
https://github.com/dyollb/s4l-scripts/blob/df8e2241f87ca91d71138e9d2b3d4336dadb82dc/src/anisotropic_conductivity/load_labels.py#L44