Search

Search papers and lessons

Visual SLAM

Level 3 · 15 min read

Visual SLAM builds a map and tracks a camera's position at the same time, using only images, by matching features, triangulating points and refining everything jointly.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Computer vision
  • Probability
  • Classical models
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Visual SLAM, short for simultaneous localisation and mapping, lets a robot or phone with only a camera work out where it is while it builds a map of what surrounds it. It finds distinctive points in each image, matches them over time, triangulates them into 3D, and continually refines the camera's path and the map together. Without an outside reference the estimate drifts, so systems also recognise places they have seen before and correct the accumulated error.

Where you'll see it: in the tracking of drones and robot vacuums that have no GPS indoors, in the world-tracking behind augmented reality on phones, and in self-driving stacks that fuse camera data with other sensors.

Intuition

Localise yourself and map the room at once

Imagine walking through an unfamiliar building blindfolded except for a camera on your head. To know where you are, you need a map. To build a map, you need to know where you are. Each depends on the other, and SLAM is the method for solving both at once.

The camera gives you snapshots. In each one you pick out a few hundred corners and textured spots, which are easy to find again from a slightly different angle. As you move, the same spots shift across the image. Their shift, together with the geometry of two views, tells you how the camera moved and where the spots are in 3D. A sequence of such estimates is the trajectory, and the spots form the map.

Why it drifts, and how it is fixed

Every estimate carries a small error, and the errors add up. A camera that turns 1 degree wrongly at each step ends up pointing in a completely different direction after a few hundred steps. This is drift. SLAM fights it in two ways. Locally, it refines the recent poses and points together so they agree with all the images. Globally, when the camera returns to a place it has seen, it recognises the place and pulls the whole trajectory back into shape. That second step is called loop closure.

flowchart LR
  A[New image] --> B[Track features against the map]
  B --> C[Estimate camera pose]
  C --> D[Triangulate new points]
  D --> E[Refine poses and points locally]
  E --> F{Revisited a known place?}
  F -- yes --> G[Loop closure and global correction]
  F -- no --> A
  G --> A

The math

Unknowns

SLAM estimates two kinds of quantity. Each camera pose TiT_i is a rigid motion: a rotation RiR_i and a translation ti\mathbf{t}_i, which together move a world point into camera coordinates as Xc=RiXw+ti\mathbf{X}_c = R_i\mathbf{X}_w + \mathbf{t}_i. This is the rigid body transformation that packs the rotation and translation into one matrix. Each map point is a 3D position Xj\mathbf{X}_j. The camera projects a point through the pinhole model with calibration matrix KK, and π\pi denotes the division by depth that turns camera coordinates into pixels.

Reprojection error

The measurement is the pixel zij\mathbf{z}_{ij} where point jj was seen in frame ii. The model predicts π(K(RiXj+ti))\pi\left(K(R_i\mathbf{X}_j + \mathbf{t}_i)\right). The error is the difference, and SLAM minimises the sum of squared differences over all observations (the sign of each difference does not matter once squared; the examples below compute predicted minus measured):

min⁡{Ri,ti}, {Xj}∑(i,j)∥zij−π ⁣(K(RiXj+ti))∥2.\min_{\{R_i, \mathbf{t}_i\},\ \{\mathbf{X}_j\}} \sum_{(i,j)} \left\lVert \mathbf{z}_{ij} - \pi\!\left(K(R_i\mathbf{X}_j + \mathbf{t}_i)\right) \right\rVert^2.

This is a nonlinear least-squares problem, because the projection divides by depth and the rotations are nonlinear in their parameters. It is solved by the same method as in nonlinear least squares: linearise the residuals, solve a linear system for an update, apply it, and repeat. When the poses and points are optimised together over a window of recent frames, this is called bundle adjustment.

Structure of the problem

Each observation involves one camera and one point, so the Jacobian is sparse. Most entries are zero because a point is seen by only a few cameras. Solvers exploit this sparsity, which is what makes bundle adjustment over a window of recent frames fast enough for real time.

Scale ambiguity

A single moving camera cannot recover absolute scale. Scale every camera translation and every point by the same factor ss, and the predicted pixels do not change, because π\pi divides out the scale. The reconstruction is only known up to a global scale. Monocular SLAM therefore needs a reference, such as a known baseline between the first two frames, an object of known size, or an inertial sensor, to fix the scale.

Pose graphs

Once the map is large, optimising every point is expensive. A common approach keeps only keyframe poses and the relative motions between them, a pose graph. Each edge says how one keyframe is positioned relative to another, and loop closures add extra edges between distant keyframes. Solving the pose graph spreads the correction along the trajectory.

Worked examples

Example
Residual from a small pose error

Take a point at X=(1,0,4)\mathbf{X} = (1, 0, 4) in camera coordinates, with K=IK = I for simplicity. The true pose has zero translation. The measured pixel is u=1/4=0.25u = 1/4 = 0.25.

  1. The predicted horizontal coordinate is u=X/Z=1/4=0.25u = X/Z = 1/4 = 0.25, so the residual is 00.
  2. Suppose the tracked pose has a translation error tx=0.1t_x = 0.1 along the xx-axis. The predicted point is X+0.1=1.1X + 0.1 = 1.1, so u=1.1/4=0.275u = 1.1/4 = 0.275.
  3. The residual is 0.275−0.25=0.0250.275 - 0.25 = 0.025, and the squared error is 0.0252=0.0006250.025^2 = 0.000625.
  4. Interpretation: a 0.1-unit error in position at depth 4 shows up as a 0.025 error in normalised image coordinates. The optimiser reduces the total over all points and frames until the residuals are as small as the noise allows.

The plot shows the residual as the translation error changes. Here the residual is simply t/4t/4, so the marked point is t=0.1t = 0.1 with residual 0.025.

−1−0.500.51−0.2−0.100.10.2translation error t (metres)residual in normalised image units
  • residual = t / 4
For a point at depth 4, a translation error t shifts its image by t/4. The marked point is the 0.1 error worked above.
Example
Scale ambiguity, checked by hand

Start with the camera at the origin and a point at X=(1,0,4)\mathbf{X} = (1, 0, 4) with K=IK = I. Its image is u=1/4u = 1/4.

  1. Add a second camera, translated by t=(0.5,0,0)\mathbf{t} = (0.5, 0, 0) from the first, which sees the point at u=(1−0.5)/4=0.125u = (1 - 0.5)/4 = 0.125.
  2. Scale the whole scene by s=2s = 2: the point becomes (2,0,8)(2, 0, 8) and the translation becomes (1,0,0)(1, 0, 0).
  3. The first camera still sees u=2/8=1/4u = 2/8 = 1/4. The second sees u=(2−1)/8=0.125u = (2 - 1)/8 = 0.125. Both images are unchanged.
  4. Interpretation: the images cannot tell a scene of size 1 from one of size 2. A monocular system must bring in a known length, such as the baseline of its first two frames, before its map has real units.
Example
Drift over a long path

Suppose a camera's estimated step lengths are 1 percent too long, and it travels 100 metres in a straight line. The estimated path is 101 metres long, so the position error is 1 metre, or 1 percent of the distance travelled. The same 1 percent over a longer path adds proportionally more error, which is the drift that loop closure is meant to remove.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Featureless walls or blurFew features are tracked, and the pose estimate fails or jumps.Add an inertial sensor for short gaps, or wait for texture to return.
Fast rotationFeatures move too far between frames to match.Use higher frame rates, or predict the motion first.
Dynamic objectsMoving people or cars are treated as static points and corrupt the map.Mask dynamic regions or reject inconsistent points.
Repetitive patternsLoop closure matches the wrong place.Verify candidate loops geometrically before accepting them.
Monocular onlyScale is unknown, and drift is not bounded by any measurement.Fuse an IMU, a stereo pair or a wheel odometer.
Full bundle adjustment on every frameCost grows too fast for real time.Optimise a sliding window, and run global pose-graph optimisation in the background.

Visual SLAM has no single best design. Feature-based systems are robust and widely used. Direct methods, which work on pixel intensities, can use more of the image but are more sensitive to lighting changes.

History

The problem comes from robotics in the 1980s, when Smith, Self and Cheeseman and others described how a robot could maintain a map and its own position using probabilistic estimates. Through the 1990s the joint estimation view was developed, mostly with range sensors such as lasers and sonar, and the name SLAM became standard in the mid-1990s.

Camera-based versions became practical in the 2000s, as cheap cameras and faster processors made it possible to track hundreds of points per frame. Monocular systems that ran in real time on a laptop appeared in the early to mid 2000s, for example Andrew Davison's MonoSLAM and, later, Klein and Murray's PTAM, and the feature-based approach that combined tracking, mapping and loop closure became the common design in the following decade. Mobile devices later brought the same ideas into augmented reality.

Check yourself

QuestionRecall: what does the reprojection error measure, and what is being minimised?Show answer
It measures the difference between a pixel where a map point was actually observed and the pixel predicted by the current camera pose and point position. SLAM minimises the sum of squared reprojection errors over all observations, adjusting poses and points together.
QuestionComputation: a point is at camera coordinates (0.5,0,2)(0.5, 0, 2) with K=IK = I. A pose error shifts it by tx=−0.2t_x = -0.2. What is the horizontal residual in normalised image units?Show answer
The true image is 0.5/2=0.250.5/2 = 0.25. With the shift, the point is at 0.30.3, so the image is 0.3/2=0.150.3/2 = 0.15. The residual is 0.15−0.25=−0.10.15 - 0.25 = -0.1.
QuestionWhy: a monocular SLAM system can reconstruct a room perfectly in shape and still have the wrong size. Why?Show answer
Scaling every camera translation and every point by the same factor leaves all image measurements unchanged, because the projection divides out the scale. The images contain no information about absolute length, so the map is known only up to scale until a known length or another sensor fixes it.
QuestionApplied: a robot tracks its path using only a camera and drifts so that it reports the two times it passed one doorway as 2 metres apart. Which SLAM component should correct this, and how?Show answer
Loop closure. When the robot recognises the doorway from its earlier visit, the system adds a constraint between the two distant poses. A pose-graph optimisation then spreads the correction along the trajectory, pulling the drifted poses back into a consistent shape.

Sources