Solve a Maze from a Photo
Photograph a maze and watch the way through drawn onto it.
Loading…
About the Solve a Maze from a Photo converter
Take a photograph of a maze — a puzzle book, a newspaper, the back of a cereal packet — and the way through appears drawn on top of it.
It is not useful in the slightest, and it is a genuinely satisfying thing to watch happen.
The searching is the easy half
Once the picture is black and white, the rest is a solved problem: every light pixel is somewhere you can stand, every dark pixel is a wall, and finding the shortest route between two of them is textbook computer science from the 1950s.
The route deliberately prefers the middle of a corridor rather than taking the strictly shortest line. A true shortest path scrapes along whichever wall it happens to meet, which draws the answer on top of the ink where you can least see it.
The hard half is that a photograph is not a drawing
A photograph of a page has a lighting gradient across it — one corner brighter, one in shadow, the paper grey rather than white. A single black-and-white cut-off cannot cope: pick a value that suits the bright corner and the shadowed corner turns solid black, hiding the maze entirely.
So the lighting is measured and divided out before anything else happens. What gets measured is how bright the paper itself is in each part of the picture — paper being the brightest thing on a page, since ink is only ever darker than what it sits on. Every pixel is then read as the fraction of its own local paper it reflects, which is a property of the ink alone and comes out the same in the bright corner and the shadowed one.
One stray pixel is a doorway
The other thing photographs bring is speckle: sensor noise, paper texture, compression artefacts. A single wrongly-lit pixel in the middle of a wall is, as far as a solver is concerned, a perfectly good door — and it will find that door and produce a route that strolls straight through solid ink.
A majority vote over each pixel's neighbours closes those pinholes without eroding the walls themselves. If a maze still comes out wrong, turning that tidying up is usually the fix.
When it does not work
There is a switch to show exactly what the solver is looking at — the black and white grid rather than your photo. That is the quickest way to understand a failure: if the walls have gaps in them, or the whole page has gone one colour, you can see it immediately and adjust.
Faint pencil needs the ink sensitivity turned down. A busy or shadowed photo needs it turned up. A maze printed light on a dark background needs the switch for that.
Start and finish
Both ends are guessed from the gaps in the border of the maze when the photo loads, which handles most printed mazes with no clicking at all.
Otherwise click the picture to place them. A click that lands on a wall snaps to the nearest open space, because nobody clicks precisely into a corridor.
Frequently asked questions
Does it work on a photo, or does it need a scan?
A photo is fine, and is what it was built for. Uneven lighting is handled by judging each pixel against its own surroundings rather than against a fixed threshold. Hold the camera reasonably square on and make sure the whole maze is in frame.
It went through a wall.
A speck of noise has opened a pinhole in that wall. Turn the tidying up a step or two, which closes lone pixels without eating the walls themselves.
It says there is no way through.
Either the two ends genuinely are not connected, or a corridor has been closed by the thresholding. Switch on "show what the solver sees" to look at the grid — the problem is almost always obvious once you can see it.
Can I choose where it starts and ends?
Yes. Click the picture to place each end. They are guessed from the gaps in the maze border to begin with, so often you will not need to.
Does it work on a maze with loops or several routes?
Yes. It finds a shortest route rather than following a wall, so loops, dead ends and multiple solutions are all handled.
Is my photo uploaded?
No. The picture is read into a canvas in your own browser, everything happens on your device, and the answer image is produced there too.