Debian Codegraph @

examples/02-plot/volume_rendering.py

raw
1 """
2 .. _volume_rendering_example:
3
4 Volume Rendering
5 ~~~~~~~~~~~~~~~~
6
7 Volume render uniform mesh types like :class:`pyvista.ImageData` or 3D
8 NumPy arrays.
9
10 This also explores how to extract a volume of interest (VOI) from a
11 :class:`pyvista.ImageData` using the
12 :func:`pyvista.ImageDataFilters.extract_subset` filter.
13 """
14
15 # sphinx_gallery_thumbnail_number = 3
16 import numpy as np
17 import pyvista as pv
18 from pyvista import examples
19
20 # sphinx_gallery_start_ignore
21 # volume rendering does not work in interactive plots currently
22 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
23 # sphinx_gallery_end_ignore
24
25 # Download a volumetric dataset
26 vol = examples.download_knee_full()
27 vol
28
29 # %%
30 # Simple Volume Render
31 # ++++++++++++++++++++
32 #
33
34 # A nice camera position
35 cpos = pv.CameraPosition(
36 position=(-381.74, -46.02, 216.54),
37 focal_point=(74.8305, 89.2905, 100.0),
38 viewup=(0.23, 0.072, 0.97),
39 )
40
41 vol.plot(volume=True, cmap='bone', cpos=cpos)
42
43
44 # %%
45 # Opacity Mappings
46 # ++++++++++++++++
47 #
48 # Or use the :func:`pyvista.Plotter.add_volume` method like below.
49 # Note that here we use a non-default opacity mapping to a sigmoid:
50
51 pl = pv.Plotter()
52 pl.add_volume(vol, cmap='bone', opacity='sigmoid')
53 pl.camera_position = cpos
54 pl.show()
55
56 # %%
57 # You can also use a custom opacity mapping
58 opacity = [0, 0, 0, 0.1, 0.3, 0.6, 1]
59
60 pl = pv.Plotter()
61 pl.add_volume(vol, cmap='viridis', opacity=opacity)
62 pl.camera_position = cpos
63 pl.show()
64
65 # %%
66 # We can also use a shading technique when volume rendering with the ``shade``
67 # option
68 pl = pv.Plotter(shape=(1, 2))
69 pl.add_volume(vol, cmap='viridis', opacity=opacity, shade=False)
70 pl.add_text('No shading')
71 pl.camera_position = cpos
72 pl.subplot(0, 1)
73 pl.add_volume(vol, cmap='viridis', opacity=opacity, shade=True)
74 pl.add_text('Shading')
75 pl.link_views()
76 pl.show()
77
78 # %%
79 # Cool Volume Examples
80 # ++++++++++++++++++++
81 #
82 # Here are a few more cool volume rendering examples.
83
84
85 # %%
86 # Head Dataset
87 # """"""""""""
88
89 head = examples.download_head()
90
91 pl = pv.Plotter()
92 pl.add_volume(head, cmap='cool', opacity='sigmoid_6', show_scalar_bar=False)
93 pl.camera_position = pv.CameraPosition(
94 position=(-228.0, -418.0, -158.0),
95 focal_point=(94.0, 122.0, 82.0),
96 viewup=(-0.2, -0.3, 0.9),
97 )
98 pl.camera.zoom(1.5)
99 pl.show()
100
101
102 # %%
103 # Bolt-Nut MultiBlock Dataset
104 # """""""""""""""""""""""""""
105 # .. note::
106 # See how we set interpolation to ``'linear'`` here to smooth out scalars of
107 # each individual cell to make a more appealing plot. Two actor are returned
108 # by ``add_volume`` because ``bolt_nut`` is a :class:`pyvista.MultiBlock`
109 # dataset.
110
111 bolt_nut = examples.download_bolt_nut()
112
113 pl = pv.Plotter()
114 actors = pl.add_volume(
115 bolt_nut, cmap='coolwarm', opacity='sigmoid_5', show_scalar_bar=False
116 )
117 actors[0].prop.interpolation_type = 'linear'
118 actors[1].prop.interpolation_type = 'linear'
119 pl.camera_position = pv.CameraPosition(
120 position=(127.4, -68.3, 88.2),
121 focal_point=(30.3, 54.3, 26.0),
122 viewup=(-0.25, 0.28, 0.93),
123 )
124 cpos = pl.show(return_cpos=True)
125
126
127 # %%
128 # Frog Dataset
129 # """"""""""""
130
131 frog = examples.download_frog()
132
133 pl = pv.Plotter()
134 pl.add_volume(frog, cmap='viridis', opacity='sigmoid_6', show_scalar_bar=False)
135 pl.camera_position = pv.CameraPosition(
136 position=(929.0, 1067.0, -278.9),
137 focal_point=(249.5, 234.5, 101.25),
138 viewup=(-0.2048, -0.2632, -0.9427),
139 )
140 pl.camera.zoom(1.5)
141 pl.show()
142
143
144 # %%
145 # Extracting a VOI
146 # ++++++++++++++++
147 #
148 # Use the :func:`pyvista.ImageDataFilters.extract_subset` filter to extract
149 # a volume of interest/subset volume to volume render. This is ideal when
150 # dealing with particularly large volumes and you want to volume render only
151 # a specific region.
152
153 # Load a particularly large volume
154 large_vol = examples.download_damavand_volcano()
155 large_vol
156
157 # %%
158 opacity = [0, 0.75, 0, 0.75, 1.0]
159 clim = [0, 100]
160
161 pl = pv.Plotter()
162 pl.add_volume(
163 large_vol,
164 cmap='magma',
165 clim=clim,
166 opacity=opacity,
167 opacity_unit_distance=6000,
168 )
169 pl.show()
170
171
172 # %%
173 # Woah, that's a big volume. We probably don't want to volume render the
174 # whole thing. So let's extract a region of interest under the volcano.
175 #
176 # The region we will extract will be between nodes 175 and 200 on the x-axis,
177 # between nodes 105 and 132 on the y-axis, and between nodes 98 and 170 on
178 # the z-axis.
179
180 voi = large_vol.extract_subset([175, 200, 105, 132, 98, 170])
181
182 pl = pv.Plotter()
183 pl.add_mesh(large_vol.outline(), color='k')
184 pl.add_mesh(voi, cmap='magma')
185 pl.show()
186
187 # %%
188 # Ah, much better. Let's now volume render that region of interest.
189
190 pl = pv.Plotter()
191 pl.add_volume(voi, cmap='magma', clim=clim, opacity=opacity, opacity_unit_distance=2000)
192 pl.camera_position = pv.CameraPosition(
193 position=(531554.5542909054, 3944331.800171338, 26563.04809259223),
194 focal_point=(599088.1433822059, 3982089.287834022, -11965.14728669936),
195 viewup=(0.3738545892415734, 0.244312810377319, 0.8947312427698892),
196 )
197 pl.show()
198
199
200 # %%
201 # .. _volume_with_mask_example:
202 #
203 # Volume With Segmentation Mask
204 # +++++++++++++++++++++++++++++
205 # Visualize a medical image with a corresponding binary segmentation mask.
206 #
207 # For this example, we use :func:`~pyvista.examples.downloads.download_whole_body_ct_male`
208 # though :func:`~pyvista.examples.downloads.download_whole_body_ct_female`, or any
209 # other dataset with a corresponding label or mask may be used.
210
211 # %%
212 # Load the dataset and get the ct image and a mask image. Here, a mask of the heart is
213 # used.
214 dataset = examples.download_whole_body_ct_male()
215 ct_image = dataset['ct']
216 heart_mask = dataset['segmentations']['heart']
217
218 # %%
219 # Use the segmentation mask to isolate the heart in the CT image.
220 #
221 # Initialize a new array and image with CT background values. Here, we set the scalar
222 # values to ``-1000`` which typically corresponds to air (low density).
223
224 heart_array = np.full_like(ct_image.active_scalars, -1000)
225
226 # %%
227 # Extract the intensities for the heart segment. We use heart mask's array
228 # to mask the CT image to only extract the intensities of interest.
229 ct_image_array = ct_image.active_scalars
230 heart_mask_array = heart_mask.active_scalars
231 heart_array[heart_mask_array == True] = ct_image_array[heart_mask_array == True] # noqa: E712
232
233 # %%
234 # Add the masked array to the CT image as a new set of scalar values.
235 ct_image['heart'] = heart_array
236
237 # %%
238 # Create the plot.
239 #
240 # For the CT image, the opacity is set to a sigmoid function to show the
241 # subject's skeleton. Since different images have different intensity
242 # distributions, you may need to experiment with different sigmoid functions.
243 # See :func:`~pyvista.Plotter.add_volume` for details.
244
245 pl = pv.Plotter()
246
247 # Add the CT image.
248 pl.add_volume(
249 ct_image,
250 scalars='NIFTI',
251 cmap='bone',
252 opacity='sigmoid_15',
253 show_scalar_bar=False,
254 )
255
256 # Add masked CT image of the heart and use a contrasting color map.
257 _ = pl.add_volume(
258 ct_image,
259 scalars='heart',
260 cmap='gist_heat',
261 opacity='linear',
262 opacity_unit_distance=np.mean(ct_image.spacing),
263 )
264
265 # Orient the camera to provide a latero-anterior view.
266 pl.view_yz()
267 pl.camera.azimuth = 70
268 pl.camera.up = (0, 0, 1)
269 pl.camera.zoom(1.5)
270 pl.show()
271
272 # %%
273 # .. tags:: medical, plot