current position:Home>Mapping 3D model surface distances using Python VTK
Mapping 3D model surface distances using Python VTK
2022-01-31 05:55:25 【Pie star's keyboard】
This is my participation 11 The fourth of the yuegengwen challenge 7 God , Check out the activity details :2021 One last more challenge
This article explains how to use it VTK Read the picture and calculate two three-dimensional models (stl) Surface distance of , And map its distance value to the model .
Data preparation : Need to prepare two stl file 、Python Need to install vtk library
Step one : data fetch First, through vtk.vtkSTLReader() Definition stl File reading interface , Re pass reader1.GetOutput() You can get stl stay vtk Workflow data .
Step two : Remove duplicate points adopt vtk.vtkCleanPolyData() You can remove duplicate points in the model
Step three : Calculated distance Use vtk.vtkDistancePolyDataFilter() , Use the data after filtering out duplicate points in the previous step as input . Such as distanceFilter.SetInputConnection(1, clean1.GetOutputPort()), The first parameter is the label of the input data , from 0 Start counting ; The second parameter is the input data . We will vtkDistancePolyDataFilter Of output to mapper The distance mapping is completed .
Step four : Color configuration lut = vtk.vtkLookupTable() Equivalent to a palette function , By changing its parameters , Adjust the color range of the final mapping . scalarBar = vtk.vtkScalarBarActor() It's the color bar , Map the distance value to color according to the results of the previous palette .
import vtk
input1 = vtk.vtkPolyData()
reader1 = vtk.vtkSTLReader()
reader1.SetFileName('model1.stl')
reader1.Update()
input1 = reader1.GetOutput() # Read the model A
input2 = vtk.vtkPolyData()
reader2 = vtk.vtkSTLReader()
reader2.SetFileName('model2.stl')
reader2.Update()
input2 = reader2.GetOutput() # Read the model B
# Data merging , You can merge and display two models
clean1 = vtk.vtkCleanPolyData()
clean1.SetInputData(input1)
clean2 = vtk.vtkCleanPolyData()
clean2.SetInputData(input2)
distanceFilter = vtk.vtkDistancePolyDataFilter()
distanceFilter.SetInputConnection(1, clean1.GetOutputPort())
distanceFilter.SetInputConnection(0, clean2.GetOutputPort())
distanceFilter.SignedDistanceOff()
distanceFilter.Update() # Calculated distance
distanceFilter.GetOutputPort()
mapper = vtk.vtkPolyDataMapper() # To configure mapper
mapper.SetInputConnection(distanceFilter.GetOutputPort())
mapper.SetScalarRange( # Set the color mapping range
distanceFilter.GetOutput().GetPointData().GetScalars().GetRange()[0],
distanceFilter.GetOutput().GetPointData().GetScalars().GetRange()[1])
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor1 = vtk.vtkActor()
actor1.SetMapper(mapper)
lut = vtk.vtkLookupTable()
lut.SetHueRange(0.2, 0.7) # Mapped color transformation parameters ( Adjust your own color )
# lut.SetAlphaRange(1.0, 1.0)
# lut.SetValueRange(1.0, 1.0)
# lut.SetSaturationRange(1.0, 1.0)
# lut.SetNumberOfTableValues(256)
mapper.SetLookupTable(lut)
mapper2 = vtk.vtkPolyDataMapper()
mapper2.SetInputData((distanceFilter.GetSecondDistanceOutput()))
mapper2.SetScalarRange( # Set the color mapping range
distanceFilter.GetSecondDistanceOutput().GetPointData().GetScalars().GetRange()[0],
distanceFilter.GetSecondDistanceOutput().GetPointData().GetScalars().GetRange()[1])
actor2 = vtk.vtkActor()
actor2.SetMapper(mapper2)
scalarBar = vtk.vtkScalarBarActor() # Set up color_bar
scalarBar.SetLookupTable(mapper.GetLookupTable())
scalarBar.SetTitle("SD(mm)")
scalarBar.SetNumberOfLabels(5) # Set the number of scale labels to display . Set the position of the ribbon by yourself
scalarBar.SetMaximumNumberOfColors(10)
# scalarBar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
# scalarBar.GetPositionCoordinate().SetValue(0.01, 0.49) # The smaller the parameter, the more left , The larger the second parameter is, the higher it goes
# scalarBar.SetWidth(0.16)
# scalarBar.SetHeight(0.5)
# scalarBar.SetTextPositionToPrecedeScalarBar() # Whether the title and scale mark should precede the scalar column ( The text will appear to the left of the bar )
# # Set the margin between the title and the bar
# scalarBar.SetVerticalTitleSeparation(10)
# # Set the title color
scalarBar.DrawTickLabelsOn()
scalarBar.GetTitleTextProperty().SetColor(0, 0, 0)
scalarBar.GetLabelTextProperty().SetColor(0, 0, 0)
arender = vtk.vtkRenderer()
arender.SetViewport(0, 0.0, 1, 1.0)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(arender)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
style = vtk.vtkInteractorStyleTrackballActor()
iren.SetInteractorStyle(style)
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(actor)
# arender.AddActor(actor1)
arender.SetActiveCamera(aCamera)
arender.ResetCamera()
arender.SetBackground(1, 1, 1)
arender.ResetCameraClippingRange()
arender.AddActor2D(scalarBar)
renWin.Render()
iren.Initialize()
iren.Start()
Copy code
Result example :
copyright notice
author[Pie star's keyboard],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310555231274.html
The sidebar is 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
guess what you like
-
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
Random recommended
- 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
- [algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)
- Test platform series (71) Python timed task scheme
- Java AES / ECB / pkcs5padding encryption conversion Python 3
- Loguru: the ultimate Python log solution
- Blurring and anonymizing faces using OpenCV and python
- How fast Python sync and async execute
- Python interface automation test framework (basic) -- common data types list & set ()
- Python crawler actual combat, requests module, python realizes capturing video barrage comments of station B
- Python: several implementation methods of multi process
- Sword finger offer II 054 Sum of all values greater than or equal to nodes | 538 | 1038 (Java / C / C + + / Python / go / trust)
- How IOS developers learn python programming 3-operator 2
- How IOS developers learn python programming 2-operator 1
- [Python applet] 8 lines of code to realize file de duplication
- Python uses the pynvml tool to obtain the working status of GPU
- Data mining: Python actual combat multi factor analysis
- Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency
- Use Python VTK to batch read 2D slices and display 3D models
- Complete image cutting using Python version VTK
- Python interface automation test framework (basic) -- common data types Dict
- Python specific text extraction in actual combat challenges the first step of efficient office