Automate printing segmentation volumes?

Sorry if this is painfully obvious or already covered elsewhere - I’m fairly new to both python programming and 3D Slicer. I’ve been reading through the docs all morning but so far they’ve been more confusing than helpful.

I have a directory of ~700 images (.nii) and corresponding segmentations. I want to write a simple script that will load each segmentation and print the volumes of the two segments (each segmentation contains a “tissue” and a “blood” volume).

So far I have slicer.util.loadSegmentation(“image_number.seg.nrrd”), but I’m not sure from a python perspective how to get the output shown by the segment statistics module on the GUI interface. Can anyone point me in the right direction?

You are doing good so far. After you loaded the segmentation, run the segment statistics to get the volumes (see complete example in the script repository), and write the results into any format you prefer (for example, you can write the values to a CSV file).

@lassoan thank you so much! Turns out I wasn’t looking in the right spot. This is exactly what I needed :slight_smile:

However - I’ve successfully done pip install slicerio and pip install pynrrd and reloaded jupyter labs, but whenever I try to import them into a script I still get the error: ModuleNotFoundError: No module named ‘nrrd’ or ModuleNotFoundError: No module named ‘slicerio’.

Did these get changed to something else with the newest release?

Probably you installed them in a different Python environment.

However, both slicerio and pynrrd are only needed when your code runs outside of Slicer’s Python environment, so you should not need them.

I see, thank you again so much!!