What's wrong with this code of 'INTERSECT' to 2 segmentations?


I want to use python to do the intersection of 2 Segmentations…I have a problem…Help me please

seg = slicer.util.getNode("segment")

# Cyl = Logic.p2pCyl(P0,P1,radius=1)
modNode = getNode("Cylinder")
cylSegnode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
  modNode, cylSegnode

# Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
# To show segment editor widget (useful for debugging):
# segmentEditorWidget.show()
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
# segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)


segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation", "INTERSECT")
35 >>> effect.setParameter("ModifierSegmentID", seg.GetID())
Line 35:    

File /Applications/Slice98.app/Contents/lib/Slicer-4.13/qt-scripted-modules/SegmentEditorEffects/SegmentEditorLogicalEffect.py, in onApply:
Line 244:   vtkSegmentationCore.vtkOrientedImageDataResample.OPERATION_MINIMUM, selectedSegmentLabelmap.GetExtent())

AttributeError: 'NoneType' object has no attribute 'GetExtent'

@lassoan@Juicy@jamesobutler @jcfr @pieper

There might be multiple problems here, but one I see is that in the above code, the two segments you are trying to intersect are not in the same segmentation. You get seg using getNode(), which must not be how you really do it because you cannot get segmentation segments using getNode because they are not MRML nodes. After that, you create a new segmentation node for the cylinder model and make a segment from the model node. Thus the new segmentation node only has the cylinder segment, not any other segments. Later, you try to use the seg segment, which is not in the cylSegNode segmentation, as a modifier, which will not work, because there is not segment with that ID in the cylSegNode.

I also suspect that you need to set the geometry (grid spacing and orientation) of the cylSeg segmentation somehow before it can be used in a logical intersection (setting a master volume is a typical way to do this). When you create the a segment from a model, the closed surface representation of that model is definitely created in the segmentation, but you might need to explicitly create the binary labelmap representation (you can see what representations are present by looking at the segmentation in the Segmentations module). Looking at your error message, the ‘selectedSegmentLabelmap’ for your cylinder segment might be ‘None’ because it has not been created yet.

1 Like