# 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:** 1\
**Showing post:** 3

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

---

_[View the full topic](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559)._
