current position:Home>Use Python VTK to batch read 2D slices and display 3D models

Use Python VTK to batch read 2D slices and display 3D models

2022-01-31 04:53:05 Pie star's keyboard

This is my participation 11 The fourth of the yuegengwen challenge 6 God , Check out the activity details :2021 One last more challenge

VTK,(visualizationtoolkit) It is a free software system with open resources , Mainly used in 3D computer graphics 、 Image processing and visualization .Vtk It is designed and implemented on the basis of object-oriented principle , Its kernel uses C++ Built , Contains about 250,000 Line code ,2000 Multiple classes , There are also several conversion interfaces , So it's free to pass Java,Tcl/Tk and Python Use of various languages vtk.

This article explains how to use it Python Version of VTK Complete 2D slice batch reading and 3D reconstruction .

Main function introduction

vtk.vtkJPEGReader(): Interface for reading pictures , You can set the position of the read picture through this interface 、 size 、 dimension . It should be noted that , If you need to read a list of two-dimensional pictures into a three-dimensional image, you need to name some pictures in natural order 0、1、2、3···, such VTK To read the picture correctly . If you need to read pictures in other formats, you only need to put JPEG Just change to the corresponding format , Such as PNG.

vtk.vtkMarchingCubes(): 3D reconstruction function ,MarchingCubes The algorithm is actually a divide and conquer method , Because it distributes the extraction of isosurface to each voxel (voxel) In the middle of . For each voxel being processed , Use triangular patches to approach its internal isosurface . Each voxel is a small cube (cube), In the process of constructing triangular patches, each voxel is “ scanning ” Again , It's like a processor moving on these voxels , It's also called .·· In the process of isosurface extraction, a series of two-dimensional slice data are regarded as a three-dimensional data field , Extract substances with a certain threshold from , Connected into triangular patches in some topological form , therefore MC The algorithm is also called “ Isosurface extraction ”(IsosurfaceExtraction) Algorithm .·· In medical applications , use MC The algorithm can reconstruct the external contour of the human body 、 Internal tissues and organs , So that doctors can directly observe the spatial relationship between interested organs and surrounding tissues on three-dimensional images .

import vtk
#  Define the rendering window 、 Interactive mode 
aRender = vtk.vtkRenderer()
Renwin = vtk.vtkRenderWindow()
iren = vtk.vtkRenderWindowInteractor()

#  Define a picture reading interface 
# Read PNG Replace the picture with PNG_Reader = vtk.vtkPNGReader()
Jpg_Reader = vtk.vtkJPEGReader()
Jpg_Reader.SetFileDimensionality(3)  #  It shows that the image is three-dimensional 
 #  Define image size , This line indicates that the image size is (512*512*240)
Jpg_Reader.SetDataExtent(0, 512, 0, 512, 0, 240) 
 #  Set the storage location of the image 
 #  Set image prefix name 
 # Indicates that the image prefix is a number ( Such as :0.jpg)

#  Method of calculating contour 
contour = vtk.vtkMarchingCubes()  
contour.SetValue(0, 255)

mapper = vtk.vtkPolyDataMapper()

actor = vtk.vtkActor()

renderer = vtk.vtkRenderer()
renderer.SetBackground([0.1, 0.1, 0.5])

window = vtk.vtkRenderWindow()
window.SetSize(512, 512)

interactor = vtk.vtkRenderWindowInteractor()

#  Start showing 
 Copy code 

Effect display


copyright notice
author[Pie star's keyboard],Please bring the original link to reprint, thank you.

Random recommended