-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspectral_denoise.py
70 lines (50 loc) · 1.79 KB
/
spectral_denoise.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "Christian Heider Nielsen"
__doc__ = r"""
Created on 09/04/2020
"""
import numpy
from scipy import fftpack
__all__ = ["fft_im_denoise"]
def fft_im_denoise(img: numpy.ndarray, keep_fraction: float = 0.1) -> numpy.ndarray:
"""
a blur with an FFT
Implements, via FFT, the following convolution:
.. math::
f_1(t) = \int dt'\, K(t-t') f_0(t')
.. math::
\tilde{f}_1(\omega) = \tilde{K}(\omega) \tilde{f}_0(\omega)
# keep_fraction - Define the fraction of coefficients (in each direction) we keep
Compute the 2d FFT of the input image
Filter in FFT
Reconstruct the final image
:param keep_fraction:
:type keep_fraction:
:param img:
:type img:
:return:
:rtype:"""
assert 0.0 < keep_fraction < 1.0
im_fft = fftpack.fft2(img)
# In the lines following, we'll make a copy of the original spectrum and
# truncate coefficients.
# Call ff a copy of the original transform. Numpy arrays have a copy
# method for this purpose.
im_fft_cp = im_fft # .copy()
num_row, num_columns = im_fft_cp.shape
# Set to zero all rows with indices between r*keep_fraction and
# r*(1-keep_fraction):
im_fft_cp[int(num_row * keep_fraction) : int(num_row * (1 - keep_fraction))] = 0
im_fft_cp[
:, int(num_columns * keep_fraction) : int(num_columns * (1 - keep_fraction))
] = 0
# pyplot.figure()
# plot_spectrum(im_fft)
# pyplot.title('Fourier transform')
# pyplot.figure()
# plot_spectrum(im_fft_cp)
# pyplot.title('Filtered Spectrum')
# Reconstruct the denoised image from the filtered spectrum, keep only the
# real part for display.
return fftpack.ifft2(im_fft_cp).real # Inverse / Reconstruction