Turning Images into Fourier Sums and Drawing

Overview

Fourier Drawing Example

This Processing (Java) application takes an input image, extracts its contours using OpenCV, and reconstructs the outline using rotating circles (epicycles) driven by the Discrete Fourier Transform (DFT).

The pipeline converts the image to grayscale, applies thresholding, and extracts contour points. The x and y coordinates of those points are separated and each fed independently into a DFT, producing a set of frequency coefficients that are interpreted as rotating circles with specific amplitudes, frequencies, and phases.

Two chains of epicycles — one for x and one for y — rotate simultaneously. The tip of each chain traces the respective coordinate, and their intersection reconstructs the original contour as time sweeps from 00 to 2π2\pi.

How It Works

Contour Extraction

The input image is loaded and processed through an OpenCV pipeline: conversion to grayscale, binary thresholding, and contour detection via findContours. The largest contour is selected and its points are sampled at regular intervals to produce an ordered list of (xn,yn)(x_n, y_n) coordinate pairs.

These sampled points define the discrete signal that will be decomposed by the DFT. Separating x and y into independent arrays allows each dimension to be transformed and drawn with its own set of epicycles.

Discrete Fourier Transform

The DFT is applied independently to the x-coordinate array and the y-coordinate array. For each frequency index kk, the transform computes real and imaginary components by correlating the signal with sine and cosine basis functions:

re=1Nn=0N1xncos ⁣(2πknN)\text{re} = \frac{1}{N}\sum_{n=0}^{N-1} x_n \cos\!\left(\frac{2\pi k n}{N}\right)
im=1Nn=0N1xnsin ⁣(2πknN)\text{im} = -\frac{1}{N}\sum_{n=0}^{N-1} x_n \sin\!\left(\frac{2\pi k n}{N}\right)

Each coefficient is converted to polar form — amplitude Ak=re2+im2A_k = \sqrt{\text{re}^2 + \text{im}^2}, phase φk=atan2(im,re)\varphi_k = \text{atan2}(\text{im}, \text{re}), and frequency kk — defining a rotating circle. The coefficients are sorted by descending amplitude so the largest circles are drawn first, giving rapid visual convergence.

Epicycle Drawing

Two independent chains of epicycles are rendered on screen — one arranged horizontally (producing the x-coordinate) and one vertically (producing the y-coordinate). Each chain stacks rotating circles end-to-end: the centre of circle k+1k+1 sits on the rim of circle kk.

As time tt advances from 00 to 2π2\pi, the tip of the x-chain and the tip of the y-chain each trace a coordinate value. A horizontal line from the y-chain tip and a vertical line from the x-chain tip intersect at the drawn point, progressively reconstructing the full contour on the canvas.

Additional Examples

Smooth curved shapes such as this are easier to draw because OpenCV can extract a continuous curve that encompass the entire drawing at once. If multiple curves are present, then there would be discontinuities, the algorithm might fail to draw correctly.

Extending to Text

Text-to-Image Pre-processing

To support drawing text, the application includes an automated pre-processing step. Instead of requiring an external image file, it uses Processing's createGraphics API to render any arbitrary text string offscreen using a custom font (such as Parisienne) at high resolution (default 350px).

The generated image is saved as a temporary PNG file, which is then fed directly into the OpenCV contour detection and sampling pipeline. This creates an extremely flexible system where any text string can be converted into a Fourier drawing sequence on the fly.

Text Epicycle Animation

The drawing stage uses the same dual-epicycle system as the image version, stacked and rotated to trace out the coordinates. However, to distinguish the text version and provide better visibility, the path is rendered with a distinctive yellow stroke color (RGB 255, 248, 21) instead of the cyan used in the image version.

The epicycles trace each character sequentially, resolving the discrete Fourier components of the letters end-to-end to reconstruct the written word.

Text-to-Image Drawing Pipeline

Text String
Font Rendering
PNG Image
OpenCV Contours
Point Sampling
DFT(x), DFT(y)
Epicycles
Drawn Text

This entire pipeline is fully automated: once a string and font are specified, the program renders the text, processes the image, runs the DFT, and animates the resulting epicycle drawing without any manual step.

The Mathematics

DFT Formulation

The Discrete Fourier Transform converts a finite sequence of NN equally-spaced samples into a same-length sequence of complex frequency coefficients:

Xk=1Nn=0N1xnei2πkn/NX_k = \frac{1}{N}\sum_{n=0}^{N-1} x_n \cdot e^{-i\,2\pi k n / N}

Each coefficient XkX_k encodes a circle with amplitude AkA_k, frequency kk, and phase φk\varphi_k. The original signal is reconstructed by summing these rotating phasors:

x(t)=kAkcos ⁣(kt+φk)x(t) = \sum_{k} A_k \cos\!\left(k \cdot t + \varphi_k\right)

Sorting the coefficients by descending amplitude means the first few circles capture the coarse shape while later, smaller circles add finer detail — analogous to how low-frequency Fourier components carry most of the signal energy.

Technology Stack

Tools & Libraries

  • Processing (Java) — Creative-coding framework used for real-time rendering and animation
  • OpenCV (gab.opencv) — Image processing: grayscale conversion, thresholding, contour extraction
  • PeasyCam — 3D camera control for interactive scene navigation
  • Custom Fonts — TTF font rendering for text generation
  • Discrete Fourier Transform — Custom implementation converting contour coordinates into frequency coefficients
  • Real-time Animation — Epicycle chains drawn each frame, progressively tracing the reconstructed contour

Source Code

The fourier drawing system is split into two implementations: one extracting contours from input source images via OpenCV, and another that generates and draws text outline contours directly.

Fourier Drawing from Images

A Processing-based visualization tool that extracts contours from images using computer vision and recreates them dynamically using Fourier Epicycles (orbiting circles). By treating the extracted 2D contour coordinates as periodic signals, the system applies a Discrete Fourier Transform (DFT) to map the geometry into a sum of rotating vectors (phasors).

Dependencies / Tech Stack

Processing (Java)OpenCV for ProcessingPeasyCamJOGL

Modules & Components

Main SketchManages canvas rendering loop, coordinate tracing, and coordinates epicycle chains
DFT ModulePerforms manual O(N²) Discrete Fourier Transform calculations on coordinate lists
Contour ExtractorUses OpenCV to extract edge/boundary vector points sequentially from source images
Circle ClassData structure containing physical parameters (frequency, amplitude, phase) of each phasor

Fourier Drawing from Texts

An extension of the Fourier drawing tool designed to render text strings as boundary coordinates, converting them to Fourier series parameters for dynamic epicycle drawing.

Dependencies / Tech Stack

Processing (Java)PeasyCam

Modules & Components

Text Contour GeneratorConverts input text strings into boundary outline coordinate lists
Phasor OrchestratorTraces character contours via frequency amplitude sorting and epicycle rotation

Nuwantha Kumara

Mechanical Engineering student passionate about software development, simulations, and creating impactful solutions.

Connect

© 2026 Nuwantha Kumara. All rights reserved.

Built withNext.js