← All Base Form Math projects
BM-03NOT STARTEDBASE FORM · LINEAR ALGEBRAMATHPYTHON

Photo transformer

Rotate, scale, shear and mirror your own photos with 3×3 matrices, chain edits, and undo any of them with the inverse.

EST.6 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
output image vs reference
GOAL · PSNR ≥ 40 dB on 10 edits
scipy.ndimage.affine_transform—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

You will build a photo editor that knows only one trick: multiply by a matrix. Rotate, zoom, shear, mirror and shift all become 3×3 matrices, and a chain of edits is just their product. Undo is the inverse matrix.

The trick for shifting is homogeneous coordinates. A pixel position (x,y)(x, y) becomes (x,y,1)(x, y, 1), and an edit is

[x′y′1]=[abtxcdty001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

The top-left 2×22 \times 2 block rotates, scales, shears or mirrors. The last column shifts.

The new idea is the determinant. For the block above it is det⁡=ad−bc\det = ad - bc, and it tells you how much the edit scales area. A determinant of 2 doubles area. A negative determinant means the picture is mirrored. A determinant of 0 squashes the photo onto a line, and no inverse can bring it back. When the determinant is not zero, the inverse M−1M^{-1} undoes the edit, and (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1} undoes a chain in reverse order.

One practical surprise: you warp a photo backwards. Pushing each input pixel forward leaves holes. Instead, loop over output pixels, apply M−1M^{-1} to ask "where did this one come from?", and blend the nearest input pixels.

VISUALFig. 1
[1 0; 0 1] startarea 1 [1 0.6; 0 1] shear, det = 1area 1 [2 0; 0 1] stretch, det = 2area 2 [−1 0; 0 1] mirror, det = −1area 1, flipped where (1, 0) goes where (0, 1) goes
The unit square under three edits. The determinant is the new area; its sign says whether the picture flipped. Why it's here: Objective 4 asks you to read area change and mirroring from one number. This shows what that number means.
HINT · OBJECTIVE 3
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · your photos

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. Every one of the 10 edits scores PSNR ≥ 40 dB against scipy.ndimage.affine_transform (order=1), recorded with `python tools/mlarena.py record BM-03 "<lowest PSNR> dB"`.
  2. Your determinant and inverse match NumPy within 1e-9 for every matrix you use.
  3. Undo works on a chain of at least three edits, and the README shows before, after and undone photos side by side.

Side quests

Add a perspective (projective) warp, where the bottom row of the matrix is no longer (0, 0, 1). Note what that does to straight lines and parallel lines.
Replace bilinear with nearest-neighbour sampling and measure how much PSNR drops.

Terms in plain words

Homogeneous coordinates
Write a 2-D point (x, y) as (x, y, 1). Then a 3×3 matrix can rotate, scale and also shift the point with one multiplication.
Determinant
One number from a square matrix that says how much it scales area. Negative means the image is mirrored; zero means the map squashes everything flat.
Inverse matrix
The matrix that undoes another one, so that M⁻¹M is the identity. It exists only when the determinant is not zero.
Bilinear interpolation
When a pixel lands between four source pixels, blend those four, weighted by how close each one is.
PSNR
Peak signal-to-noise ratio, a score in decibels for how close two images are. Higher is closer; 40 dB means the difference is hard to see.