Structure from Motion, from pixels to 3D

<< Back to my homepage

Structure from Motion, usually called SFM, asks a surprisingly interesting question: if I take many ordinary photographs of the same scene from different positions, can a computer recover both the 3D structure of the scene and the positions from which the photographs were taken?

The answer is yes, under the right conditions. This is one of the foundations of photogrammetry, 3D reconstruction, visual localization, mapping, and many modern computer vision pipelines.

The core intuition: one photograph tells us the direction in which a 3D point lies, but not its distance. Two or more photographs from different viewpoints give us several rays. If those rays correspond to the same physical point, geometry lets us estimate where that point exists in 3D.

1. Start with one camera

Before understanding SFM, we need to understand what a camera does geometrically.

Imagine a 3D point in camera coordinates:

P = (X, Y, Z)

The camera turns this 3D point into a 2D image location:

x = f X / Z
y = f Y / Z

Here f is the focal length. The important part is the division by Z.

Why does this happen? Because the camera is a perspective projection system. A point twice as far away appears roughly half as large. Similar triangles give us the relationship directly.

For the horizontal coordinate:

x / f = X / Z

therefore

x = f X / Z

The same reasoning gives y = fY/Z.

This already tells us why a single photograph cannot directly recover depth. Suppose a pixel was generated by the 3D point (1, 1, 2). The point (2, 2, 4) projects to the same location because the ratios X/Z and Y/Z are unchanged. In fact, infinitely many points along the same viewing ray can produce the same pixel.

Pinhole camera geometry
Pinhole camera geometry. Image by Finitehoax, CC0, via Wikimedia Commons.

2. Real cameras use intrinsic parameters

Real images use pixel coordinates, so the camera model is usually written with an intrinsic matrix:

    [ fx  0  cx ]
K = [  0 fy  cy ]
    [  0  0   1 ]

fx and fy describe focal length in pixel units. cx and cy describe the principal point, usually somewhere near the image center.

If a 3D point is already expressed in the camera coordinate system, the projection can be written in homogeneous form:

s p = K P

But a point normally lives in a world coordinate system. The camera can be rotated and translated, so we first transform it into camera coordinates:

Pc = R Pw + t

Then projection becomes:

s p = K [ R | t ] Pw

This single equation is central to SFM. The unknowns are now visible. We want to discover the 3D points Pw, and we also want to discover the camera poses R and t.

3. Find the same physical points in different photographs

Suppose I photograph a building from two nearby locations. A corner of a window may appear at pixel (420, 215) in the first image and (367, 218) in the second image.

SFM first needs to establish that these two pixels describe the same physical point.

Classical systems detect distinctive local features using methods such as SIFT, SURF, ORB, or similar feature detectors. Each detected point receives a descriptor, which is a compact numerical representation of the local visual pattern around that point.

The descriptors are compared across images. Similar descriptors become candidate matches.

But descriptor matching is imperfect. Repeated windows, bricks, leaves, reflections, and textureless regions can produce incorrect correspondences. This is why geometry must verify the matches.

4. Epipolar geometry reduces the search

Take two cameras observing one 3D point P. The first camera center, the second camera center, and P define a plane called the epipolar plane.

That plane cuts each image plane along an epipolar line.

This gives an extremely useful constraint. If a point appears at one location in image 1, its corresponding point in image 2 cannot be anywhere in the image. It must lie on the associated epipolar line.

Epipolar geometry
Two cameras observing the same 3D point. Public domain illustration by ZooFari via Wikimedia Commons.

Mathematically, corresponding homogeneous image points x1 and x2 satisfy:

x2^T F x1 = 0

F is the fundamental matrix.

You can interpret F x1 as the epipolar line in image 2 on which the corresponding point should lie. A candidate match far away from that line is suspicious.

If camera intrinsics are known, we work with normalized camera coordinates and the essential matrix:

x2^T E x1 = 0

The fundamental matrix and essential matrix encode the relative geometry between two views. For calibrated cameras:

E = [t]x R

Here R is relative rotation, t is relative translation, and [t]x is the skew symmetric matrix representing the cross product with t.

5. RANSAC protects us from wrong matches

Imagine that feature matching gives us 1,000 correspondences. Maybe 800 are correct and 200 are wrong. A direct least squares estimate can be badly corrupted by the wrong matches.

RANSAC solves this by repeatedly sampling a small subset of matches, estimating a model such as F or E, and counting how many other matches agree with that model.

A simplified version looks like this:

repeat many times:
    choose a small random set of matches
    estimate F or E
    measure geometric error for all matches
    count the inliers

keep the model with the strongest consensus

This does not magically guarantee perfection, but it makes the pipeline much more resistant to outliers.

6. Recover the relative camera pose

Once we estimate the essential matrix, we can decompose it to recover candidate values for rotation and translation between the two cameras.

There is an important limitation. From two monocular images, translation is recovered only up to scale.

If the true camera displacement was 10 centimeters, the reconstruction could equally represent it as 1 unit, 100 units, or 0.1 units. The image geometry stays consistent as long as the entire reconstruction is scaled together.

This is called scale ambiguity.

To obtain metric scale, we need extra information such as a known distance, stereo baseline, depth sensor, GPS, IMU constraints, or another source of physical scale.

7. Triangulation creates 3D points

Now suppose we know the poses of two cameras and we know that pixel p1 and pixel p2 correspond to the same physical point.

Each pixel defines a ray extending outward from its camera center. In an ideal noiseless world, the two rays intersect at exactly one 3D point.

Ideal triangulation
Ideal two view triangulation. Public domain illustration via Wikimedia Commons.

In real data the rays usually do not intersect perfectly because feature locations, calibration, pose estimates, and lens models contain errors. Triangulation therefore finds the 3D point that best satisfies the observations.

For camera matrix P1 and image point x1:

x1 x (P1 X) = 0

and similarly for the second camera:

x2 x (P2 X) = 0

These constraints can be rearranged into a linear system:

A X = 0

The homogeneous 3D point X can be estimated using singular value decomposition. Practical systems can later refine this using nonlinear optimization.

8. Reprojection error tells us how wrong we are

Suppose we reconstructed a 3D point Xj and estimated camera i with parameters Pi.

We can project Xj back into the image:

x_hat_ij = project(Pi, Xj)

We also have the feature position that was actually measured in the image:

x_ij

The difference is the reprojection error:

e_ij = || x_ij - x_hat_ij ||

If the geometry is correct, this error should be small. If the error is large, something may be wrong with the feature match, camera pose, calibration, or 3D point.

Reprojection error is one of the most important quantities in classical geometric computer vision because it directly asks whether the reconstructed world explains the images we actually observed.

9. Bundle adjustment is the global cleanup step

Early camera poses and triangulated points are not perfect. Small errors accumulate. Bundle adjustment jointly refines camera parameters and 3D point positions by minimizing reprojection error over many observations.

A simplified objective is:

min over cameras and 3D points

sum over observations || x_ij - project(camera_i, X_j) ||^2

This is a nonlinear least squares problem.

The phrase bundle adjustment comes from the bundles of light rays connecting cameras and 3D points. Optimization adjusts those bundles until the predicted projections line up with the measured image features as closely as possible.

This stage is one reason SFM can produce surprisingly coherent reconstructions from many individually noisy measurements.

10. How more images are added

A common incremental SFM system does not solve the entire reconstruction from scratch at once.

It begins with a strong image pair. That pair should have enough common features and enough viewpoint change to give useful triangulation.

After the initial two view reconstruction, a new image is selected. Some features in this new image correspond to 3D points that already exist in the current model.

Now the problem changes. We know 3D points and their 2D locations in the new image. We need to estimate the new camera pose.

This is the Perspective n Point problem, usually abbreviated PnP.

Conceptually:

known:
    3D point X1  -> observed pixel x1
    3D point X2  -> observed pixel x2
    3D point X3  -> observed pixel x3
    ...

unknown:
    camera rotation R
    camera translation t

PnP, usually combined with RANSAC, estimates the camera pose. After that, new tracks can be triangulated to create more 3D points. Bundle adjustment then refines the growing model.

11. The complete incremental SFM pipeline

INPUT PHOTOGRAPHS | v Detect local features | v Compute descriptors | v Match features between images | v Geometrically verify matches with RANSAC | v Choose an initial image pair | v Estimate relative pose using E or F | v Triangulate initial 3D points | v Bundle adjustment | v Choose another image | v Estimate its pose using PnP | v Triangulate additional points | v Bundle adjustment again | v Repeat until useful images are registered | v SPARSE 3D RECONSTRUCTION + CAMERA POSES

12. What SFM actually gives you

A classical SFM system normally outputs two major things:

The result is usually sparse because feature detectors select distinctive points rather than reconstructing every image pixel.

If we want a dense point cloud or surface, SFM is often followed by Multi View Stereo, or MVS. SFM gives reliable camera geometry. MVS then uses those camera poses to estimate dense depth across the images.

A simplified reconstruction stack is therefore:

Images
  -> Structure from Motion
       -> camera poses + sparse points
  -> Multi View Stereo
       -> dense point cloud
  -> meshing
       -> surface mesh
  -> texturing
       -> textured 3D model

13. Why camera movement matters

SFM needs parallax. If the camera only rotates around its optical center without meaningful translation, points change direction in the image but there is very little geometric information about their depth.

For triangulation, a larger baseline usually improves depth accuracy, but too much viewpoint change can make feature matching harder.

This produces a practical tradeoff:

14. Why SFM sometimes fails

SFM is geometry driven, so certain scenes are difficult.

15. The most important mental model

I think the easiest way to remember SFM is not as a list of algorithms but as a loop between images and geometry.

Images propose correspondences. Geometry rejects bad correspondences. Correspondences reveal camera motion. Camera motion enables triangulation. Triangulated points help localize more cameras. All cameras and points are then optimized together.

So the reconstruction grows because each solved part makes the next part easier.

A single image says, "the point is somewhere along this ray." A second image gives another ray. Camera geometry tells us how those rays relate. Triangulation estimates the 3D point. Hundreds of thousands of these relationships eventually turn ordinary 2D photographs into a 3D reconstruction.

16. One equation to keep in mind

If I had to keep only one equation from the whole discussion, it would be:

x = f X / Z
y = f Y / Z

It looks simple, but almost the entire problem begins here.

The division by depth creates perspective. Perspective means one image loses depth information. Camera motion gives us multiple perspectives. Multiple perspectives create geometric constraints. Those constraints allow us to recover both motion and structure.

That is Structure from Motion.


Image credits: The diagrams on this page are hosted by Wikimedia Commons. The pinhole camera diagram is CC0. The epipolar geometry and ideal triangulation diagrams used here are public domain. Source links are provided beneath each figure.