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.
If there is only vtkMRMLModelNode, how can I find centroid of a VTK model?
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()
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 have a question about getting a centroid of segment.
I ran this code below, but it couldn’t work.
segmentationNode = slicer.util.getNode("vtkMRMLSegmentationNode1")
segmentId = "Segment_1"
# Get array voxel coordinates
seg=slicer.util.arrayFromSegment(segmentationNode, segmentId)
# numpy array has voxel coordinates in reverse order (KJI instead of IJK)
# and the array is cropped to minimum size in the segmentation
mean_KjiCropped = [coords.mean() for coords in np.nonzero(seg)]
# Get segmentation voxel coordinates
segImage = segmentationNode.GetBinaryLabelmapRepresentation(segmentId)
segImageExtent = segImage.GetExtent()
# origin of the array in voxel coordinates is determined by the start extent
mean_Ijk = [mean_KjiCropped[2], mean_KjiCropped[1], mean_KjiCropped[0]] + np.array([segImageExtent[0], segImageExtent[2], segImageExtent[4]])
# Get segmentation physical coordinates
ijkToWorld = vtk.vtkMatrix4x4()
segImage.GetImageToWorldMatrix(ijkToWorld)
mean_World = [0, 0, 0, 1]
ijkToRas.MultiplyPoint(np.append(mean_Ijk,1.0), mean_World)
mean_World = mean_World[0:3]
# If segmentation node is transformed, apply that transform to get RAS coordinates
transformWorldToRas = vtk.vtkGeneralTransform()
slicer.vtkMRMLTransformNode.GetTransformBetweenNodes(segmentationNode.GetParentTransformNode(), None, transformWorldToRas)
mean_Ras = transformWorldToRas.TransformPoint(mean_World)
# Show mean position value and jump to it in all slice viewers
print(mean_Ras)
slicer.modules.markups.logic().JumpSlicesToLocation(mean_Ras[0], mean_Ras[1], mean_Ras[2], True)