Can an algorithm paint a recognizable portrait using nothing but semi-transparent circles, rectangles, or polygons? In their 2019 paper from the OsloMet AI Lab, "Evolved Art with Transparent, Overlapping, and Geometric Shapes" (arXiv:1904.06110), Joachim Berg, Stefano Nichele, and colleagues explored how evolutionary algorithms can approximate complex target images—like the Mona Lisa or Theodor Kittelsen's Nøkken—using indirect genetic representations.
Inspired by their methodology, I built an interactive JavaScript implementation (Evolutionary Image Approximation) to experiment with how different geometric primitives, mutation schedules, and perceptual fitness metrics behave in real time inside an HTML5 Canvas.
1. Indirect Encoding: Genotype to Phenotype
Rather than evolving a 2D grid of individual pixels, evolutionary art relies on an indirect encoding. As Berg et al. describe, the compressed genetic identity—the genotype—is an ordered collection of shape objects (an "image genome"), where each gene encodes parameters for a single geometric primitive:
-
Colour & Alpha · RGBA channels
RGB values (0–255) paired with an alpha transparency scalar (0.0–1.0) so overlapping shapes blend optically.
-
Spatial Coordinates · Anchor & Bounds
Canvas coordinates (x, y) and primitive-specific geometry: radius for circles, corner bounds for rectangles, or N radial vertices for polygons.
To evaluate a candidate, the algorithm performs a genotype-to-phenotype mapping: rendering the sequence of genes onto a blank 2D canvas buffer and comparing the resulting visual phenotype against the target image.
2. Lessons from the OsloMet Experiments
Berg et al. benchmarked polygons, circles, and lines across 10,000 generations using a (1+1) baseline configuration. Several of their empirical findings directly informed the parameter ranges in my web tool:
| Experiment Variable | Berg et al. (2019) Finding | JS Tool Adaptation |
|---|---|---|
| Polygon Vertices | 8 vertices achieved the highest relative fitness (90.57%), outperforming triangles (3) and high-vertex shapes (15–20). | Polygon mode dynamically samples between [3, 8] vertices per primitive. |
| Shape Primitives | Circles and polygons excelled (~90–92% fitness); lines alone lagged behind (65.50%–83.86%). | Focused on triangles, polygons, circles, and rectangles for dense area coverage. |
| Mutation Intensity | High mutation probabilities (>50%) destabilized convergence; soft local tweaks paired with occasional larger jumps worked best. | Implemented a two-stage loop: broad candidate exploration followed by simulated cooling adjustments. |
3. Optimizing for Real-Time Browser Execution
In Python with OpenCV, evolving a fixed genome of 20–50 shapes over 10,000 generations is computationally manageable offline. In a single-threaded browser environment, however, we want the viewer to watch the painting emerge live at 60fps using 1,000 to 2,500 shapes. To make this responsive, my implementation introduces three key adjustments:
Error-Weighted Spawning: Instead of spawning new shapes purely at random across the canvas, chooseErrorWeightedPoint() samples 14 random pixel coordinates, computes the squared RGB error against the target image at each point, and anchors the new shape where the current approximation is most inaccurate.
Once a promising candidate shape is found, it enters an adjustment phase with simulated cooling. Over 40–50 mutation steps, vertex positions, radii, and RGBA channels are perturbed using Gaussian noise (Box-Muller transform) scaled by a cooling factor:
$$\text{cooling} = \max\left(0.08, 1 - \frac{\text{attempt}}{\text{totalAdjustments}}\right)$$
Early adjustments make bold geometric corrections (analogous to Berg et al.'s medium mutation), while final steps make fine sub-pixel and tint refinements (soft mutation).
4. Pixel Loss vs. Perceptual SSIM
One of the most interesting observations in Berg et al. (Section 2.4) is the gap between mathematical pixel error and human visual perception:
"The fitness score does not necessarily determine the best rendered image for human eyes... Certain defined features of an image can be more important for recognition by humans." — Berg et al. (2019)
Standard pixel-by-pixel difference treats every pixel independently, often smoothing over high-contrast structural edges like eyes or horizons. To test this directly in the browser, I implemented a toggleable fitness function in the tool:
- Pixel Loss (MSE): Fast channel-wise squared error across RGB values. Great for rapid colour blocking.
- SSIM Loss (Structural Similarity Index): Evaluates 8 × 8 pixel windows for luminance mean, variance, and covariance. While heavier to compute, SSIM prioritizes structural edges and local contrast.
See the browser-based stochastic hill-climber evolve images in real time using randomized primitives and MSE/SSIM scoring.
References
Berg, J., Berggren, N. G. A., Borgeteien, S. A., Jahren, C. R. A., Sajid, A., & Nichele, S. (2019). Evolved Art with Transparent, Overlapping, and Geometric Shapes. Oslo Metropolitan University, AI Lab. arXiv:1904.06110v3.