Search

Search papers and lessons

Structure from Motion

Level 3 · 9 min read

Structure from motion recovers where each photo was taken and a sparse 3D point cloud from nothing but overlapping images, by matching points and then refining everything jointly.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Probability
  • Computer vision
  • Discrete math
  • Architectures
  • Sign in to see your progress

TL;DR

Structure from motion (SfM) takes a set of photos of one static scene and works out two things at once: the position and direction of the camera for each photo, and a sparse cloud of 3D points that the photos see. It finds points that appear in several images, triangulates them, adds one camera at a time, and then adjusts all cameras and points together to fit every observation.

It is the step that turns a folder of holiday photos or a phone video into the cameras and points that a 3D reconstruction or a renderer needs.

Where you'll see it: as the first stage of photogrammetry tools, as the camera-calibration step before training a neural renderer such as Gaussian Splatting, and in robotics as the core of visual mapping.

Intuition

Two unknowns, one set of pictures

Hold a coin at arm's length and look at it from the left, then from the right. The coin's edge looks different in the two views. Your brain uses that change to tell you the coin's shape and how far you moved your head. Structure from motion does the same thing with a computer.

The problem is circular. To locate a 3D point, you need to know where the cameras were. To know where the cameras were, you need to know where the 3D points are. SfM breaks the circle by starting small. It picks two photos, finds points they share, and works out their relative pose from those matches. Once the two cameras and some points are fixed, it can place a third camera by looking at points already in place, then add more points, and so on. At the end a single global refinement makes everything agree.

The key ingredient is that the photos overlap. Two photos of the same wall with a shared corner give one constraint. A hundred photos with dozens of shared features give the system enough constraints to pin down every camera and point.

flowchart LR
  A["Photos"] --> B["Detect features"]
  B --> C["Match features between pairs"]
  C --> D["Pick a starting pair"]
  D --> E["Triangulate points"]
  E --> F["Register next camera"]
  F --> G["Triangulate new points"]
  G --> H["Bundle adjustment"]
  H --> F
  H --> I["Cameras and sparse cloud"]

The math

Cameras

Each camera is a pinhole model (see Pinhole Camera Model). Its pose is a rotation RiR_i and translation ti\mathbf{t}_i that take world points into its frame, so a world point Xj\mathbf{X}_j lands at camera coordinates RiXj+tiR_i \mathbf{X}_j + \mathbf{t}_i. The camera has intrinsics KK with focal length and principal point, and the image point is

xij=π ⁣(K (RiXj+ti)),π ⁣((uvw))=(u/wv/w).\mathbf{x}_{ij} = \pi\!\left(K\,(R_i \mathbf{X}_j + \mathbf{t}_i)\right), \qquad \pi\!\left(\begin{pmatrix} u \\ v \\ w \end{pmatrix}\right) = \begin{pmatrix} u/w \\ v/w \end{pmatrix}.

Here π\pi divides by depth. Rotations are handled by rotation matrices or by a parameterisation such as a quaternion.

Two views and the epipolar constraint

For a point seen in views ii and i′i' at homogeneous pixel positions x\mathbf{x} and x′\mathbf{x}', the epipolar constraint says the two rays and the baseline are coplanar. For normalised image coordinates this is x′TE x=0\mathbf{x}'^{\mathsf{T}} E \, \mathbf{x} = 0, where E=[t]×RE = [\mathbf{t}]_\times R is the essential matrix. Matching points that satisfy this constraint is how the starting pair is found. Real matches include wrong ones, so the estimate is run inside a robust loop that rejects outliers.

Triangulation

Once two cameras are known, a point is found by intersecting the two rays. With exact data the rays meet. With noise they do not, so the point is chosen to best fit both observations, usually by a linear least-squares solve on the stacked projection equations (see Singular Value Decomposition (SVD) for the solve).

Bundle adjustment

Every camera and point is refined together by minimising the reprojection error: the distance between each observed pixel and the pixel predicted by the current model. Writing O\mathcal{O} for the set of observed pairs (i,j)(i,j) and xij\mathbf{x}_{ij} for the measured pixel,

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

This is a nonlinear least-squares problem (see Nonlinear Least Squares). Its Jacobian is large but very sparse, because each residual depends on one camera and one point. Solvers exploit that sparsity, and damped Gauss-Newton steps (the Levenberg-Marquardt idea) keep each update stable. The Jacobian is the object from Jacobian Matrix.

Scale and gauge

Photos alone cannot give absolute scale. If every camera and point is scaled by the same factor, all images stay the same. Reconstructions are therefore known only up to a similarity transform: a rotation, a translation and one overall scale. Applications fix the scale from a known distance or from a GPS or ruler measurement.

Worked examples

Example
Triangulating one point from two cameras

Use a normalised camera with focal length 1, so u=X/Zu = X/Z and v=Y/Zv = Y/Z. Camera 1 sits at the origin. Camera 2 is translated one unit along xx, so its coordinates are X2=X−1X_2 = X - 1.

  1. Place a 3D point at X=(0.5,0,2)\mathbf{X} = (0.5, 0, 2).
  2. In camera 1, u1=0.5/2=0.25u_1 = 0.5 / 2 = 0.25.
  3. In camera 2, the point is at (0.5−1,0,2)=(−0.5,0,2)(0.5 - 1, 0, 2) = (-0.5, 0, 2), so u2=−0.5/2=−0.25u_2 = -0.5 / 2 = -0.25.
  4. The disparity is d=u1−u2=0.5d = u_1 - u_2 = 0.5.
  5. For a rectified pair with baseline b=1b = 1, depth is Z=fb/d=1⋅1/0.5=2Z = f b / d = 1 \cdot 1 / 0.5 = 2.
  6. Recover X=u1Z=0.25⋅2=0.5X = u_1 Z = 0.25 \cdot 2 = 0.5, which matches the original point.

Interpretation: the bigger the disparity, the closer the point. Wide baselines give larger disparities and more accurate depth, which is why SfM prefers photos taken from different positions.

Example
Reprojection error with a noisy pixel

Take the same setup but with a real focal length of f=800f = 800 pixels, and a point whose true image position in camera 2 is u2=−0.25⋅800=−200u_2 = -0.25 \cdot 800 = -200 pixels.

  1. The feature detector reports the point at −160-160 pixels in camera 2, which is an error of 40 pixels in one direction.
  2. The current model predicts −200-200 pixels, so the residual is x−π(⋅)=−160−(−200)=40\mathbf{x} - \pi(\cdot) = -160 - (-200) = 40.
  3. The squared residual contributes 402=160040^2 = 1600 to the bundle adjustment cost.
  4. If camera 2 is nudged slightly so that its predicted pixel moves to −160-160, the residual becomes zero. Bundle adjustment does this kind of nudge across all observations at once.

Interpretation: the cost is in pixels squared, not in metres. This is what lets SfM combine points from cameras at very different distances on one scale.

Example
Size of the problem for a modest capture

Suppose a capture has 50 cameras and 2000 points, and each point is seen by 5 cameras. Ignore intrinsics for now.

  1. Each camera has 6 pose parameters (3 for rotation, 3 for translation). The cameras need 50×6=30050 \times 6 = 300 unknowns.
  2. Each point has 3 coordinates, so the points need 2000×3=60002000 \times 3 = 6000 unknowns.
  3. The total is 300+6000=6300300 + 6000 = 6300 unknowns.
  4. Each point seen by 5 cameras gives 5 observations, each with 2 pixel coordinates. That is 2000×5×2=20 0002000 \times 5 \times 2 = 20\,000 equations.
  5. The system is over-determined by a wide margin, so the solution is well constrained once the gauge (scale and frame) is fixed.

Interpretation: the Jacobian has about 20 00020\,000 rows and 63006300 columns but only a few nonzeros per row. Sparse solvers are the reason bundle adjustment scales to thousands of photos.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Few shared features between photosStarting pair is weak, and new cameras cannot be registeredTake photos with more overlap, or use more features
Repeated texture or symmetric structureFalse matches that look correct locallyGeometric verification with a robust estimator
Pure rotation of the cameraDepth cannot be triangulated, because the baseline is zeroUse a capture with translation, or a rotation-only panorama method
Moving objects in the sceneMatches on people or cars break the static-scene assumptionMask dynamic regions before matching
Long sequencesDrift accumulates when cameras are added one by onePeriodic global bundle adjustment, or loop closure
Need a dense surfaceSfM gives only sparse pointsRun multi-view stereo on the recovered cameras

SfM is the right tool when the scene is static and the photos overlap. It is the wrong tool when the scene moves, when there is no texture, or when a dense surface is needed without a second stage.

History

Structure from motion grew out of photogrammetry, the older practice of measuring 3D shape from photographs with known cameras. The shift to unknown cameras came from computer vision work on projective geometry in the 1990s, which showed that a reconstruction is possible from images alone.

The modern incremental pipeline is usually traced to the Photo Tourism work of Snavely, Seitz and Szeliski in 2006, which reconstructed large collections of internet photos. The bundle adjustment step that refines everything jointly is much older, from photogrammetry, and it remains the core of the method.

Schönberger and Frahm's COLMAP pipeline, published at CVPR in 2016, made a robust incremental SfM system widely available. Gaussian splatting uses the output of such a pipeline as its starting cloud. The Kerbl et al. paper cites both Snavely et al. and Schönberger and Frahm for this step, and it treats SfM as the input stage rather than as its own contribution.

Check yourself

QuestionRecall: what does the epipolar constraint tell you about a point seen in two photos?Show answer

If a point is seen at x\mathbf{x} in one photo and x′\mathbf{x}' in another, then x′TEx=0\mathbf{x}'^{\mathsf{T}} E \mathbf{x} = 0 for normalised coordinates, where EE is the essential matrix. The matching point in the second photo must lie on a known line, so a search for it is one-dimensional.

QuestionComputation: in the triangulation example, a point is at X=(0.5,0,2)\mathbf{X} = (0.5, 0, 2) with cameras one unit apart and focal length 1. What is the disparity?Show answer

u1=0.25u_1 = 0.25 and u2=−0.25u_2 = -0.25, so the disparity is u1−u2=0.5u_1 - u_2 = 0.5.

QuestionWhy: why does bundle adjustment use pixel reprojection error rather than error in 3D space?Show answer

The measurements are pixels, so the residual is the difference between the observed and predicted pixel. Measuring in 3D would require knowing depth, which is the unknown being solved for. Pixel error is also comparable across cameras at different distances.

QuestionApplied: you reconstruct a scene from photos and the result is correct in shape but the room looks twice as big as it is. What is the most likely cause, and how would you fix it?Show answer

SfM recovers the scene only up to a similarity transform, so the overall scale is arbitrary. The fix is to fix the scale from a known measurement in the scene, such as a distance between two marked points, and rescale all cameras and points by the ratio of the true distance to the reconstructed one.

Sources