Skip to main content
 首页 » 编程设计

python之"GetImage"和 "Update"的 VTK 更改

2024年10月01日6cloudgamer

我正在尝试使用 Python 在 3DSlicer 中进行扩展编程。 有 a tutorial在线的。不幸的是,第三个示例脚本“HelloSharpen”存在问题。我做了他们所做的完全相同的事情,但我得到了这个错误:

Traceback (most recent call last): 
  File "C:/Users/johan/Desktop/HelloPythonSlicer4/helloPython/code/HelloSharpen.py", line 105, in onApply 
    laplacian.SetInput(inputVolume.GetImageData()) 
AttributeError: 'vtkImagingGeneralPython.vtkImageLaplacian' object has no attribute 'SetInput' 

我通过将 laplacian.SetInput(inputVolume.GetImageData()) 更改为 laplacian.SetInputData(inputVolume.GetImageData()) 解决了这个问题,因为我读到他们改变了这个在较新版本的 VTK 中。

然而,当我尝试运行这个时,出现了一个新的错误:

Traceback (most recent call last): 
  File "C:/Users/johan/Desktop/HelloPythonSlicer4/helloPython/code/HelloSharpen.py", line 107, in onApply 
    laplacian.GetOutput().Update() 
AttributeError: 'vtkCommonDataModelPython.vtkImageData' object has no attribute 'Update' 

似乎 laplacian.GetOutput().Update() 导致了问题,所以 如果他们在较新版本的 VTK 中也更改了此内容,我试图在 Internet 上查找一些内容,但我找不到任何内容。我试图将其更改为“UpdateData”,但这不起作用。 你知道他们是否也改变了这个,如果是,你知道我应该用什么来代替它吗?

这是“HelloSharpen”的完整代码:

from __main__ import vtk, qt, ctk, slicer 
 
# 
# HelloSharpen 
# 
 
class HelloSharpen: 
  def __init__(self, parent): 
    parent.title = "Hello Python Part D - Sharpen" 
    parent.categories = ["Examples"] 
    parent.dependencies = [] 
    parent.contributors = ["Jean-Christophe Fillion-Robin (Kitware)", 
                           "Steve Pieper (Isomics)", 
                           "Sonia Pujol (BWH)"] # replace with "Firstname Lastname (Org)" 
    parent.helpText = """ 
    Example of scripted loadable extension for the HelloSharpen tutorial. 
    """ 
    parent.acknowledgementText = """ 
    This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc., 
Steve Pieper, Isomics, Inc., and Sonia Pujol, Brigham and Women's Hospital and was  
partially funded by NIH grant 3P41RR013218-12S1 (NAC) and is part of the National Alliance  
for Medical Image Computing (NA-MIC), funded by the National Institutes of Health through the  
NIH Roadmap for Medical Research, Grant U54 EB005149.""" # replace with organization, grant and thanks. 
    self.parent = parent 
 
# 
# qHelloPythonWidget 
# 
 
class HelloSharpenWidget: 
  def __init__(self, parent = None): 
    if not parent: 
      self.parent = slicer.qMRMLWidget() 
      self.parent.setLayout(qt.QVBoxLayout()) 
      self.parent.setMRMLScene(slicer.mrmlScene) 
    else: 
      self.parent = parent 
    self.layout = self.parent.layout() 
    if not parent: 
      self.setup() 
      self.parent.show() 
 
  def setup(self): 
    # Collapsible button 
    self.laplaceCollapsibleButton = ctk.ctkCollapsibleButton() 
    self.laplaceCollapsibleButton.text = "Sharpen Operator" 
    self.layout.addWidget(self.laplaceCollapsibleButton) 
 
    # Layout within the laplace collapsible button 
    self.laplaceFormLayout = qt.QFormLayout(self.laplaceCollapsibleButton) 
 
    # 
    # the volume selectors 
    # 
    self.inputFrame = qt.QFrame(self.laplaceCollapsibleButton) 
    self.inputFrame.setLayout(qt.QHBoxLayout()) 
    self.laplaceFormLayout.addWidget(self.inputFrame) 
    self.inputSelector = qt.QLabel("Input Volume: ", self.inputFrame) 
    self.inputFrame.layout().addWidget(self.inputSelector) 
    self.inputSelector = slicer.qMRMLNodeComboBox(self.inputFrame) 
    self.inputSelector.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" ) 
    self.inputSelector.addEnabled = False 
    self.inputSelector.removeEnabled = False 
    self.inputSelector.setMRMLScene( slicer.mrmlScene ) 
    self.inputFrame.layout().addWidget(self.inputSelector) 
 
    self.outputFrame = qt.QFrame(self.laplaceCollapsibleButton) 
    self.outputFrame.setLayout(qt.QHBoxLayout()) 
    self.laplaceFormLayout.addWidget(self.outputFrame) 
    self.outputSelector = qt.QLabel("Output Volume: ", self.outputFrame) 
    self.outputFrame.layout().addWidget(self.outputSelector) 
    self.outputSelector = slicer.qMRMLNodeComboBox(self.outputFrame) 
    self.outputSelector.nodeTypes = ( ("vtkMRMLScalarVolumeNode"), "" ) 
    self.outputSelector.setMRMLScene( slicer.mrmlScene ) 
    self.outputFrame.layout().addWidget(self.outputSelector) 
 
    self.sharpen = qt.QCheckBox("Sharpen", self.laplaceCollapsibleButton) 
    self.sharpen.toolTip = "When checked, subtract laplacian from input volume" 
    self.sharpen.checked = True 
    self.laplaceFormLayout.addWidget(self.sharpen)  
 
 
    # Apply button 
    laplaceButton = qt.QPushButton("Apply") 
    laplaceButton.toolTip = "Run the Laplace or Sharpen Operator." 
    self.laplaceFormLayout.addWidget(laplaceButton) 
    laplaceButton.connect('clicked(bool)', self.onApply) 
 
    # Add vertical spacer 
    self.layout.addStretch(1) 
 
    # Set local var as instance attribute 
    self.laplaceButton = laplaceButton 
 
  def onApply(self): 
    inputVolume = self.inputSelector.currentNode() 
    outputVolume = self.outputSelector.currentNode() 
    if not (inputVolume and outputVolume): 
      qt.QMessageBox.critical( 
          slicer.util.mainWindow(), 
          'Sharpen', 'Input and output volumes are required for Laplacian') 
      return 
    # run the filter 
    laplacian = vtk.vtkImageLaplacian() 
    laplacian.SetInputData(inputVolume.GetImageData()) 
    laplacian.SetDimensionality(3) 
    laplacian.GetOutput().Update() 
    ijkToRAS = vtk.vtkMatrix4x4() 
    inputVolume.GetIJKToRASMatrix(ijkToRAS) 
    outputVolume.SetIJKToRASMatrix(ijkToRAS) 
    outputVolume.SetAndObserveImageData(laplacian.GetOutput()) 
 
    # optionally subtract laplacian from original image 
    if self.sharpen.checked: 
      parameters = {} 
      parameters['inputVolume1'] = inputVolume.GetID() 
      parameters['inputVolume2'] = outputVolume.GetID() 
      parameters['outputVolume'] = outputVolume.GetID() 
      slicer.cli.run( slicer.modules.subtractscalarvolumes, None, parameters, wait_for_completion=True )  
 
    selectionNode = slicer.app.applicationLogic().GetSelectionNode() 
    selectionNode.SetReferenceActiveVolumeID(outputVolume.GetID()) 
    slicer.app.applicationLogic().PropagateVolumeSelection(0) 

请您参考如下方法:

TL;DR 将 laplacian.GetOutput().Update() 更改为 laplacian.Update()。

解释: 根据 this链接,VTK 6 中引入了一个重大变化。总而言之,较新版本的 VTK 将算法和数据分离到两个不同的类层次结构中。在较新版本的 VTK 中,Update() 函数只能在派生自 vtkAlgorithm 类的对象上调用。可以看看vtkImageLaplacian的继承图here它确实是从 vtkAlgorithm 类派生的。所以 laplacian.Update() 将起作用。

顾名思义,vtkImageData 是一个数据对象。 laplacian.GetOutput() 返回一个 vtkImageData 对象,这就是为什么您不能对其调用 Update() 函数,因此会出现错误。