geometry

drones

computer vision

From Pixel to Map: Why Casting a Ray to the Terrain Is So Hard

A drone sees a person. Finding where they stand on the map sounds like one line of geometry. It isn’t, and one degree can cost you 200 metres.

Where on the map is this person??
A drone sees a person. The question sounds trivial.

A drone sees a person. Simple question: where exactly on the map are they?

Not “somewhere over there”. A latitude and a longitude you could send someone to.

The obvious answer

It looks like three steps.

  • Pixel: find the person in the image.
  • Ray: turn that pixel into a direction leaving the camera.
  • Ground: follow the ray until it touches the terrain.
pixel→ray→groundcamera image
Pixel → ray → ground. The first two arrows are well understood. The last one is where it gets hard.

The first two steps are solved problems. Camera calibration tells you which direction each pixel looks at. The drone’s GPS and attitude tell you where the camera is and where it points. The hard part hides in one word: touches.

The terrain is not a formula

If the ground were a perfect plane, the intersection would be one line of algebra. Real ground is a height grid: millions of cells, each holding one number, its elevation. There is no equation to solve. You have to walk.

So you walk the ray. Take a step and ask: is the ray still above the ground here? Take another. The moment the answer flips to “no”, the ray crossed the ground somewhere inside the last step.

Then you refine. Cut that step in half, keep the half where the crossing is, repeat. That’s bisection. About ten halvings take a 30-metre step down to a few centimetres.

height grid · one height per cell1 · walk the ray
Walk the ray and compare heights at each step (cyan). When a sample falls below the ground (pink), halve the last step until the crossing is pinned down (green).

An error that moves the point 200 metres

There’s one error in this pipeline that can move your point 200 metres. It isn’t a bug. Every input can be almost right and it still happens.

200 m…and it is not a bug
Same drone, same person, almost the same inputs. The point lands 200 m away.

Hold that thought. First, a more ordinary trap.

Problem 1: jumping over a crest

Walking the ray with big steps is fast. It’s also how you miss things. A thin ridge, a wall or a cliff edge can fit entirely between two samples. Both samples say “above ground”, and the ray goes straight through the crest without noticing. You still get a hit, just in the wrong valley.

The fix is to stop picking a step size at all. Walk the grid cell by cell, visiting every cell the ray passes over, in order. That traversal is called DDA, the same trick early 3D games used to cast rays through a tile map. No cell is skipped, so no crest can hide.

Big steps: jumps the crest ✗wrong valleyCell by cell: nothing skipped
Top: with big steps, every sample is above ground and the crest is never seen. Bottom: visiting every cell, the crest is found.

The 200 metres: it’s the angle

Now the promised number. It has nothing to do with the terrain. It’s about how steep the ray is.

Put the drone h=200h = 200 m above flat ground. A ray looking down at an angle θ\theta below the horizon lands at a distance

d=htan⁡θd = \dfrac{h}{\tan\theta}

Looking 45° down, the point is 200 m away. If your angle is off by one degree, 44°, it lands at 207 m. Seven metres of error. Annoying, but fine.

Now look almost at the horizon: 8° down. The point is 1,423 m away. One degree off, 7°, and it lands at 1,629 m. Same one-degree error. 206 metres of difference.

Looking down45°hits at 200 m1° less: 207 merror7 m04008001,2001,600drone at 200 m · same scale both axes
Drag the angle. The yellow ray is the measurement; the dashed pink ray is the same measurement one degree flatter.

Near the horizon the ray skims the ground, so a tiny rotation slides the hit point a long way. One degree is not a big error once you add up the attitude sensor, the gimbal and the timing. That’s why a good system rejects grazing rays: below some angle, the answer is too sensitive to trust.

Not a point: a region

Which leads to the honest answer. Every input carries a small error: the GPS position, the angles, the photo’s timestamp against the GPS fix, the exact pixel. Instead of pretending they’re zero, you cast hundreds of rays, each with its own small random nudge to those inputs, and look at where they land.

The hits form a cloud, stretched along the direction the camera looks, because that’s where angle error turns into distance. Draw the ellipse that holds 95% of them and you have the real output: the person is in here, with 95% confidence.

± GPS ± angles ± time ± pixel95 % regiondrone
Hundreds of rays, each with small errors in GPS, angles, time and pixel. Where they land forms the 95% ellipse.

A point on a map looks precise. An ellipse is precise about how imprecise it is. That’s the better answer.

Your turn

Which one surprised you more, the hidden crest or the one degree? And what should I explain next: why your GPS height isn’t height above sea level, or why your compass doesn’t point north?

Also in Spanish and Portuguese.

I’m Arnold Moya. I write about software architecture, AI, systems, performance, and the tradeoffs behind building technology that works in the real world.