我现在在Python中使用VTK。这是我第一次使用Python进行编程。
输出3D对象后,我们想使用AddClippingPlane
函数输出对象的横截面。
但是,将显示以下错误,并且不会执行:
TypeError:未绑定的方法需要一个 将vtkRenderingCorePython.vtkAbstractMapper作为第一个参数。
当您看到所附的代码时,您能告诉我怎么了吗?非常感谢您的帮助。
import vtk
import sys
from PyQt5 import QtCore,QtWidgets
from PyQt5.QtGui import *
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5.QtWidgets import QMainWindow, QApplication
from foo import Ui_MainWindow
from PyQt5 import Qt
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setMouseTracking(True)
self.setupUi(self)
self.pushButton.clicked.connect(self.OpenVTK)
self.pushButton2.clicked.connect(
QtCore.QCoreApplication.instance().quit)
def OpenVTK(self):
self.setMouseTracking(True)
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl = Qt.QVBoxLayout()
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName(FilePath)
reader.Update()
colors = vtk.vtkNamedColors()
colors.SetColor("BkgColor", [51, 77, 102, 255])
boneExtractor = vtk.vtkMarchingCubes()
boneExtractor.SetInputConnection(reader.GetOutputPort())
boneExtractor.SetValue(0, 1000)
boneStripper = vtk.vtkStripper()
boneStripper.SetInputConnection(boneExtractor.GetOutputPort())
boneMapper = vtk.vtkPolyDataMapper()
boneMapper.SetInputConnection(boneStripper.GetOutputPort())
boneMapper.ScalarVisibilityOff()
plane1 = vtk.vtkPlane()
plane1.SetOrigin(0.05, 0.0, 0.0) # [*1]
plane1.SetNormal(-1.0, 0.0, 0.0)
bone = vtk.vtkActor()
bone.SetMapper(boneMapper)
bone.GetProperty().SetDiffuseColor(colors.GetColor3d("Ivory"))
bone = vtk.vtkAbstractMapper.AddClippingPlane(plane1)
bwLut = vtk.vtkLookupTable()
bwLut.SetTableRange(0, 2000)
bwLut.SetSaturationRange(0, 1)
bwLut.SetHueRange(0, 0)
bwLut.SetValueRange(0, 1)
bwLut.Build()
sagittalColors = vtk.vtkImageMapToColors()
sagittalColors.SetInputConnection(reader.GetOutputPort())
sagittalColors.SetLookupTable(bwLut)
sagittalColors.Update()
sagittal = vtk.vtkImageActor()
sagittal.GetMapper().SetInputConnection(sagittalColors.GetOutputPort())
sagittal.SetDisplayExtent(250, 250, 0, 501, 0, 500)
aCamera = vtk.vtkCamera()
aCamera.SetViewUp(0, 0, -1)
aCamera.SetPosition(0, -1, 0)
aCamera.SetFocalPoint(0, 0, 0)
aCamera.ComputeViewPlaneNormal()
aCamera.Azimuth(0.0)
aCamera.Elevation(0.0)
self.ren.AddActor(sagittal)
self.ren.AddActor(bone)
self.ren.SetActiveCamera(aCamera)
self.show()
self.ren.ResetCamera()
self.frame.setLayout(self.vl)
aCamera.Dolly(1.0)
self.ren.ResetCameraClippingRange()
self.show()
self.iren.Initialize()
self.iren.Start()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您的错误来自bone = vtk.vtkAbstractMapper.AddClippingPlane(plane1)
行。
AddClippingPlane()
是实例方法,必须应用于对象。
应该类似于boneMapper.AddClippingPlane(plane1)