Paint a few strokes and let colour clusters separate foreground from background.
Notebook complete. The demo is a TypeScript reimplementation of the same algorithm.

Overview
Seed-based foreground segmentation in the spirit of Lazy Snapping. Red strokes mark foreground and blue strokes mark background; each stroke set is clustered into 64 colours with k-means, and every pixel goes to whichever side's clusters explain its colour better.
Interactive demo
Runs in your browser with the notebook's settings: 64 k-means clusters per side, the same exp(-0.1 x distance) likelihood, and the same foreground rule. Two fixes: seeds are the image colours under each stroke (the notebook clustered the stroke colours themselves, which are pure red and blue), and the uint8 overflow in the output step is gone.
What it does
- K-means colour models (64 clusters) per seed set.
- Per-pixel likelihood as a sum of exp(-0.1 x distance) over centroids.
- Face recognition with a from-scratch KNN in the same assignment (see Face KNN).
Limitations
- No graph-cut smoothing step, so boundaries are pixel-wise.
- In the notebook, seed colours were read from the stroke image rather than the photo, so its segmentation reduced to comparing each pixel with pure red and pure blue. The demo corrects this.