Area of segment in a given slice?

Is there a way to determine the area a segment takes up in a given slice? I’m interested in generating a plot from a CT scan, anterior to posterior, of the area taken up by a segment slice-by-slice (if that makes sense).

If you were to do this on a sphere, for example, you’d wind up with a gaussian. (I think, I’d have to double-check the math on that…)

Thanks!

-Hollister

There’s nothing built-in to do that, but it would be possible with a little python scripting. Are you interested in trying that? We could give some pointers.

This should do what you need (this script puts results in a table but you can easily create a plot from the vtkDoubleArray):

segmentationNode = getNode('Segmentation')
segmentId = 'Segment_1'

# Get segment as a numpy array
import vtk.util.numpy_support
import numpy as np
vimage = segmentationNode.GetBinaryLabelmapRepresentation(segmentId)
narray = vtk.util.numpy_support.vtk_to_numpy(vimage.GetPointData().GetScalars())
vshape = vimage.GetDimensions()

# Reshape the segment volume to have an array of slices
narrayBySlice = narray.reshape([-1,vshape[1]*vshape[2]])

# Count number of >0 voxels for each slice
narrayBySlicePositive = narrayBySlice[:]>0
areaBySliceInVoxels = np.count_nonzero(narrayBySlicePositive, axis=1)

# Convert number of voxels to area in mm2
areaOfPixelMm2 = vimage.GetSpacing()[0] * vimage.GetSpacing()[1]
areaBySliceInMm2 = areaBySliceInVoxels * areaOfPixelMm2

# Save results to a new table node
tableNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", "Segment quantification")
updateTableFromArray(tableNode, areaBySliceInMm2)
tableNode.GetTable().GetColumn(0).SetName("Segment Area")

You guys are awesome! Thanks!!

-Hollister

This code snipped will create a plot:

plotDataNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotDataNode")
plotDataNode.SetAndObserveTableNodeID(tableNode.GetID())
plotDataNode.SetXColumnName("Indexes")
plotDataNode.SetYColumnName(tableNode.GetTable().GetColumn(0).GetName())

plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode")
plotChartNode.AddAndObservePlotDataNodeID(plotDataNode.GetID())

layoutManager = slicer.app.layoutManager()
layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpPlotView)
plotWidget = layoutManager.plotWidget(0)

plotViewNode = plotWidget.mrmlPlotViewNode()
plotViewNode.SetPlotChartNodeID(plotChartNode.GetID())

It’ll work with tomorrow’s nightly build (I had to make some fixes in the Slicer core).

1 Like