Typefully

How to Create Any Image Using Only Sine Functions | 2D Fourier Transform in Python

Avatar

Share

 • 

4 years ago

 • 

View on X

What's an image made of? There are many correct answers. But the most fascinating one is: << sines & cosines >> Read on if you're intrigued👇🧵🪡 #python #images #fourier
*Any* image can be reconstructed from a series of sinusoidal gratings. A sinusoidal grating looks like this… #sinusoidal #grating
It’s called a sinusoidal grating because the grayscale values vary according to the sine function. If you plot the values along a horizontal line of the grating, you’ll get a plot of a sine function
Sinusoidal gratings can have different orientations…
…different amplitudes…
…and different frequencies—these are spatial frequencies, not temporal ones
There's one more parameter that defines a sinusoidal grating: the phase. Gratings with a different phase are shifted with respect to each other…
You can create a 2D sinusoidal grating in #Python using #NumPy and display it using #Matplotlib @numpy_team @matplotlib
Or even better, you can use a function of both x and y to make any grating
You can find the parameters of a sinusoidal grating by using the 2D #FourierTransform. The dots shown contain the amplitude and phase of the grating. Their position from the centre gives the frequency, and their orientation represents the orientation of the grating.#
Now, if you have lots of gratings superimposed on each other, the #FourierTransform gives you a pair of dots for each of the components
Now, here's the "magical" part of #Fourier theory. *Any* image is made up of lots of sinusoidal gratings. So, the 2D Fourier Transform of an image gives you thousands of pairs of dots, and each pair represent a sinusoidal grating.
And therefore, you can reconstruct the image by adding all of those sinusoidal gratings together. The more gratings you add, the closer the result is to the actual image
Here's another example
and another one [these are traditional #MalteseBalconies, which is where I'm originally from—#Malta, that is, not the balconies!]
There's a lot more than can fit in a Twitter thread. If you want to read more detail, and go through the step-by-step writing of the code to decompose & recostruct *any* image, read full article here: thepythoncodingbook.com/2021/08/30/2d-fourier-transform-in-python-and-fourier-synthesis-of-images/ #coding #2dfourierimages #2dfouriertransform #fourier
Avatar

Stephen Gruppetta

@s_gruppetta_ct

Constantly looking for innovative ways to talk and write about Python • Mentoring learners • Writing about Python • Writing about technical writing