Fourier Transformation in Image Processing
How our final application would look like —
The Fourier Transform is an important image processing tool which is used to decompose an image into its sine and cosine components. The output of the transformation represents the image in the Fourier or frequency domain, while the input image is the spatial domain equivalent. In the Fourier domain image, each point represents a particular frequency contained in the spatial domain image.
The Fourier Transform is used in a wide range of applications, such as image analysis, image filtering, image reconstruction and image compression. In this blogpost, we will be creating an application with GUI, through which we would be able to make changes to the given image by making changes to its frequency domain. The final result can be viewed from here.
We will go through the required functions that would be used in the streamlit applications.
The function given below, takes input an image, and returns two list, fft_images and fft_images_log. fft_images contains the list of fourier transformation of each channel in the image. But, it contains complex numbers which can’t be used for visualising the result. For that, we also have fft_images_log, which contains the logarithm of abs values of fft_images, and can be used for visualisation.
Once we have the fft_images, and fft_images_log. We proceed to get input from streamlit writing pad, which will act as the filter that is applied on top of the frequency representation of the image. This will be implemented by the functions available inside steramlit.
Once we have the mask (filters to be applied, which we get from scratchpad), we need to make the required changes to fft_images. This is done by the function, apply_mask_to_all uses the function apply_mask internally and applied the mask to the fft_images.
Moving on to creating Streamlit Application
Once we have the basic functions required, we move on to create the streamlit application, which provides us a UI through which we can better visualise the results produced by our program.
The next main component is the drawing pad which will unable us to make changes to the frequency domain of the image. We will use st_canvas instance, with some parameters that define the use case that we are looking for here.
Apart from the drawing pad, we have also used buttons, image and text placeholders, which are basic and hence code for them are not included in this blogpost, but you can find the full code on the link given below.
The final code can be accessed from here. The ipynb notebooks are contained in the folder just to showcase how the research went for creating the application, there was a lot of trial and error, and it took almost a week to complete this project end to end.
Now, you can open the streamlit application, and play around with it. Just see what changes in the frequency change brings out which changes in the image.