# Identifying empty segments?

**URL:** <https://discourse.slicer.org/t/identifying-empty-segments/8333>\
**Category:** Development\
**Created:** [September 8, 2019, 12:51am UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333 "2019-09-08T00:51:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [September 8, 2019, 12:51am UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/1 "2019-09-08T00:51:56Z")

</div>

Hi, i’ve been using this code to grab a segment, but i found no way to check if it’s empty or not.

```
segmentation_node = getNode("Segmentation")
seg = segmentation_node.GetSegmentation()
seg0 = seg.GetNthSegment(0)

```

i tried using `polydata = seg0.GetRepresentation("Closed surface")` then `polydata.GetNumberOfCells()` but it only works if there is a closed surface representation of the segment, i’m looking for some approach that is independent of the existence of a closed surface if available, thanks for any inputs.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [September 8, 2019, 1:55pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/2 "2019-09-08T13:55:22Z")

</div>

First of all, you can make your life a bit easier by using the utility functions to get representations:  
[https://apidocs.slicer.org/master/classvtkMRMLSegmentationNode.html#a71ebfa4439ec3ebdf57994888f572623](https://apidocs.slicer.org/master/classvtkMRMLSegmentationNode.html#a71ebfa4439ec3ebdf57994888f572623)

As for your question, you can check if the image data is empty by using the IsEmpty function  
[https://apidocs.slicer.org/master/classvtkOrientedImageData.html#a8ba73e835202dc5e05539a9e4441d70f](https://apidocs.slicer.org/master/classvtkOrientedImageData.html#a8ba73e835202dc5e05539a9e4441d70f)

If it can occur in your use case that you have an image data with a valid extent but you’re wondering if there are only background voxels, you can use the vkImageAccumulate class  
[https://vtk.org/doc/nightly/html/classvtkImageAccumulate.html](https://vtk.org/doc/nightly/html/classvtkImageAccumulate.html)

---

<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:** [September 8, 2019, 5:17pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/3 "2019-09-08T17:17:34Z")

</div>

> [@cpinter](#):
>
> If it can occur in your use case that you have an image data with a valid extent but you’re wondering if there are only background voxels, you can use the vkImageAccumulate class  
> [https://vtk.org/doc/nightly/html/classvtkImageAccumulate.html](https://vtk.org/doc/nightly/html/classvtkImageAccumulate.html)

You can also use `slicer.util.arrayFromSegment(segmentation_node, segment_id).max() > 0` to see if the segment has any non-zero voxels.

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [September 8, 2019, 6:54pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/4 "2019-09-08T18:54:49Z")

</div>

> [@lassoan](#):
>
> You can also use `slicer.util.arrayFromSegment(segmentation_node, segment_id).max() > 0` to see if the segment has any non-zero voxels.

Thanks, that worked fine, only needed to make sure the binary label map extent was not empty(New and unedited segment), as arrayfromsegment returned an attribute error in that case

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [September 8, 2019, 7:00pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/5 "2019-09-08T19:00:24Z")

</div>

> [@cpinter](#):
>
> If it can occur in your use case that you have an image data with a valid extent but you’re wondering if there are only background voxels, you can use the vkImageAccumulate class

Thanks, indeed i need to check for the existence of foreground voxels, i could not get vtkImageAccumulate to work properly(result is always 0) im i doing it wrong?

> segmentation = getNode(“Segmentation”)  
> seg = segmentation.GetSegmentation()  
> seg0 = seg.GetNthSegment(0)  
> segID = seg.GetSegmentIdBySegment(seg0)  
> stat = vtk.vtkImageAccumulate()  
> stat.SetInputData(segmentation.GetBinaryLabelmapRepresentation(segID))  
> print int(stat.GetVoxelCount())

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [September 8, 2019, 7:18pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/6 "2019-09-08T19:18:52Z")

</div>

I don’t quite understand. You marked @lassoan’s suggestion as solution still you try to use my suggestion for the exact same issue? I guess what solved your initial problem was the IsEmpty function, correct?

Anyway, you can use @lassoan’s simpler way to do the voxel counting.  
As for your actual question, you need to call Update to have vtkImageAccumulate actually do the processing. also, if you just call GetVoxelCount like this, it will return the number of all voxels. Use SetIgnoreZero(True) to only get the number of foreground voxels.

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [September 8, 2019, 7:37pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/7 "2019-09-08T19:37:37Z")

</div>

> [@cpinter](#):
>
> You marked @lassoan’s suggestion as solution still you try to use my suggestion for the exact same issue?

my issue is indeed solved, i tried vkImageAccumulate before but could not get it work, it’s interesting to have the voxel count rather than a boolean for future uses.

> [@cpinter](#):
>
> I guess what solved your initial problem was the IsEmpty function, correct?

I actually did use both isEmpty to eliminate new segments (returned an error with arrayfromsegment) and then `slicer.util.arrayFromSegment(segmentation_node, segment_id).max()` to check for foregroud voxels existence, the final code is this:

```
if label.IsEmpty():
    full = False
elif not(label.IsEmpty()):
    if slicer.util.arrayFromSegment(segmentation, segmentID).max() > 0:
        full = True
    else:
        full = False

```

> [@cpinter](#):
>
> you need to call Update to have vtkImageAccumulate actually do the processing. also, if you just call GetVoxelCount like this, it will return the number of all voxels. Use SetIgnoreZero(True) to only get the number of foreground voxels.

Thanks, that made it work.

---

<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:** [September 9, 2019, 5:38pm UTC](https://discourse.slicer.org/t/identifying-empty-segments/8333/8 "2019-09-09T17:38:00Z")

</div>

You can use either VTK or numpy functions - for this very simple check they are about equally simple and efficient. If you use numpy then you can make things simpler and more pythonic like this:

```python
try:
  full = slicer.util.arrayFromSegment(segmentation, segmentID).max() > 0
except AttributeError:
  # there is no labelmap in the segment
  full = False

```
