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()
Renwin.AddRenderer(aRender)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(Renwin)
# Define a picture reading interface
# Read PNG Replace the picture with PNG_Reader = vtk.vtkPNGReader()
Jpg_Reader = vtk.vtkJPEGReader()
Jpg_Reader.SetNumberOfScalarComponents(1)
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
Jpg_Reader.SetFilePrefix("E:/outer/label/5/")
# Set image prefix name
# Indicates that the image prefix is a number ( Such as :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 showing
window.Render()
interactor.Initialize()
interactor.Start()
Copy code
Effect display
copyright notice
author[Pie star's keyboard],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310453026666.html
The sidebar is recommended
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
guess what you like
-
Parameter passing of Python function
-
Stroke tuple in Python
-
Talk about ordinary functions and higher-order functions in Python
-
[Python data acquisition] page image crawling and saving
-
[Python data collection] selenium automated test framework
-
Talk about function passing and other supplements in Python
-
Python programming simulation poker game
-
leetcode 160. Intersection of Two Linked Lists (python)
-
Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
-
Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems
Random recommended
- My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
- Python interface automation testing framework -- if you want to do well, you must first sharpen its tools
- Python multi thread crawling weather website pictures and saving
- How to convert pandas data to excel file
- Python series tutorials 122
- Python Complete Guide - printing data using pyspark
- Python Complete Guide -- tuple conversion array
- Stroke the list in python (top)
- Analysis of Python requests module
- Comments and variables in Python
- New statement match, the new version of Python is finally going to introduce switch case?
- Fanwai 6 Different operations for image details in Python opencv
- Python crawler native code learning (I)
- Python quantitative data warehouse building series 2: Python operation database
- Python code reading (Part 50): taking elements from list intervals
- Pyechart + pandas made word cloud pictures of 19 report documents
- [Python crawler] multithreaded daemon & join() blocking
- Python crawls cat pictures in batches to realize thousand image imaging
- Van * Python | simple crawling of a planet
- Input and output of Python practice
- Django ORM details - fields, attributes, operations
- Python web crawler - crawling cloud music review (3)
- Stroke list in python (bottom)
- What cat is the most popular? Python crawls the whole network of cat pictures. Which one is your favorite
- [algorithm learning] LCP 06 Take coins (Java / C / C + + / Python / go / trust)
- Python shows the progress of downloading files from requests
- Solve the problem that Django celery beat prompts that the database is incorrectly configured and does not support multiple databases
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python quantitative data warehouse construction 3: data drop library code encapsulation
- The source code and implementation of Django CSRF Middleware
- Python hashlib module
- The cover of Python 3 web crawler development (Second Edition) has been determined!
- The introduction method of executing Python source code or Python source code file (novice, please enter the old bird and turn left)
- [Python basics] explain Python basic functions in detail, including teaching and learning
- Python web crawler - crawling cloud music review (4)
- The first step of scientific research: create Python virtual environment on Linux server
- Writing nmap scanning tool in Python -- multithreaded version
- leetcode 2057. Smallest Index With Equal Value(python)
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python crawler actual combat, requests module, python realizes capturing a video barrage