rbumm
(Rudolf Bumm)
November 25, 2020, 9:13am
1
Hi,
I am developing an semiautomated script for CT lung masking.
After thresholding and cutting out segments of the trachea and left main bronchus (works), I normally use the “Islands” function of the Segment Editor next with the parameter “Keep selected island”.
However, something like this
...
segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation","KEEP_SELECTED_ISLAND")
effect.setParameter("MinimumSize","1000")
effect.self().onApply()
...
does not work from my script.
How can I pass coordinates / selected slice view / mouse click ?
Would be glad if someone had an idea or a code snippet.
Thanks, best regards
rudolf
1 Like
rbumm
(Rudolf Bumm)
November 25, 2020, 12:12pm
2
rbumm:
...
def rasToDisplay(self, r, a, s):
displayCoords = [0, 0, 0, 1]
# get the slice node
lm = slicer.app.layoutManager()
sliceWidget = lm.sliceWidget('Red')
sliceLogic = sliceWidget.sliceLogic()
sliceNode = sliceLogic.GetSliceNode()
xyToRASMatrix = sliceNode.GetXYToRAS()
rasToXyMatrix = vtk.vtkMatrix4x4()
rasToXyMatrix.Invert(xyToRASMatrix, rasToXyMatrix)
worldCoords = [r, a, s, 1.0]
rasToXyMatrix.MultiplyPoint(worldCoords, displayCoords)
return (int(displayCoords[0]), int(displayCoords[1]))
...
def how_to_call_keep_selected_islands():
...
lm = slicer.app.layoutManager()
sliceWidget = lm.sliceWidget('Red')
segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation","KEEP_SELECTED_ISLAND")
slicer.util.clickAndDrag(sliceWidget, start=self.rasToDisplay(25.3, 5.8, sliceOffset), end=self.rasToDisplay(25.3, 5.8, sliceOffset), steps=1)
...
Found the solution myself.
Enjoy.
3 Likes
lassoan
(Andras Lasso)
November 25, 2020, 2:47pm
3
Thanks for sharing. You could make your code simpler and more robust by slightly changing the Islands effect so that it can take a 3D coordinate directly (not just an interaction event that you need to simulate via the GUI). The effect is a simple Python script, which you can freely modify locally (you could inject the 3D coordinate here ). If the modification works well for you then you can send us a pull request so that we can integrate that change into Slicer core.
jumbojing
(Jumbo Jing)
October 2, 2021, 1:16pm
4
rbumm:
...
def rasToDisplay(self, r, a, s):
displayCoords = [0, 0, 0, 1]
# get the slice node
lm = slicer.app.layoutManager()
sliceWidget = lm.sliceWidget('Red')
sliceLogic = sliceWidget.sliceLogic()
sliceNode = sliceLogic.GetSliceNode()
xyToRASMatrix = sliceNode.GetXYToRAS()
rasToXyMatrix = vtk.vtkMatrix4x4()
rasToXyMatrix.Invert(xyToRASMatrix, rasToXyMatrix)
worldCoords = [r, a, s, 1.0]
rasToXyMatrix.MultiplyPoint(worldCoords, displayCoords)
return (int(displayCoords[0]), int(displayCoords[1]))
...
def how_to_call_keep_selected_islands():
...
lm = slicer.app.layoutManager()
sliceWidget = lm.sliceWidget('Red')
segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation","KEEP_SELECTED_ISLAND")
slicer.util.clickAndDrag(sliceWidget, start=self.rasToDisplay(25.3, 5.8, sliceOffset), end=self.rasToDisplay(25.3, 5.8, sliceOffset), steps=1)
...
@rbumm 我想知道这个代码只是在一个slice上选点,可是这个slice如何确定呢…我想说的是三维空间的第三个点怎么确定呢?
I want to know that this code just selects a point on a slice, but how to determine this slice…What I want to konw is how to determine the third point in the three-dimensional space?
jumbojing
(Jumbo Jing)
October 2, 2021, 1:28pm
5
@lassoan 没有更简洁的方法,直接输入the 3D coordinate来获取island?
There is no more concise way that inject the 3D coordinate to get the island…e.g. effiect.setparameter()