This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

VTK (visual visualizationtoolkit) is an open source free software system for 3d computer graphics, image processing and visualization. Vtk is designed and implemented on the basis of object-oriented principles. Its core is built in C++, contains approximately 250,000 lines of code, over 2000 classes, and contains several conversion interfaces, so it is also free to use Vtk through Java, Tcl/Tk, and Python languages.

This article describes how to use the Python version of VTK to complete 2d slice batch reading and 3D reconstruction.

Introduction to main functions

Vtk.vtkjpegreader (): the interface for reading images. You can use this interface to set the position, size, and dimension of the image to be read. Note that if you want to read a list of TWO-DIMENSIONAL images into a three-dimensional image, you need to name some images in the natural order 0, 1, 2, 3···, so that VTK can read the image correctly. If you need to read images in other formats, simply change the JPEG in the function to the corresponding format, such as PNG.

Vtk.vtkmarchingcubes (): A 3d reconstruction function. The MarchingCubes algorithm is actually a divide-and-conquer method, as it extracts the isosurface in each voxel. For each processed voxel, the internal isosurface is approximated by a triangular plane. Each voxel is a small cube that is “scanned” for each voxel during the process of constructing the triangle, as if a processor were moving on these voxels, hence the name. ·· in the process of IsosurfaceExtraction, a series of 2d slice data is regarded as a 3d data field, from which substances with a certain threshold value are extracted and connected into triangular facets in a certain topological form. Therefore, MC algorithm is also called IsosurfaceExtraction algorithm. ·· In medical application, THE MC algorithm can be used to reconstruct the external contour and internal tissues and organs of the human body, so that doctors can directly observe the spatial relationship between the organs of interest and surrounding tissues on the THREE-DIMENSIONAL image.

import vtk
# Define render window, interaction mode
aRender = vtk.vtkRenderer()
Renwin = vtk.vtkRenderWindow()
Renwin.AddRenderer(aRender)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(Renwin)

# Define a picture reading interface
PNG_Reader = vtk.vtkpngreader ()
Jpg_Reader = vtk.vtkJPEGReader()
Jpg_Reader.SetNumberOfScalarComponents(1)
Jpg_Reader.SetFileDimensionality(3)  # Indicate that the image is three-dimensional
 Size = 512*512*240
Jpg_Reader.SetDataExtent(0.512.0.512.0.240) 
 # Set the image location
Jpg_Reader.SetFilePrefix("E:/outer/label/5/")
 # set image prefix name
 # indicates that the image is prefixed with a number (e.g. 0.jpg)
Jpg_Reader.SetFilePattern("%s%d.jpg")
Jpg_Reader.Update()
Jpg_Reader.SetDataByteOrderToLittleEndian()

# Method of calculating contour
contour = vtk.vtkMarchingCubes()  
contour.SetInputConnection(Jpg_Reader.GetOutputPort())
contour.ComputeNormalsOn()
contour.SetValue(0.255) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(contour.GetOutputPort()) mapper.ScalarVisibilityOff() actor  = vtk.vtkActor() actor.SetMapper(mapper) renderer = vtk.vtkRenderer() renderer.SetBackground([0.1.0.1.0.5])
renderer.AddActor(actor)

window = vtk.vtkRenderWindow()
window.SetSize(512.512)
window.AddRenderer(renderer)


interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(window)

# start display
window.Render()
interactor.Initialize()
interactor.Start()
Copy the code

Results show