Reference location information for segment with respect to master volume

Operating system: Windows 10
Slicer version: 4.8.1 (r26813) built myself

I am currently working on some extension submodule to Segment Editor doing automatic segmentation of some organs. This involves handling multiple segments mostly working with numpy arrays. This is currently done as follows inside a SegmentEditor effect method:

# get master volume array
volumeNode = slicer.util.getNode('CT image')
npImage = slicer.util.arrayFromVolume(volumeNode)

# get mask array
segNode = slicer.util.getNode('Segmentation') # Get the segmentation node    
npMask = slicer.util.arrayFromSegment( segNode, 'Airways')

My question: The npMask array is smaller than the npImage array. Where can I get information about how they relate spatially?
Or more precisely: where in npImage is the lower left corner of npMask located?

Origin, spacing, axis directions, and extents of binary labelmap representation for each segment is stored in the vtkOrientedImage data object in the corresponding vtkSegment object. You can use GetBinaryLabelmapRepresentation convenience method in segmentation node to get this image directly, as it is done in the slicer.util.arrayFromSegment method.

Segment editor takes care of getting the master volume and resample to the segment’s geometry (so you don’t need to worry about matching voxels of the master volume and your segment; origin, spacing, and axis directions will be the same; only their extent may be different), updating voxels in of all segments by respecting masking settings, etc. Therefore, instead of trying to implement an effect from scratch, I would recommend to find the effect that is most similar to what you would like to do and modify/extend that. Most segment editor effects are implemented in Python, so there are plenty of examples. Source code:

1 Like