Menu

[r6218]: / trunk / py4science / examples / fft_imdenoise.py  Maximize  Restore  History

Download this file

84 lines (60 with data), 2.2 kB

 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
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
"""Image denoising example using 2-dimensional FFT."""
import sys
import numpy as np
from matplotlib import pyplot as plt
def mag_phase(F):
"""Return magnitude and phase components of spectrum F."""
return (np.absolute(F), np.angle(F))
def plot_spectrum(F, amplify=1000):
"""Normalise, amplify and plot an amplitude spectrum."""
M, Phase = mag_phase(F)
M *= amplify/M.max()
M[M > 1] = 1
print M.shape, M.dtype
plt.imshow(M, plt.cm.Blues)
if __name__ == '__main__':
try:
# Read in original image, convert to floating point for further
# manipulation; imread returns a MxNx4 RGBA image. Since the image is
# grayscale, just extrac the 1st channel
im = plt.imread('data/moonlanding.png').astype(float)[:,:,0]
except:
print "Could not open image."
sys.exit(-1)
# Compute the 2d FFT of the input image
F = np.fft.fft2(im)
# Now, make a copy of the original spectrum and truncate coefficients.
keep_fraction = 0.1
# Call ff a copy of the original transform. Numpy arrays have a copy
# method for this purpose.
ff = F.copy()
# Set r and c to be the number of rows and columns of the array.
r,c = ff.shape
# Set to zero all rows with indices between r*keep_fraction and
# r*(1-keep_fraction):
ff[r*keep_fraction:r*(1-keep_fraction)] = 0
# Similarly with the columns:
ff[:, c*keep_fraction:c*(1-keep_fraction)] = 0
# Reconstruct the denoised image from the filtered spectrum, keep only the
# real part for display
im_new = np.fft.ifft2(ff).real
# Show the results
plt.figure()
plt.subplot(221)
plt.title('Original image')
plt.imshow(im, plt.cm.gray)
plt.subplot(222)
plt.title('Fourier transform')
plot_spectrum(F)
plt.subplot(224)
plt.title('Filtered Spectrum')
plot_spectrum(ff)
plt.subplot(223)
plt.title('Reconstructed Image')
plt.imshow(im_new, plt.cm.gray)
plt.savefig('fft_imdenoise.png', dpi=150)
plt.savefig('fft_imdenoise.eps')
# Adjust the spacing between subplots for readability
plt.subplots_adjust(hspace=0.32)
plt.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.