<jupyter_start><jupyter_text>Realizēt filtrēšanu ar Furjē transformācijas palīdzību<jupyter_code>import numpy as np                          # Numerical Python 
import matplotlib.pyplot as plt             # Python plotting
from PIL import Image                       # Python Imaging Library
from numpy.fft import fft2, fftshift, ifft2 # Python DFT

%matplotlib inline
img = Image.open('./assets/lenna_noise.png').convert('L')
img = np.asarray(img)
plt.figure()
plt.imshow(img, plt.cm.gray)
plt.title('Original image')
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))

fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(15)
fig.add_subplot(1,2,1)
plt.title('Original image')
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap='gray')

fig.add_subplot(1,2,2)
plt.title('Magnitude Spectrum')
plt.xticks([])
plt.yticks([])
plt.imshow(magnitude_spectrum, cmap='gray')

plt.show(block=True)<jupyter_output><empty_output><jupyter_text>By unsetting some pixels in the magnitude spectrum, try to clean the periodic noise on the image.<jupyter_code>f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))

np.fill_diagonal(fshift, 1)

magnitude_spectrum = 20*np.log(np.abs(fshift))

fig = plt.figure()
fig.set_figheight(10)
fig.set_figwidth(10)
plt.title('Magnitude Spectrum')
plt.xticks([])
plt.yticks([])
plt.imshow(magnitude_spectrum, cmap='gray')<jupyter_output><empty_output><jupyter_text>Restore the image from magnitude spectrum<jupyter_code>f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.real(img_back)

fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(15)
fig.add_subplot(1,2,1)
plt.title('Original image')
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap='gray')

fig.add_subplot(1,2,2)
plt.title('Restored image')
plt.xticks([])
plt.yticks([])
plt.imshow(img_back, cmap='gray')

plt.show(block=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15205271160669032, 'fraction_numerical': 0.01824632539280284, 'mean_word_length': 5.783505154639175, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '226463', 'license': 'MIT', 'n_tokens_mistral': 844, 'n_tokens_neox': 787, 'n_words': 132, 'path': 'Fourier-transform.ipynb', 'repo_name': 'emukans/image-processing', 'size': '1973'}