Lego Town - Part 3: Two $35 webcams that measure depth to the millimeter with stereo vision

Two photos of the new setup: left, the metal bar with two webcams seen from below, held by the arm of a floor stand; right, looking down past the cameras at the green board framed by printed black-and-white cards
The new setup: two webcams on a metal bar, held 1.2 m above the board by a floor stand. Left, from below; right, from above, with the board and its frame of printed cards.

Our stereo vision, depth experiment

One evening, my kiddo and I turned the living-room floor into a science lab.

First, he held a printed checkerboard under the cameras in about 20 spots, so the app could measure exactly how each webcam sees. Then we built ten brick towers, 2 to 6 bricks tall, with a small printed card on top of each. We measured them with a ruler, and he typed the numbers into the laptop. Then we stepped back, and two webcams 1.2 meters above the board measured the towers too.

Our first try was a flop. A one-brick tower came out as two bricks, and a six-brick tower as five. Depth is fussy: the app has to know exactly how the two cameras sit, and one camera turned by a tenth of a degree throws everything off. So I changed the app to check the cameras against the printed cards around the board every time it measures, and we tried again.

This time, every brick count was right. Every height was within 2.1 mm of the truth, 0.9 mm on average, in evening light. The webcams even beat our ruler, which was off by up to 5 mm!

That experiment sums up the biggest change to Lego Town so far: the webcams moved from the sides of the board to above it, and now they see depth.

In case you missed parts 1 and 2

Lego Town is a side project I’m building with my 8-year-old. He builds with Lego-like Duplo bricks on the living-room floor, two ordinary webcams watch the board, and a laptop turns the bricks into a living 3D city. Each brick color is a type of place: red for homes, dark blue for offices, black for factories, and so on. Citizens write letters to their mayor (aka. my kiddo) about real planning problems, and he fixes them by moving bricks.

Part 1 tells how we built it in two weeks. In part 2, the city moved onto the bricks themselves: point an iPad Pro at the board, and buildings, people and cars stand where the bricks are, in augmented reality.

This part is about the eyes behind it all: the two webcams.

The old setup: a camera on each side

Until recently, the board lay in the middle of the floor, with a webcam on a tripod at each long side, facing each other. Each camera stood about 95 cm up and looked down at the board at an angle, so together they saw the tops of the bricks and their sides.

Illustration of the old setup: the 64×32-stud board on the floor, with a webcam on a tripod at each long side, facing each other and looking down at an angle; a hatched patch marks what a tall tower hides from the front camera
The old setup: a webcam at each long side of the 64×32-stud board, looking down at an angle.

It worked well in good light, but it had two big problems.

1. Dim light. From the side, the app works out each stack’s height from how it looks: which pixels are tops, which are sides, and what color they are. Under a warm evening lamp, colors fade and tops and sides blur together. In one experiment we ran, the side cameras read fewer than half of the stacks right (58 of 125).

2. Things in the way. A camera looking across the board is easy to block:

  • By people. Kids move around the board as they build. Every time my kiddo leaned in between a camera and the board, he hid part of the board from that camera.
  • By tall towers. From an angle, a tall tower hides whatever stands behind it. A 10-brick tower in the middle of the board hides a strip about 15 cm long, so a small house tucked behind a skyscraper could vanish from a camera’s view.

A smaller board, and a bike ride to Home Depot

The board got smaller, too. In part 1, four baseplates made a 64×64-stud board, about a meter on each side. But even for his biggest ideas, my kiddo used only a third to a half of it. The size even got in his way: it was hard for him to get around it quickly. And for me, a bigger board made the cameras harder to set up and calibrate.

So we halved it, to 64×32 studs: two baseplates, 102 × 51 cm. I biked to Home Depot for a sheet of thin plywood and had it cut to size there, to make a stiff base under the plates, with room around them for the printed cards.

The new setup: two eyes above the board

The new setup uses the same two webcams, now side by side on a metal bar, about 30 cm apart. A floor stand holds the bar 1.2 meters above the board, with both cameras looking straight down.

Illustration of the new setup: a floor stand holds a bar 1.2 m above the board, with two webcams about 30 cm apart looking straight down; their two views overlap over the whole board
The new setup: both webcams on one bar, 1.2 m up and about 30 cm apart, looking straight down at the whole board.

Because they’re 30 cm apart, the two cameras see the board from slightly different spots, just like your two eyes. Hold up a finger and look at it with one eye, then the other: the finger jumps, while the far wall barely moves. The closer something is, the more it jumps. The top of a tall tower is closer to the cameras than the plate, so it shifts more between the two pictures. Measure that shift at every point, and you get a depth map: the height of everything on the board.

Four panels: the board as the left and right cameras see it; a zoomed-in overlay where the tops of taller towers shift more between the two pictures (1 brick: 7 px, 4 bricks: 25 px, 10 bricks: 68 px); and the depth map with each tower's height
What the two cameras see: the towers shift between the left and right pictures, and the taller the tower, the bigger the shift. The depth map turns that shift into height.

This fixes both problems:

  • Dim light. Heights now come from geometry, not from how the bricks look. The app only has to find the same spot in both pictures, and that still works in evening light. Color still tells it what a brick is (a home or a factory), but no longer how tall a stack is.
  • Things in the way. From above, a tall tower hides only a thin sliver beside it, not a whole street behind it. And my kiddo can move all around the board without getting between the cameras and the bricks. Only hands over the board block the view, and the app waits for them to leave, as before.
Side view, before and now: before, a tall tower hides a small house from the left camera and a child leaning in hides half the board from the right camera; now, both cameras look straight down on every tower and the child is out of their view
Seen from the side. Before: a child leaning in, or a tall tower, could hide part of the board from a camera. Now: both cameras look down on every tower.

Cheap enough for schools and families

Here’s everything the new setup needed in Canadian dollars:

Part Price
Two Logitech C920 webcams, refurbished ($35 each; about $90 each new) $70
A 13-inch metal camera bar $17
Zip ties and silicone tape, to hold the cameras on the bar $20
A tablet floor stand to hold the bar (we already had one) $50
Total about $160

Prices in Canadian dollars. The laptop, bricks and printed cards are the same as before.

No special depth camera, just two everyday webcams set far enough apart. That’s cheap enough for a school, or even a family.

How accurate is it?

We tested two things, both in evening light: how well the cameras measure height, and how well the app reads the bricks.

Height. In our depth experiment, all ten towers got the right brick count, and every height was within 2.1 mm of the truth, 0.9 mm on average. A Duplo brick is 19.2 mm tall, so even the worst error was about a tenth of a brick, on a board about a meter long.

The depth experiment's result window: a blue depth map seen from above, with ten towers circled and numbered, each with its height in mm and its brick count
The depth experiment's result, seen from above: each tower's height and brick count. A yellow ring means the height came from the card on top.
Chart: the height error of each of the ten towers, all between −1.5 and +2.1 mm, far inside the ±9.6 mm band where the brick count comes out right
Height error for each tower, after taking out the extra 4.5 mm.

Bricks. To test brick reading, we used the app’s Teach mode from part 1: a square lights up, you put a brick there, and the app always knows exactly what’s on the board. One evening, I recorded four Teach rounds with the cameras above. I trained the app’s small neural network on three rounds and tested it on the fourth, four times over, so every test was on a round it had never seen (4-fold cross-validation).

Cameras above, evening light Read correctly
Occupied squares (color and height both right) 92% (2,584 of 2,816)
Stacks (color, height and position all right) 92% (644 of 702)

A square is occupied when there's a brick on it according to the ground truth or in the reading. Each of the 76 steps is read starting from the correct board before it, so a square or stack counts at every step it stands.

That’s about as good as the side cameras did in good light (93% of stacks in an October test), and twice what they managed under the warm evening lamp (46%). To be fair, part of that jump comes from camera settings: in that lamp test, the webcams’ exposure was locked and their pictures were very dark, while in November they adjusted their own exposure.

What still goes wrong

Colors cause most of the mistakes.

Bar chart of occupied squares read right by brick color: black 62%, white 84%, red 90%, orange 98%, and dark blue, light blue, pink and yellow 100%
Occupied squares read right, by brick color (cameras above, evening light) under the new setup with stereo vision.

In this evening test, black was the hardest: in the shadows, the app read some black bricks as dark blue, or missed them. White came next: in the evening light, some white bricks looked yellow. In earlier tests, white and pink were the weak spots: white can look just like the glare of a window on the plate, and in evening light, pink turns salmon, close to the color of skin.

Most of the other mistakes are near misses, often off by one. In the test, a 10-brick tower was read one stud away from where it stood. During play, a few buildings with a label card on top sometimes read one floor too tall: the library and the water tower kept flipping between one and two floors.

I expect it to do even better in daylight. Every number here comes from evening light, the hardest case for colors. And the mistakes cluster: across the four rounds there were only seven distinct ones, each repeated for several steps while that brick stood.

Automatic calibration, all the time

Depth from two cameras only works if the app knows exactly how the cameras sit. Not roughly: exactly. If one camera turns by a tenth of a degree, a nudge you would never notice, the two pictures stop lining up and the depth map falls apart.

And in a living room, cameras get nudged. A kid bumps the stand. The bar comes down and goes back up a little off.

So the app calibrates itself, all the time. The printed cards around the board (the same ones the iPad uses in part 2) sit at known spots. Every time the app reads the board, it finds the cards in both pictures, works out how the cameras sit right now, and corrects for it. Only the lenses are measured once, at setup, from the checkerboard my kiddo held up.

The app's page with the cameras above: one camera's picture of the board and its printed cards, the depth map below it, and a status line saying a camera had turned by 0.15° and was set right from the cards
The app reading the board live, with the cameras above: one camera's picture and the depth map. Under the depth map: "A camera had turned by 0.15°: set right from the cards."

It has already paid off. Once, the bar had been taken down and put back, and the right camera had turned by 5° and slid 5 mm. Replaying that session, the app found it at the very first reading and set it right, with no new setup. In a later play session, over 62 readings, the two pictures stayed lined up to about half a pixel.

Under the hood (for ML and computer vision readers)

Depth from two cameras. Both pictures are first straightened (rectified), so that every point on the board lands on the same row in the left and right pictures. Matching then becomes a search along each row. The horizontal offset between a point’s two matches is its disparity d, in pixels. With the focal length f (in pixels) and the baseline B (the distance between the cameras), the point’s distance from the cameras is:

Z = f · B / d

Our webcams at 1080p have f ≈ 1,380 px, B ≈ 0.30 m, and the plate is Z ≈ 1.2 m away, so the plate’s disparity is about 350 px. Raising a point by Δh adds about

Δd ≈ f · B · Δh / Z²

of disparity: about 5.5 px for one Duplo brick (19.2 mm). Turned around, a matching error of δd pixels becomes a height error of

δZ ≈ Z² · δd / (f · B)

or about 3.5 mm per pixel on our rig. The baseline B sits in the denominator, which is why the cameras are 30 cm apart, not 5:

Gap between the cameras Shift per brick Height error per pixel
5 cm 0.9 px 21 mm
10 cm 1.8 px 10 mm
20 cm 3.7 px 5.2 mm
25 cm 4.6 px 4.2 mm
30 cm 5.5 px 3.5 mm

The same webcams, 1.2 m above the plate. Small depth cameras put their two lenses a few centimeters apart, as in the first row.

A wider gap isn’t free: the two views differ more, so matching is harder, and a tall tower hides a thin strip beside it from one camera, which then has no depth at all. At 1.2 m, about 30 cm (a quarter of the height) is a good balance.

Diagram: two cameras a baseline B apart at height Z above the plate; rays from a tower top and a plate point land at different spots in each camera's picture; the equations Z = f·B/d, Δd ≈ f·B·Δh/Z² and δZ ≈ Z²·δd/(f·B)
Stereo in one picture: the closer a point is to the cameras, the more it shifts between the two pictures.

The matcher is semi-global matching (SGM, in OpenCV) with a left-right check, which drops pixels whose left-to-right and right-to-left matches disagree. The matches become 3D points in the board’s frame, dropped into a top-down height map with 2 mm cells. A tower’s height is the middle level of its top minus the plate around it, plus the studs’ 4.5 mm on a bare top. A card on top is matched as a whole, to a fraction of a pixel. Averaging over a tower’s top is how a rig with 3.5 mm per pixel gets to 0.9 mm on average: about a quarter of a pixel.

Continuous calibration from the ArUco cards. Calibration comes in two parts:

  • Once, at setup. OpenCV measures both lenses (focal length, center, distortion) and how the two cameras sit relative to each other, from 15 to 20 pictures of a printed checkerboard: about 1,700 equations for about 130 unknowns, solved with Levenberg–Marquardt. The focus stays locked, so the lenses don’t change.
  • At every reading. The 16 ArUco cards around the board give up to 64 known corners per camera. From them, the app re-finds the bar’s pose above the board, the bar’s direction and the small turn of one camera against the other, and rebuilds the straightening maps. A correction changes only the warps, not the matching.

Why every reading? A 0.1° turn moves the rows of one picture up to 2.8 px out of line with the other, and matching along rows needs them within a fraction of a pixel.

Diagram of continuous calibration: the lenses measured once from a checkerboard; then at every reading, find the 16 cards, fit how the cameras sit, keep the most typical recent fit, and straighten and match the pictures for depth
Continuous calibration: the lenses once, from the checkerboard; the cameras' pose at every reading, from the cards; and a keeper that holds the most typical recent fit.

Single fits are noisy, so a keeper holds the most typical of the last five fits rather than the latest. A jump that sticks for three readings is a bump: the keeper takes the new fit at once and says so under the depth map. If even a fresh fit can’t line the cards up, a quiet note suggests redoing the setup, and play goes on. One button resets to the setup calibration, and every update is logged.

One limit: flat cards can’t tell focal length from distance. A lens with a 2% longer focal length, 2% further away, sees exactly the same picture of a flat board, yet reads every height 2% off: 2.4 mm on a six-brick tower. Next, the towers themselves will help. Every tower is a whole number of bricks, n × 19.2 mm (plus 4.5 mm of studs on a bare top), so towers that read tall or short again and again reveal a drift the flat cards can’t see.

Want to build it together?

Next on my list: expanding the experience to new worlds beyond Lego Town. It’s still a side project, and still early: one family, one living room. I’d love to test it properly with educators and more kids aged 6 to 10.

If this sparks something, please get in touch:

  • if you have ideas, or want to work on it together
  • if you’d like to try it with your kids or in your classroom (it takes Duplo baseplates, two webcams on a bar and a stand, and a laptop; the AR view adds an iPad Pro)

LEGO® and DUPLO® are trademarks of the LEGO Group of companies, which does not sponsor, authorize or endorse this project.

More posts Get in touch