Centroid determination

I am trying to determine the centroid of several previously segmented materials. I can’t find this option in the Quantification module “Segment Statistics” of this software.
Is there any solution to this problem or the software 3D Slicer does’nt have any module that determines the centroid of an already segmented material?

I am currently using version 4.8.1 of the 3D Slicer software.

1 Like

I have the same issue as yours.

It is shown in this example how to get the centroid using numpy and then transform the raw array coordinates to scene RAS coordinates: https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_centroid_of_a_segment_in_world_.28RAS.29_coordinates

I have some follow-up questions:

  1. If there is only vtkMRMLModelNode, how can I find centroid of a VTK model?
  2. If 1) is not possible, how can I convert model to segmentation in a loadable script? I cannot find any associated function from slicer.modules.models.logic()

Thank you very much!

You can get center point very quickly as center of bounding box, with a little more computation from centroid of model points, or the exa t centroid (center of gravity assuming the model is filled with material of uniform density) using Segment statistics module.

I see, thank you.
I ended up implementing it without referring to bounding box, here’s my sample code as future reference for anyone who needs it:

md = slicer.util.getNode(‘ModelX’)
pd = md.GetPolyData().GetPoints().GetData()
from vtk.util.numpy_support import vtk_to_numpy
import numpy as np
print(np.average(vtk_to_numpy(pd), axis=0))

The output should print the average coordinate of all points within a vtkMRMLModelNode.


Thank you for sharing the solution! You can use slicer.util helper functions to simplify things a bit:

modelNode = slicer.util.getNode(‘ModelX’)
pointCoordinates = slicer.util.arrayFromModelPoints(modelNode)
import numpy as np
print(np.average(vtk_to_numpy(pd), axis=0))

or even shorter (a bit less explicit, so I would only recommend for testing and troubleshooting, not in modules):

import numpy as np
print(np.average(array('ModelX'), axis=0))
1 Like

Oh, great! I like the two line solution, and it reads quite explicit to me :slight_smile: