# Using script for creating 'intersection' from 'Logical operators' of Segment Editor

**URL:** <https://discourse.slicer.org/t/using-script-for-creating-intersection-from-logical-operators-of-segment-editor/36104>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [May 13, 2024, 3:07am UTC](https://discourse.slicer.org/t/using-script-for-creating-intersection-from-logical-operators-of-segment-editor/36104 "2024-05-13T03:07:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [May 13, 2024, 3:07am UTC](https://discourse.slicer.org/t/using-script-for-creating-intersection-from-logical-operators-of-segment-editor/36104/1 "2024-05-13T03:07:14Z")

</div>

Hi,

I am using python scrip to access Intersection operation from Logical Operators. I might did the script wrong. Using Logical Operators manually, the intersection between the orbit and the plate is:

 ![Screenshot 2024-05-12 at 9.25.53 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/6/a6a0325e1a55d62a64fa852db991ecdfebe5489e.jpeg) ![Screenshot 2024-05-12 at 9.44.59 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/1/11ff1a6025f3a9c240dbdb8ac8341f25a1c3e0b0.png)

I used the script:

```auto
#Logic operator
segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation","INTERSECT")

boneSegID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('bone_no_fx')
plateSegID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('plate_copy')

segmentEditorNode.SetSelectedSegmentID(plateSegID)
effect.setParameter("ModifierSegmentID", boneSegID)
effect.self().onApply()

```

It showed this effect:

 ![Screenshot 2024-05-12 at 9.29.19 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/7/e785849804ee620f205e2804acbdcac9c1bbc4b0.jpeg)

I used `segmentEditorNode.SetSelectedSegmentID(plateSegID)` to set up the plate segment to be modified by the bone. I think I did this wrong. Can someone tell me how to properly set up the segment to be modified? This [Segment editor — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/modules/segmenteditor.html#effect-parameters) only shows setting up the modifier ID.

Here is a [sample vol and segments](https://drive.google.com/file/d/14ZLOH-gHlnHqL3cIbqDG2FOq3MH5T4yY/view?usp=sharing) and here is my [snippet](https://github.com/chz31/surgical_plate_registration/blob/main/intersect_plate_orbit.py).

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [May 13, 2024, 8:21pm UTC](https://discourse.slicer.org/t/using-script-for-creating-intersection-from-logical-operators-of-segment-editor/36104/2 "2024-05-13T20:21:22Z")

</div>

Your script works fine after commenting

`# segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)`

In the default Segment editor node, or if you show yours (_segmentEditorWidget.show()_), we can see that the geometry of ‘5 DummySeriesDesc!’ does not fit in the segmentation or output geometry.

---

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [May 14, 2024, 4:06am UTC](https://discourse.slicer.org/t/using-script-for-creating-intersection-from-logical-operators-of-segment-editor/36104/3 "2024-05-14T04:06:15Z")

</div>

@chir.set Thank you for the response and solution! I cropped the volume to save the space but did not notify that I over cropped it.

As you suggested, my script did work after disable `segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)`.

However, when I tried to load the original volume of the `masterVolumeNode` set up, it stilled showed the same effect of not generating a proper intersection (here is the [volume](https://drive.google.com/file/d/1QZodtttwqjQiRe8PVpSPXNRf8r3iggZW/view?usp=sharing) that fits the segmentation)

It also output this error message. It still shows there is a geometry mismatch between `inputImage and imageToAppend`

```auto
[VTK] Generic Warning: In vtkOrientedImageDataResample.cxx, line 1126
[VTK] vtkOrientedImageDataResample::MergeImage failed: geometry mismatch between inputImage and imageToAppend
[VTK] vtkSegmentationModifier::SetBinaryLabelmapToSegment: Invalid input labelmap
[Qt] virtual void qSlicerSegmentEditorAbstractEffect::modifySegmentByLabelmap(vtkMRMLSegmentationNode *, const char *, vtkOrientedImageData *, qSlicerSegmentEditorAbstractEffect::ModificationMode, const int *, bool) : Failed to add modifier labelmap to selected segment

```

Perhaps I simply should not set up a master volume for intersection? I wonder if it is due to the two segments were converted back from surface models. I also acquired these samples segmented by other people. The Segment Editor did work well though even with the original unfitted master volume.
