current position:Home>Complete image cutting using Python version VTK
Complete image cutting using Python version VTK
2022-01-31 04:56:02 【Pie star's keyboard】
This is my participation 11 The fourth of the yuegengwen challenge 5 God , Check out the activity details :2021 One last more challenge
This article introduces the use of VTK Of Python Version finish the cutting of the surface drawing model
Introduction to the modules that will be used
1、 Read two-dimensional picture sequence and finish surface drawing
For details, see Read 2D sequence display
2、vtk.vtkOutlineFilter() Introduce
This space is equivalent to generating the outline of the rendered model , For example, the three-dimensional image size is (256x256x200), Then this control will generate a control with length, width and height respectively 256x256x200 A cuboid frame
Detailed introduction :VTK Official documents
3、 Implicit function plane module vtk.vtkImplicitPlaneWidget()
Using this module, the plane to be selected can be flexibly adjusted vtkImplicitPlaneWidget Official documents
4、vtk.vtkClipPolyData()
vtkclippolydata The shear results of , It is divided into upper and lower parts according to the normal of the tangent plane , There are corresponding output interfaces in the interface
vtkClipPolyData Official documents
Cutting effect display
The code is as follows :
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()
# Read the picture 、 Face painting
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()
# Face drawing code , For details, see use python-vtk Finish drawing the article
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)
# Define an image boundary control
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);
# Define cutter
global cliper
cliper = vtk.vtkClipPolyData()
cliper.SetInputData(skinStripper.GetOutput())
# Define the plane implicit function
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)
# relation CallBack function
implicitPlaneWidget.AddObserver("EndInteractionEvent", my_call_back)
implicitPlaneWidget.On()
renWin.Render()
iren.Initialize()
iren.Start()
#CallBack function
def my_call_back(pWidget,ev):
# Said when pWidget When the control changes , Trigger function
if (pWidget):
print(pWidget.GetClassName(), "Event Id:", ev)
planeNew = vtk.vtkPlane()
# get pWidget The plane in , Assign the plane value planeNew
pWidget.GetPlane(planeNew)
#cliper Clip the clipper cliper The plane of is set to planeNew
cliper.SetClipFunction(planeNew)
planeNew.GetNormal()
cliper.Update();
# Pass the trimmed model to another window
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()
Copy code
copyright notice
author[Pie star's keyboard],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310456006189.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