Fourier Reconstruction of an Image

Published by Arun Isaac on

Tags: math, signals, software

Any image can be reconstructed using just a superposition of plane waves of different spatial frequencies. This is a numerical experiment using a NumPy script that reconstructs an image one frequency component at a time.

Recently, in Fourier optics class, I learnt that any image can be decomposed into a superposition of plane waves of different spatial frequencies. Obviously, I have known for a long time how a 2D Fourier transform works, but still, I just wanted to do the superposition in a numerical experiment and convince myself that it is indeed possible to construct any given image simply from plane waves.

And, Gokul has also been telling me about NumPy and SciPy for a long time. So, I figured that this was a good excuse to learn to use these libraries. It turns out that NumPy and SciPy, just like all things pythonic, are very simple and friendly to use. Unlike in the Matlab/GNU Octave world, I can actually write a for loop without dreading the end of the world. Plus, a more full fledged programming language like Python has a certain versatility that something very specialized like Matlab/GNU Octave cannot offer.

So, here's what the script does. First, an input image file is read and its 2D FFT is computed. To sparsify the spectrum, frequency components with a magnitude lesser than a threshold are completely suppressed. Then, the frequency components of the image are sorted in the descending order of their energy contribution. From this sorted list of frequency components, each component is taken one at a time and added to the reconstructed image. Thus the image slowly acquires finer and finer detail till it is complete. The sorting of the frequency components is done so that the most dominant components appear first, and the less dominant ones later.

In addition to this, the script also takes as an input argument the number of output frames to generate. To generate the required number of frames, the sorted list of frequency components is partitioned into groups in such a way that each partition contributes more or less the same energy.

Once the frames were generated thus, I stitched them together using ffmpeg to create a video. Stitching together several still images to create an animation or video is quite a handy thing to be able to do. You can read more about how to do it using ffmpeg here.

I am releasing the Fourier reconstruction script into the public domain. You can download it from the link below. It has a neat command line interface written with argparse. So you hopefully won't have too much difficulty using it.

Downloads

As you can see, in some kind of irony, I decided to do Fourier reconstruction of Fourier himself! The video showing reconstruction of an image of Fourier component by component is at the top of the page. Similar videos for a simple square and a circle are shown below. Your browser should support HTML5 video and WebM with the VP8 codec.