ulovepython画三维函数图_使⽤Python+VTK实现三维模型的显⽰和
切割(⾯绘制)
本⽂将介绍使⽤VTK的Python版本完成⾯绘制已经模型的切割
会使⽤的模块介绍
1、读取⼆维图⽚序列完成⾯绘制
详情见读取⼆维序列显⽰
2、vtk.vtkOutlineFilter()介绍
这个空间就相当于⽣成渲染模型的轮廓线,⽐如三维图像⼤⼩为(256x256x200),那么这个控件就会⽣成⼀个长宽⾼分别为256想
x256x200的⼀个长⽅体框架
详细介绍:VTK官⽅⽂档
3、隐函数平⾯模块vtk.vtkImplicitPlaneWidget()
使⽤该模块可以灵活的调整需要选取的平⾯
vtkImplicitPlaneWidget官⽅⽂档
4、vtk.vtkClipPolyData()
vtkclippolydata的剪切结果,根据切平⾯法线分为上下两部分,接⼝中有相应的输出接⼝
切割效果展⽰
代码如下:
import vtkpcos
def main():
arender = vtk.vtkRenderer()
arender.SetViewport(0, 0.0, 0.5, 1.0)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(arender)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Reader = vtk.vtkMetaImageReader()
# Reader.SetFileName("bbb.mhd")
# Reader.Update()
#读取图⽚、⾯绘制
Reader = vtk.vtkPNGReader()
Reader.SetNumberOfScalarComponents(1)
Reader.GetOutput().GetOrigin()
Reader.SetDataByteOrderToLittleEndian()
Reader.SetFileDimensionality(3)圣经朗读
Reader.SetDataExtent(0, 512, 0, 512,0, 226)
Reader.SetFilePrefix("E:/qct_data/in_out_data/in_data/inner/label/22/") #Reader.SetFilePrefix("C:/Urs/deng5/Desktop/2/48/")
cellularReader.SetFilePattern("%s%d.png")
Reader.SetDataSpacing(1, 1, 1) # Volume Pixel
Reader.Update()
#⾯绘制代码,详情见使⽤python-vtk完成⾯绘制⽂章
skinExtractor = vtk.vtkContourFilter()
skinExtractor.SetInputConnection(Reader.GetOutputPort()) skinExtractor.SetValue(0, 1)
skinExtractor.ComputeGradientsOn();
skinExtractor.ComputeScalarsOn();
smooth = vtk.vtkSmoothPolyDataFilter()
smooth.SetInputConnection(skinExtractor.GetOutputPort())
smooth.SetNumberOfIterations(100)
skinNormals = vtk.vtkPolyDataNormals()
skinNormals.SetInputConnection(smooth.GetOutputPort()) skinNormals.SetFeatureAngle(50)
skinStripper = vtk.vtkStripper()
skinStripper.SetInputConnection(skinNormals.GetOutputPort()) skinMapper = vtk.vtkPolyDataMapper()
skinMapper.SetInputConnection(skinStripper.GetOutputPort()) skinMapper.ScalarVisibilityOff()
grelide
怎么网页翻译
skin = vtk.vtkActor()
skin.SetMapper(skinMapper)
#定义⼀个图像边界控件
outlineData = vtk.vtkOutlineFilter()
outlineData.SetInputConnection(Reader.GetOutputPort())
mapOutline = vtk.vtkPolyDataMapper()
mapOutline.SetInputConnection(outlineData.GetOutputPort())
outline = vtk.vtkActor()
outline.SetMapper(mapOutline)
outline.GetProperty().SetColor(0, 0, 0)
aCamera = vtk.vtkCamera()
aCamera.SetViewUp(0, 0, -1)
aCamera.SetPosition(0, 1, 0)
aCamera.ComputeViewPlaneNormal()
aCamera.Azimuth(30.0)
aCamera.Elevation(30.0)
aCamera.Dolly(1.5)
arender.AddActor(outline)
arender.AddActor(skin)
#splineActor.GetProperty().SetLineWidth(5)
#arender.AddActor(splineActor)
#arender.AddActor(pointActor)
arender.SetActiveCamera(aCamera)
arender.RetCamera()
buenos airesarender.SetBackground(.2, .3, .4)
土库曼斯坦共和国
arender.RetCameraClippingRange()
renWin.SetSize(1000, 1000)
style = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style);
#定义切割器
global cliper
cliper = vtk.vtkClipPolyData()
cliper.SetInputData(skinStripper.GetOutput())
#定义平⾯隐函数
implicitPlaneWidget = vtk.vtkImplicitPlaneWidget() implicitPlaneWidget.SetInteractor(iren) implicitPlaneWidget.SetPlaceFactor(1.25) implicitPlaneWidget.SetInputData(skinStripper.GetOutput()) implicitPlaneWidget.PlaceWidget()
patented
global coneSkinActor
coneSkinActor = vtk.vtkActor()
coneSkinActor.SetMapper(skinMapper)
rRenderer = vtk.vtkRenderer()
rRenderer.SetBackground(0.2, 0.3, 0.5)
rRenderer.SetViewport(0.5, 0.0, 1.0, 1.0)
coneSkinActor.RotateZ(90)
rRenderer.AddActor(coneSkinActor)
renWin.AddRenderer(rRenderer)
#关联CallBack函数
implicitPlaneWidget.AddObrver("EndInteractionEvent", my_call_back) implicitPlaneWidget.On()
renWin.Render()
iren.Initialize()
iren.Start()
#CallBack函数
def my_call_back(pWidget,ev):
#表⽰当pWidget控件改变时,触发函数
if (pWidget):
print(pWidget.GetClassName(), "Event Id:", ev)
planeNew = vtk.vtkPlane()
#获得pWidget中的平⾯,将平⾯值赋值planeNew
pWidget.GetPlane(planeNew)
#cliper将裁剪器cliper的平⾯设置为planeNew
cliper.SetClipFunction(planeNew)
planeNew.GetNormal()
cliper.Update();
#将裁减后的模型传递给另⼀个窗⼝
clipedData = vtk.vtkPolyData()
好奇心英文
clipedData.DeepCopy(cliper.GetOutput())
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputData(clipedData)
coneMapper.ScalarVisibilityOff()
coneSkinActor.SetMapper(coneMapper)
print("Plane Normal = "+str(planeNew.GetNormal()))
print("Plane Origin = "+str(planeNew.GetOrigin()))
main()