-
Notifications
You must be signed in to change notification settings - Fork 0
/
3d_model.py
74 lines (58 loc) · 2.3 KB
/
3d_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import vtk
# Create the DICOM reader
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName('D:\\DIcom gans\\Data\\raw')
reader.Update()
# Get the image data from the reader
imageData = reader.GetOutput()
# Resampling the volume to your extracted voxel size using vtkImageReslice
resampler = vtk.vtkImageReslice()
resampler.SetInputData(imageData)
resampler.SetOutputSpacing(0.180311183, 0.180311183, 0.18031118) # Voxel size from your log
resampler.SetInterpolationModeToLinear()
resampler.Update()
resampledData = resampler.GetOutput()
# Set up the volume mapper using the resampled data
volumeMapper = vtk.vtkSmartVolumeMapper()
volumeMapper.SetInputData(resampledData)
# Volume properties for visualization, adjusted to your specific data characteristics
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.ShadeOn()
volumeProperty.SetInterpolationTypeToLinear()
# Opacity and color transfer functions, which may need to be fine-tuned to your data
opacityTransferFunction = vtk.vtkPiecewiseFunction()
opacityTransferFunction.AddPoint(0, 0.0)
opacityTransferFunction.AddPoint(500, 0.15)
opacityTransferFunction.AddPoint(1000, 0.85)
opacityTransferFunction.AddPoint(1150, 1.0)
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(0, 0.0, 0.0, 0.0)
colorTransferFunction.AddRGBPoint(500, 1.0, 0.5, 0.3)
colorTransferFunction.AddRGBPoint(1000, 1.0, 1.0, 0.9)
colorTransferFunction.AddRGBPoint(1150, 1.0, 1.0, 1.0)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.SetColor(colorTransferFunction)
# Set up the volume actor
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
# Renderer setup
renderer = vtk.vtkRenderer()
renderer.AddVolume(volume)
renderer.SetBackground(0, 0, 0) # Black background
# Render window setup
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindow.SetSize(800, 800)
# Render window interactor setup
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Camera settings for a better view angle
camera = renderer.GetActiveCamera()
camera.SetViewUp(0, 0, -1)
camera.SetPosition(-500, -500, 500)
camera.SetFocalPoint(0, 0, 0)
renderer.ResetCamera()
# Start the interaction
renderWindow.Render()
renderWindowInteractor.Start()