Debian Codegraph @

examples/01-filter/image_fft.py

raw
1 """
2 .. _image_fft_example:
3
4 Fast Fourier Transform
5 ~~~~~~~~~~~~~~~~~~~~~~
6
7 This example shows how to apply a Fast Fourier Transform (FFT) to a
8 :class:`pyvista.ImageData` using :func:`pyvista.ImageDataFilters.fft`
9 filter.
10
11 Here, we demonstrate FFT usage by denoising an image, effectively removing any
12 "high frequency" content by performing a `low pass filter
13 <https://en.wikipedia.org/wiki/Low-pass_filter>`_.
14
15 This example was inspired by `Image denoising by FFT
16 <https://scipy-lectures.org/intro/scipy/auto_examples/solutions/plot_fft_image_denoise.html>`_.
17
18 """
19
20 import numpy as np
21 import pyvista as pv
22 from pyvista import examples
23
24 # sphinx_gallery_start_ignore
25 # all but first and last image are black in interactive mode
26 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
27 # sphinx_gallery_end_ignore
28
29
30 # %%
31 # Load the example Moon landing image and plot it.
32
33 image = examples.download_moonlanding_image()
34 print(image.point_data)
35
36 # Create a theme that we can reuse when plotting the image
37 grey_theme = pv.themes.DocumentTheme()
38 grey_theme.cmap = 'gray'
39 grey_theme.show_scalar_bar = False
40 grey_theme.axes.show = False
41 image.plot(theme=grey_theme, cpos='xy', text='Unprocessed Moon Landing Image')
42
43
44 # %%
45 # Apply FFT to the image
46 # ~~~~~~~~~~~~~~~~~~~~~~
47 # FFT will be applied to the active scalars, ``'PNGImage'``, the default
48 # scalars name when loading a PNG image.
49 #
50 # The output from the filter is a complex array stored by the same name unless
51 # specified using ``output_scalars_name``.
52
53 fft_image = image.fft()
54 fft_image.point_data
55
56
57 # %%
58 # Plot the FFT of the image
59 # ~~~~~~~~~~~~~~~~~~~~~~~~~
60 # Plot the absolute value of the FFT of the image. This is a visualization of
61 # the **frequency spectrum**, not a spatial image — each pixel represents the
62 # amplitude of a frequency component, not a location in the moon landing photo.
63 #
64 # Note that we are effectively viewing the "frequency" of the data in this
65 # image, where the four corners contain the low frequency content of the image,
66 # and the middle is the high frequency content of the image.
67
68 fft_image.plot(
69 scalars=np.abs(fft_image.point_data['PNGImage']),
70 cpos='xy',
71 theme=grey_theme,
72 log_scale=True,
73 text='Moon Landing Image FFT Spectrum',
74 copy_mesh=True, # don't overwrite scalars when plotting
75 )
76
77
78 # %%
79 # Remove the noise from the ``fft_image``
80 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 # Effectively, we want to remove high frequency (noisy) data from our image.
82 # This is still done in the frequency domain — we are modifying the spectrum,
83 # not the spatial image. First, let's reshape by the size of the image.
84 #
85 # Next, perform a low pass filter by removing the middle 80% of the content of
86 # the image. Note that the high frequency content is in the middle of the array.
87 #
88 # .. note::
89 # It is easier and more efficient to use the existing
90 # :func:`pyvista.ImageDataFilters.low_pass` filter. This section is here
91 # for demonstration purposes.
92
93 ratio_to_keep = 0.10
94
95 # modify the fft_image data
96 width, height, _ = fft_image.dimensions
97 data = fft_image['PNGImage'].reshape(height, width) # note: axes flipped
98 data[int(height * ratio_to_keep) : -int(height * ratio_to_keep)] = 0
99 data[:, int(width * ratio_to_keep) : -int(width * ratio_to_keep)] = 0
100
101 # Set an explicit clim with a positive lower bound; zeroed pixels would
102 # otherwise break ``log_scale`` (``log10(0) = -inf``) and render as black.
103 abs_data = np.abs(data)
104 clim = [abs_data[abs_data > 0].min(), abs_data.max()]
105
106 fft_image.plot(
107 scalars=abs_data,
108 cpos='xy',
109 theme=grey_theme,
110 log_scale=True,
111 clim=clim,
112 text='Filtered FFT Spectrum (High Frequencies Zeroed)',
113 copy_mesh=True, # don't overwrite scalars when plotting
114 )
115
116
117 # %%
118 # Convert to the spatial domain using reverse FFT
119 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120 # Finally, convert the filtered spectrum back to the spatial domain using the
121 # inverse FFT. This is the actual denoised image.
122
123
124 rfft = fft_image.rfft()
125 rfft['PNGImage'] = np.real(rfft['PNGImage'])
126 rfft.plot(
127 cpos='xy',
128 theme=grey_theme,
129 text='Denoised Moon Landing Image (Inverse FFT)',
130 )
131 # %%
132 # .. tags:: filter