使用Python?VTK?完成圖像切割
1、讀取二維圖片序列完成面繪制
2、vtk.vtkOutlineFilter()介紹
這個(gè)空間就相當(dāng)于生成渲染模型的輪廓線,比如三維圖像大小為(256x256x200),那么這個(gè)控件就會(huì)生成一個(gè)長寬高分別為256x256x200的一個(gè)長方體框架

詳細(xì)介紹:VTK官方文檔
3、隱函數(shù)平面模塊vtk.vtkImplicitPlaneWidget()
使用該模塊可以靈活的調(diào)整需要選取的平面 vtkImplicitPlaneWidget官方文檔

4、vtk.vtkClipPolyData()
vtkclippolydata的剪切結(jié)果,根據(jù)切平面法線分為上下兩部分,接口中有相應(yīng)的輸出接口
切割效果展示

代碼如下:
import vtk
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:/Users/deng5/Desktop/2/48/")
Reader.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()
skin = vtk.vtkActor()
skin.SetMapper(skinMapper)
#定義一個(gè)圖像邊界控件
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.ResetCamera()
arender.SetBackground(.2, .3, .4)
arender.ResetCameraClippingRange()
renWin.SetSize(1000, 1000)
style = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style);
#定義切割器
global cliper
cliper = vtk.vtkClipPolyData()
cliper.SetInputData(skinStripper.GetOutput())
#定義平面隱函數(shù)
implicitPlaneWidget = vtk.vtkImplicitPlaneWidget()
implicitPlaneWidget.SetInteractor(iren)
implicitPlaneWidget.SetPlaceFactor(1.25)
implicitPlaneWidget.SetInputData(skinStripper.GetOutput())
implicitPlaneWidget.PlaceWidget()
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)
#關(guān)聯(lián)CallBack函數(shù)
implicitPlaneWidget.AddObserver("EndInteractionEvent", my_call_back)
implicitPlaneWidget.On()
renWin.Render()
iren.Initialize()
iren.Start()
#CallBack函數(shù)
def my_call_back(pWidget,ev):
#表示當(dāng)pWidget控件改變時(shí),觸發(fā)函數(shù)
if (pWidget):
print(pWidget.GetClassName(), "Event Id:", ev)
planeNew = vtk.vtkPlane()
#獲得pWidget中的平面,將平面值賦值planeNew
pWidget.GetPlane(planeNew)
#cliper將裁剪器cliper的平面設(shè)置為planeNew
cliper.SetClipFunction(planeNew)
planeNew.GetNormal()
cliper.Update();
#將裁減后的模型傳遞給另一個(gè)窗口
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()
到此這篇關(guān)于使用Python VTK 完成圖像切割的文章就介紹到這了,更多相關(guān)Python VTK圖像切割內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
selenium+python設(shè)置爬蟲代理IP的方法
這篇文章主要介紹了selenium+python設(shè)置爬蟲代理IP的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Django 響應(yīng)數(shù)據(jù)response的返回源碼詳解
這篇文章主要介紹了Django 響應(yīng)數(shù)據(jù)response的返回源碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python實(shí)現(xiàn)mysql數(shù)據(jù)庫更新表數(shù)據(jù)接口的功能
這篇文章主要給大家介紹了關(guān)于Python如何實(shí)現(xiàn)mysql數(shù)據(jù)庫更新表數(shù)據(jù)接口功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
scrapy在python爬蟲中搭建出錯(cuò)的解決方法
在本篇文章里小編給大家整理了一篇關(guān)于scrapy在python爬蟲中搭建出錯(cuò)的解決方法,有需要的朋友們可以學(xué)習(xí)參考下。2020-11-11
python實(shí)現(xiàn)百萬答題自動(dòng)百度搜索答案
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)百萬答題自動(dòng)百度搜索答案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
10分鐘用python搭建一個(gè)超好用的CMDB系統(tǒng)
這篇文章主要介紹了10分鐘用python搭建一個(gè)超好用的CMDB系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python實(shí)現(xiàn)高分辨率圖像導(dǎo)航的代碼
高分辨率圖像導(dǎo)航是一種技術(shù),它允許用戶在大型圖像中進(jìn)行導(dǎo)航和瀏覽,而無需加載整個(gè)圖像到內(nèi)存中,在本文中,我們將使用30行Python代碼實(shí)現(xiàn)這一功能,我們將使用Python的圖像處理庫和計(jì)算機(jī)視覺庫來加載圖像數(shù)據(jù)并生成高分辨率圖像導(dǎo)航2024-03-03
Python常用標(biāo)準(zhǔn)庫之os模塊功能
這篇文章主要介紹了Python常用標(biāo)準(zhǔn)庫之os模塊功能,os模塊的主要功能有系統(tǒng)相關(guān)、目錄及文件操作、執(zhí)行命令和管理進(jìn)程,其中的進(jìn)程管理功能主要是Linux相關(guān)的,此處不做討論,對(duì)Python標(biāo)準(zhǔn)庫os相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2022-11-11

