Debian Codegraph @

examples/01-filter/gaussian_smoothing.py

raw
1 """
2 .. _gaussian_smoothing_example:
3
4 Gaussian Smoothing
5 ~~~~~~~~~~~~~~~~~~
6
7 Perform a Gaussian convolution on a uniformly gridded data set.
8
9 :class:`pyvista.ImageData` data sets (a.k.a. images) a can be smoothed by
10 convolving the image data set with a Gaussian for one- to three-dimensional
11 inputs. This is commonly referred to as Gaussian blurring and typically used
12 to reduce noise or decrease the detail of an image dataset.
13
14 See also :func:`pyvista.ImageDataFilters.gaussian_smooth`.
15
16 """
17
18 # sphinx_gallery_thumbnail_number = 2
19 import pyvista as pv
20 from pyvista import examples
21
22 # Load dataset
23 data = examples.download_gourds()
24
25 # Define a good point of view
26 cp = pv.CameraPosition(
27 position=(319.5, 239.5, 1053.7372980874645),
28 focal_point=(319.5, 239.5, 0.0),
29 viewup=(0.0, 1.0, 0.0),
30 )
31
32 # %%
33 # Let's apply the Gaussian smoothing with different values of standard
34 # deviation.
35 pl = pv.Plotter(shape=(2, 2))
36
37 pl.subplot(0, 0)
38 pl.add_text('Original Image', font_size=14)
39 pl.add_mesh(data, rgb=True)
40 pl.camera_position = cp
41
42 pl.subplot(0, 1)
43 pl.add_text('Gaussian smoothing, std=2', font_size=14)
44 pl.add_mesh(data.gaussian_smooth(std_dev=2.0), rgb=True)
45 pl.camera_position = cp
46
47 pl.subplot(1, 0)
48 pl.add_text('Gaussian smoothing, std=4', font_size=14)
49 pl.add_mesh(data.gaussian_smooth(std_dev=4.0), rgb=True)
50 pl.camera_position = cp
51
52 pl.subplot(1, 1)
53 pl.add_text('Gaussian smoothing, std=8', font_size=14)
54 pl.add_mesh(data.gaussian_smooth(std_dev=8.0), rgb=True)
55 pl.camera_position = cp
56
57 pl.show()
58
59 # %%
60 # |
61 #
62 # Volume Rendering
63 # ~~~~~~~~~~~~~~~~
64 # Now let's see an example on a 3D dataset with volume rendering:
65
66 # sphinx_gallery_start_ignore
67 # volume rendering does not work in interactive plots currently
68 PYVISTA_GALLERY_FORCE_STATIC = True
69 # sphinx_gallery_end_ignore
70
71 data = examples.download_brain()
72
73 smoothed_data = data.gaussian_smooth(std_dev=3.0)
74
75
76 dargs = dict(clim=smoothed_data.get_data_range(), opacity=[0, 0, 0, 0.1, 0.3, 0.6, 1])
77
78 n = [100, 150, 200, 245, 255]
79
80 pl = pv.Plotter(shape=(1, 2))
81 pl.subplot(0, 0)
82 pl.add_text('Original Image', font_size=24)
83 # pl.add_mesh(data.contour(n), **dargs)
84 pl.add_volume(data, **dargs)
85 pl.subplot(0, 1)
86 pl.add_text('Gaussian smoothing', font_size=24)
87 # pl.add_mesh(smoothed_data.contour(n), **dargs)
88 pl.add_volume(smoothed_data, **dargs)
89 pl.link_views()
90 pl.camera_position = pv.CameraPosition(
91 position=(-162.0, 704.8, 65.02),
92 focal_point=(90.0, 108.0, 90.0),
93 viewup=(0.0068, 0.0447, 0.999),
94 )
95 pl.show()
96 # %%
97 # .. tags:: filter