<jupyter_start><jupyter_text>Fourier Transformation of Images<jupyter_code>import numpy as np 
import pandas as pd 
import tensorflow as tf 
from matplotlib import pyplot as plt
import cv2<jupyter_output><empty_output><jupyter_text>FFT is a quick DFT Algorithm 1. [Wikipedia link](https://en.wikipedia.org/wiki/Fast_Fourier_transform)2. [Numpy Implementation](https://numpy.org/doc/stable/reference/generated/numpy.fft.fft.html)3. [OpenCv](https://opencv24-python-tutorials.readthedocs.io/en/stable/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.htmladditional-resources)<jupyter_code># Read Image 
img = cv2.imread('Images/Image1.jpg',0)

# Show Image 
plt.imshow(img,cmap = 'gray')
print("Shape of Image",img.shape)
# Convert it into Fourier Transform
fourier_image = np.fft.fft2(img)

# DC should be shifted to Center 
fourier_image_shifted = np.fft.fftshift(fourier_image)

# Get Magnitude Spectrum to Plot the image 
magnitude_spectrum = 20*np.log(np.abs(fourier_image_shifted))
plt.imshow(magnitude_spectrum, cmap = 'gray')
# Magnitude spectrum is used only for plotting , using fourier_image_shifted as the input for the inverse fourier transform

inverse_image_shifted = np.fft.ifftshift(fourier_image_shifted)

# Inverse Fourier Transform using np.ifft2
inverse_image = np.fft.ifft2(inverse_image_shifted)

# Get Spatial Image
spatial_image = (np.abs(inverse_image))

plt.imshow(spatial_image,cmap = 'gray')
def fouriertransform(image):
    # Convert it into Fourier Transform
    fourier_image = np.fft.fft2(image)
    
    # DC should be shifted to Center 
    fourier_image = np.fft.fftshift(fourier_image)
    
    return fourier_image<jupyter_output><empty_output><jupyter_text>Lets Convert our fourier transform steps to function<jupyter_code>def inverse_fouriertransform(fourier_image):
    
    # DC should be shifted to Center 
    fourier_image = np.fft.ifftshift(fourier_image)
    
    # Convert it back into Spatial Image
    spatial_image = np.fft.ifft2(fourier_image)

    return spatial_image<jupyter_output><empty_output><jupyter_text>Examples of input transforms from Paper ![image.png](attachment:image.png)<jupyter_code>img = cv2.imread('Images/Paper1.png',0)
print(img.shape)
plt.imshow(img,cmap='gray')
fourier_image = fouriertransform(img)
magnitude_spectrum = 20*np.log(np.abs(fourier_image))
plt.imshow(magnitude_spectrum, cmap = 'gray')
fourier_image= inverse_fouriertransform(fourier_image)
magnitude_spectrum = np.abs(fourier_image)
plt.imshow(magnitude_spectrum, cmap = 'gray')
def tffouriertransform(image):
    # Convert it into Fourier Transform
    fourier_image = tf.signal.fft2d(image)

    # DC should be shifted to Center 
    fourier_image = tf.signal.fftshift(fourier_image)
    
    return fourier_image

def tfinverse_fouriertransform(fourier_image):
    
    # DC should be shifted to Center 
    fourier_image = tf.signal.ifftshift(fourier_image)
    
    # Convert it back into Spatial Image
    spatial_image = tf.signal.ifft2d(fourier_image)
    return spatial_image
%%timeit 
# img = cv2.imread('Images/Paper1.png',0)
# plt.subplot(131),plt.imshow(img,cmap='gray')

fourier_image = tffouriertransform(img)
magnitude_spectrum = 20*np.log(np.abs(fourier_image))
# # plt.subplot(132),plt.imshow(magnitude_spectrum, cmap = 'gray')

fourier_image= tfinverse_fouriertransform(fourier_image)
magnitude_spectrum = np.abs(fourier_image)
# plt.subplot(133),plt.imshow(magnitude_spectrum, cmap = 'gray')
%%timeit 
# img = cv2.imread('Images/Paper1.png',0)
# plt.subplot(131),plt.imshow(img,cmap='gray')

fourier_image = fouriertransform(img)
magnitude_spectrum = 20*np.log(np.abs(fourier_image))
# plt.subplot(132),plt.imshow(magnitude_spectrum, cmap = 'gray')

fourier_image= inverse_fouriertransform(fourier_image)
magnitude_spectrum = np.abs(fourier_image)
# plt.subplot(133),plt.imshow(magnitude_spectrum, cmap = 'gray')
img = cv2.imread('Images/Paper1.png',0)
plt.subplot(131),plt.imshow(img,cmap='gray')

fourier_image = tffouriertransform(img)
magnitude_spectrum = 20*np.log(np.abs(fourier_image))
plt.subplot(132),plt.imshow(magnitude_spectrum, cmap = 'gray')

fourier_image= tfinverse_fouriertransform(fourier_image)
magnitude_spectrum = np.abs(fourier_image)
plt.subplot(133),plt.imshow(magnitude_spectrum, cmap = 'gray')
# RBG Image 
img = cv2.imread('Images/Image1.jpg')

# RGB Image has 3 channels 
print(img.shape)<jupyter_output>(900, 900, 3)<jupyter_text>The Fourier Transform does not work on 3 Channel image as we can see below<jupyter_code>plt.subplot(131),plt.imshow(img)

fourier_image = tffouriertransform(img)
magnitude_spectrum = 20*np.log(np.abs(fourier_image))
plt.subplot(132),plt.imshow(magnitude_spectrum)

fourier_image= tfinverse_fouriertransform(fourier_image)
magnitude_spectrum = np.abs(fourier_image)
plt.subplot(133),plt.imshow(magnitude_spectrum)

# Make Channels into 3 different images 

new_img = tf.transpose(img,perm=[2,0,1])
fourier_image = tffouriertransform(tf.cast(new_img, tf.complex64))
magnitude_spectrum = 20*np.log(np.abs(fourier_image))


fourier_image= tfinverse_fouriertransform(fourier_image)
fourier_image = tf.math.real(tf.transpose(fourier_image,perm=[1,2,0]))

# We need to normalize the channels data 
c_max = tf.reduce_max(fourier_image, axis=(0, 1))
c_min = tf.reduce_min(fourier_image, axis=(0, 1))
im_out = tf.divide(fourier_image - c_min,c_max - c_min)
        
plt.subplot(122),plt.imshow(im_out)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11243144424131626, 'fraction_numerical': 0.019195612431444242, 'mean_word_length': 6.463847203274216, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 3, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '411803', 'license': 'MIT', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2003, 'n_words': 338, 'path': 'playground/Fourier_Notebook.ipynb', 'repo_name': 'aye21874/Spectral-Representations-for-Convolutional-Neural-Networks', 'size': '5470'}