<jupyter_start><jupyter_text>Fase e magnitude da transformada de Fourier<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft2, ifft2, fftshift

img = plt.imread('cameraman.tiff')

num_rows, num_cols = img.shape
middle_row = int(num_rows/2)
Fimg = fft2(img)
Fimg = fftshift(Fimg)

Fimg_real = np.real(Fimg)
Fimg_imag = np.imag(Fimg)

plt.figure(figsize=[15, 10])
plt.subplot(2, 3, 1)
plt.imshow(img, 'gray')
plt.title('Imagem original')
plt.subplot(2, 3, 2)
plt.imshow(Fimg_real, 'gray')
plt.title('Parte real da transformada de Fourier')
plt.subplot(2, 3, 3)
plt.imshow(Fimg_imag, 'gray')
plt.title('Parte imaginária da transformada de Fourier')
plt.subplot(2, 3, 4)
plt.plot(img[128])
plt.subplot(2, 3, 5)
plt.plot(Fimg_real[middle_row])
plt.subplot(2, 3, 6)
plt.plot(Fimg_imag[middle_row])
# Cálculo do valor absoluto da cada valor complexo contido
# na transformada de Fourier da imagem. Equivalente ao 
# comando img_abs = np.abs(Fimg)
img_abs = np.sqrt(Fimg_real**2 + Fimg_imag**2)

# Cáculo da fase (ângulo) de cada valor complexo contido
# na transformada de Fourier da imagem. Equivalente
# ao comando img_fase = np.angle(Fimg)
img_fase = np.arctan2(Fimg_imag, Fimg_real)

plt.figure(figsize=[10, 10])
plt.subplot(2, 2, 1)
plt.imshow(img_abs, 'gray')
plt.title('Valores absolutos da transformada de Fourier')
plt.subplot(2, 2, 2)
plt.imshow(img_fase, 'gray')
plt.title('Fase da transformada de Fourier')
plt.subplot(2, 2, 3)
plt.plot(img_abs[middle_row])
plt.subplot(2, 2, 4)
plt.plot(img_fase[middle_row])<jupyter_output><empty_output><jupyter_text>Podemos tentar recuperar a imagem utilizando apenas os valores absolutos da transformadaSpoiler: não dá certo<jupyter_code>Fimg_shift = fftshift(Fimg)
img_abs_shift = fftshift(img_abs)
img_rec = np.real(ifft2(Fimg_shift))
img_abs_rec = np.real(ifft2(img_abs_shift))

plt.figure(figsize=[10, 10])
plt.subplot(1, 2, 1)
plt.imshow(img_rec, 'gray')
plt.title('Transformada inversa do espectro')
plt.subplot(1, 2, 2)
plt.imshow(img_abs_rec, 'gray')
plt.title('Transformada inversa do valor absoluto')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13292910447761194, 'fraction_numerical': 0.027985074626865673, 'mean_word_length': 6.688172043010753, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '105202', 'license': 'MIT', 'n_tokens_mistral': 959, 'n_tokens_neox': 886, 'n_words': 199, 'path': 'Lecture 03 - Filtering in the frequency domain/Fase e magnitude da transformada de Fourier.ipynb', 'repo_name': 'chcomin/disciplina-visao-computacional-01-2019', 'size': '2144'}