How to enlarge(zoom in/out) the volume of a slice with python code?

如何用python代码放大某个slice的volume呢?

How to enlarge(zoom in/out) the volume of a slice with python code?

This should help:

1 Like

谢谢老师! :+1:

[/quote]

根据老师的代码和我的需求我加了这些, 感觉不够简洁…您有没有更简洁的建议呢?

[quote=“pieper, post:2, topic:20049”]
CompareVolumes/CompareVolumes.py at master · pieper/CompareVolumes · GitHub
[/quote]

According to the teacher’s code and my needs, I added these codes, which is not concise enough… Do you have any more concise suggestions?

def zoomplus(factor,sliceNodes=None):
  """Zoom slice nodes by factor.
  factor: "Fit" or +/- amount to zoom
  sliceNodes: list of slice nodes to change, None means all.
  """
  layoutManager = slicer.app.layoutManager()
  if sliceNodes==None:
    sliceNodes = slicer.util.getNodes('vtkMRMLSliceNode*')
    for sliceNode in list(sliceNodes.values()):
      if factor == "Fit":
        sliceWidget = layoutManager.sliceWidget(sliceNode.GetLayoutName())
        if sliceWidget:
          sliceWidget.sliceLogic().FitSliceToAll()
      else:
        newFOVx = sliceNode.GetFieldOfView()[0] * factor
        newFOVy = sliceNode.GetFieldOfView()[1] * factor
        newFOVz = sliceNode.GetFieldOfView()[2]
        sliceNode.SetFieldOfView( newFOVx, newFOVy, newFOVz )
  else:          
    if sliceNodes == "R":
      sliceNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeRed")
    elif sliceNodes == "Y":
      sliceNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeYellow")
    elif sliceNodes == "G":
      sliceNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeGreen")
    if factor == "Fit":
      sliceWidget = layoutManager.sliceWidget(sliceNode.GetLayoutName())      
      if sliceWidget:
          sliceWidget.sliceLogic().FitSliceToAll()
    else:   
      newFOVx = sliceNode.GetFieldOfView()[0] * factor
      newFOVy = sliceNode.GetFieldOfView()[1] * factor
      newFOVz = sliceNode.GetFieldOfView()[2]
      sliceNode.SetFieldOfView( newFOVx, newFOVy, newFOVz )
  sliceNode.UpdateMatrices() 

The original idea of the sliceNodes argument would that it’s a list of nodes to which the zoom operation should be applied, so it would be better to keep the node ids out of the function. Instead you would invoke it like this an you wouldn’t need to change the function at all.

sliceNodes = [slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeRed")]
zoom('fit', sliceNodes)

I see, thanks.

like that:

def zoomSlice(factor,sliceNodes=None):
  """Zoom slice nodes by factor.
  factor: "Fit" or +/- amount to zoom
  sliceNodes: list of slice nodes to change, None means all.
  """
  layoutManager = slicer.app.layoutManager()
  
  if sliceNodes==None:
    sliceNodes = slicer.util.getNodes('vtkMRMLSliceNode*')
  else:
    if sliceNodes == "R":
      sliceNodes = slicer.util.getNodes('vtkMRMLSliceNodeR*')
    elif sliceNodes == "Y":
      sliceNodes = slicer.util.getNodes('vtkMRMLSliceNodeY*')
    elif sliceNodes == "G":
      sliceNodes = slicer.util.getNodes('vtkMRMLSliceNodeG*')
    for sliceNode in list(sliceNodes.values()):
      sliceWidget = layoutManager.sliceWidget(sliceNode.GetLayoutName())      
      if factor == "Fit":
        sliceWidget.sliceLogic().FitSliceToAll()
      else:
        sliceWidget.sliceLogic().FitSliceToAll()        
        newFOVx = sliceNode.GetFieldOfView()[0] * factor
        newFOVy = sliceNode.GetFieldOfView()[1] * factor
        newFOVz = sliceNode.GetFieldOfView()[2]
        sliceNode.SetFieldOfView( newFOVx, newFOVy, newFOVz )
  sliceNode.UpdateMatrices() 

最终解决方案:

def zoomSlice(factor, sliceNode=None):
  """Zoom slice nodes by factor.
  factor: "Fit" or +/- amount to zoom
  sliceNode:"R";"Y";"G":"None"
  sliceNodes: list of slice nodes to change, None means all.
  """
  layoutManager = slicer.app.layoutManager()    
  if sliceNode==None:
    sliceNodes = slicer.util.getNodes('vtkMRMLSliceNode*')
  else:      
    sliceNodes = {"R": "vtkMRMLSliceNodeRed", "Y": "vtkMRMLSliceNodeYellow", "G": "vtkMRMLSliceNodeGreen"}
    sliceNodes = slicer.util.getNodes(sliceNodes[sliceNode])      
  for sliceNode in list(sliceNodes.values()):
    sliceWidget = layoutManager.sliceWidget(sliceNode.GetLayoutName())
    sliceWidget.sliceLogic().FitSliceToAll()        
    newFOVx = sliceNode.GetFieldOfView()[0] / factor
    newFOVy = sliceNode.GetFieldOfView()[1] / factor
    newFOVz = sliceNode.GetFieldOfView()[2]
    sliceNode.SetFieldOfView( newFOVx, newFOVy, newFOVz )
    sliceNode.UpdateMatrices()