# Area of segment in a given slice?

**URL:** <https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559>\
**Category:** Support\
**Created:** [November 29, 2017, 9:00pm UTC](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559 "2017-11-29T21:00:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [November 29, 2017, 9:00pm UTC](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559/1 "2017-11-29T21:00:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [November 30, 2017, 2:08pm UTC](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559/2 "2017-11-30T14:08:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 30, 2017, 2:52pm UTC](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559/3 "2017-11-30T14:52:34Z")

</div>

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")
```

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [November 30, 2017, 5:57pm UTC](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559/4 "2017-11-30T17:57:13Z")

</div>

You guys are awesome! Thanks!!

-Hollister

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 30, 2017, 7:32pm UTC](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559/5 "2017-11-30T19:32:29Z")

</div>

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).
