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.
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
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())
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.
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.
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
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:
try:
full = slicer.util.arrayFromSegment(segmentation, segmentID).max() > 0
except AttributeError:
# there is no labelmap in the segment
full = False