How to calculate the surface area from 3D fetal MRI, then convert to Python

Hi all,

I’m relatively new to both Python and 3D Slicer, but I am trying to calculate the surface area of the human placenta through 3D Slicer and then convert this into Python code for an automatic calculation of the placental surface area. I have a data set of NIfTI files of already segmented placentas, but I’m unsure how to get the surface area through 3D Slicer. Any help would be appreciated. Thank you.

Here’s my update:

  • I’ve rendered the 3D volume in the top right corner through Volume Rendering
  • I’ve added my segment under Segment Editor
  • I’ve applied only the ‘Closed Surface Statistics’ option under Segment Statistics after choosing my segment as my input, but I am not given the surface area for some reason - this is where I’m currently stuck

You need to have closed surface representation to compute “Closed surface statistics”. You can compute it by clicking the “Show 3D” button in Segment Editor module or by choosing “Create closed surface representation” in the right-click menu in Data module.

I would not recommend using Volume Rendering module for 3D rendering of a segmentation because it does not do an accurate reconstruction of the original smooth, continuous surface, but it just shows each voxel as a cube. Instead, load the file as a segmentation (choose Segmentation in “Add data” window’s “Description” column) and then create the closed surface representation to see it in 3D.

Thank you, it worked!

1 Like

Would you know how to create a Python code resembling how to calculate surface area similar to 3D Slicer?

You can run any Python code in Slicer. You can use all Slicer application features and any other Python packages. What is your overall goal?

My goal is to write a Python code that can automatically calculate the surface area of a large dataset of placentas from 3D MRI images. With 3D Slicer, I was aiming to integrate it into my python code and get the surface area information directly that way

That sounds very easily doable. Probably the simplest is to run all your Python code in Slicer’s Python environment. You can automate everything that you did in Slicer GUI using Python scripting and you can add any additional processing in the same script. This PerkLab Bootcamp Slicer Programming tutorial is a good starting point, you can find many examples in the script repository and if you don’t find any answer then you can ask on this forum.

Thank you for the resources. I looked through the steps in the PerkLab tutorial on slides 36-49 and tried slides 36-43 myself before I got stuck. I’m wondering, is the .py file in 3D Slicer what I need to focus on to help me get Python code to calculate surface area? Is it something that automatically updates after I run 3D Slicer and get the surface area that way, or do I need to manually update the .py file? I want first to understand how I need to go about this, as I’m still somewhat new to Python

For this task you don’t need to develop a new Slicer module, just write a short code snippet that uses existing modules. However, the exercises, examples, links to resources, etc. are all applicable to a simple Python script the same way as to a Slicer module.

Since you will not develop a new Slicer module, you’ll just write a Python script and start it using Slicer.exe --python-script /path/to/ If you modify the script, save it, and launch it with this command then the modified script will run.

I’m sorry, I’m still a little lost with starting this process, would I need to write more code to calculate surface area if Slicer already has code written? I’m just unsure what code I have to write.

If so, would this be where I write the Python script?

Would you please let me know which Python parameter I need to use to get the surface area? Thank you

You can work out what code you need by using the interactive Python console in Slicer:

  • load the NIFTI file as segmentation
  • create closed surface representation
  • use Segment Statistics module to compute surface area

For each of these steps, you can find the necessary code snippets in the script repository (1-2 lines of Python code each):

From the first link, “slicer.util.loadSegmentation” is not working on PyCharm for me, I have imported ‘slicer,’ but I can’t find anything on ‘util’

I tried ‘pip install util’ in the command window, but I was getting another error with util:

ERROR: Could not find a version that satisfies the requirement util (from versions: none)
ERROR: No matching distribution found for util

Would you happen to know if there’s an alternative to this line of code from the script repository or how to find the download for util? I have looked on, but I don’t know which one is the correct one.

You need to run your python code inside the Slicer Python console. Sounds like you are trying to use an external python.

Is that why the code won’t work in PyCharm?

Here’s what I get when I run it in the Slicer Python Console:

That error looks like just an incorrect path. Try forward slashes.

Thank you, I added the forward slashes, but now get this error:

Try putting your data to a simpler path (e.g., C:/data) and try. that’s is a path error. Read cannot find the file, either path provided is not correct or spaces are causing an issue.

Would this be considered a simpler file path: slicer.util.loadSegmentation(“C:/Users/jaleesaleblanc/Desktop/FMS0078.labels.nii”)

I don’t know how to make it simpler, but somehow I’m getting the same error