Skip to main content
Open In Colab This section follows Chapter 44 of Foundations of Computer Vision by Antonio Torralba, Phillip Isola, and William T. Freeman. The book’s own figures are not reproduced here. Depth from Two Views recovers depth when you already know where the two cameras are. Structure from motion drops that assumption. You start from a set of photos of a scene, taken from unknown positions, and you recover two things at once: the pose of every camera and the 3D position of the points the photos share. The poses are the motion, the points are the structure. In this section you build the method yourself on real photos. You match features between two views, estimate the essential matrix, recover the relative pose, triangulate points, and then add views one at a time while bundle adjustment refines everything. At the end you compare your cameras with a reconstruction of all the photos made with COLMAP, a widely used structure-from-motion system that Structure from Motion at Scale covers in its own section.

Unknowns and measurements

Suppose MM cameras observe NN points, and every point is visible in every image. Each observation is a pixel, so it gives two numbers, and there are 2NM2NM of them. The unknowns are three coordinates per point and six pose parameters per camera (three for rotation, three for translation), so 3N+6M3N + 6M in total. Not all of these unknowns can be determined. If you rotate, translate and scale the whole scene together with the cameras, every image stays exactly the same. That freedom has seven degrees: three for rotation, three for translation and one for scale. It is called the gauge freedom. The images can only fix the reconstruction up to such a similarity transform, so the problem has a chance of being determined when 2NM  ≥  3N+6M−7.2NM \;\ge\; 3N + 6M - 7 . Two cameras and five points give 20≥2020 \ge 20, which is why five correspondences are the minimum for the relative pose of two calibrated cameras. In practice you have thousands of points and many cameras, so the system is heavily overdetermined, and the work goes into finding correct correspondences and solving a large nonlinear least-squares problem. You will fix the gauge yourself: the first camera sits at the origin with the identity rotation, and the scale is whatever the first two views give. At the end, a similarity transform maps your reconstruction onto COLMAP’s.

The scene

The photos are the garden scene of the Mip-NeRF 360 dataset: 185 photos taken while walking around a table in a garden. You use the copies downsampled by a factor of eight, about 650×420650 \times 420 pixels. The dataset also ships a COLMAP reconstruction of the full set, made by the dataset’s authors and not rerun here, as three binary files: cameras.bin holds the intrinsics, images.bin the pose of each photo and the pixels it observed, and points3D.bin the sparse points with their colors. The reader below parses them, so you can compare against them later.
You treat the intrinsic matrix KK as known. That is the calibrated case: camera calibration measures KK once, and structure from motion then only has to find the poses and the points. With KK known, a pixel u\mathbf{u} in homogeneous coordinates becomes a viewing direction x=K−1u\mathbf{x} = K^{-1}\mathbf{u}, and the geometry below works with those normalized coordinates. You use a run of twelve consecutive photos, which covers a short arc of the walk around the table.
Twelve photos of the garden scene in a grid, all showing a round wooden table with a vase on it, a lawn and bushes behind. The viewpoint moves a little to the side from one photo to the next.

Features and matches

Structure from motion needs pixels in different photos that show the same 3D point. You find them in two steps, as Chapter 44 of the book describes. An interest point detector picks distinctive locations, such as corners and blobs, at several scales. A descriptor then summarizes the patch around each location as a vector that changes little with viewpoint and lighting. Here the detector and descriptor are SIFT: each keypoint gets a 128-dimensional histogram of gradient orientations. Two keypoints match when their descriptors are close. A nearest-neighbor match alone is unreliable on repeated texture such as grass and leaves, where many patches look alike. The ratio test keeps a match only when the nearest descriptor is clearly closer than the second nearest, here by a factor of 0.80.8. Matches that pass can still be wrong, and the geometry below removes most of the rest.
Two garden photos side by side with green lines joining matched keypoints. Most lines are nearly horizontal and parallel, joining the same spot on the table or bushes; a few cross the others at steep angles.

The essential matrix

Place the first camera at the origin, so a point X\mathbf{X} has coordinates X\mathbf{X} in its frame, and the second camera at rotation RR and translation t\mathbf{t}, so the same point has coordinates RX+tR\mathbf{X} + \mathbf{t} there. The normalized image points are x∝X\mathbf{x} \propto \mathbf{X} and x′∝RX+t\mathbf{x}' \propto R\mathbf{X} + \mathbf{t}. The three vectors x′\mathbf{x}', t\mathbf{t} and RxR\mathbf{x} all lie in one plane, the epipolar plane through the two camera centers and the point. Three coplanar vectors have a zero triple product: x′⊤(t×Rx)=0.\mathbf{x}'^{\top} \big(\mathbf{t} \times R\mathbf{x}\big) = 0 . Write the cross product with t\mathbf{t} as multiplication by the skew-symmetric matrix [t]×[\mathbf{t}]_\times. The constraint becomes x′⊤E x=0,E=[t]×R.\mathbf{x}'^{\top} E\,\mathbf{x} = 0, \qquad E = [\mathbf{t}]_\times R . EE is the essential matrix. It is defined only up to scale, because t\mathbf{t} can be scaled without changing the constraint, so it has five degrees of freedom: three for RR and two for the direction of t\mathbf{t}. A valid essential matrix has two equal singular values and a third that is zero. In pixel coordinates the same constraint reads u′⊤Fu=0\mathbf{u}'^{\top} F \mathbf{u} = 0 with the fundamental matrix F=K−⊤EK−1F = K^{-\top} E K^{-1}. For one point x\mathbf{x} in the first image, ExE\mathbf{x} is a line in the second image, the epipolar line, and the match x′\mathbf{x}' must lie on it.

Estimating E with RANSAC

Each correspondence gives one equation that is linear in the nine entries of EE. Stack eight of them into an 8×98 \times 9 matrix AA and take the null vector of AA (the right singular vector with the smallest singular value). This is the eight-point algorithm. Afterwards you project the result onto the set of valid essential matrices by replacing its singular values with (1,1,0)(1, 1, 0). The five-point algorithm uses the minimum of five correspondences and is what production systems run, but it needs a polynomial solver; eight points keep everything linear (Hartley & Zisserman, 2004). Some matches are wrong, and one wrong match ruins a least-squares fit. RANSAC handles this by repetition. Draw eight random matches, fit EE, and count how many of all the matches agree with it within a threshold. Keep the EE with the most agreement, then refit it on all of its inliers. The agreement test below is the Sampson distance, a first-order approximation of the distance in pixels from a match to its epipolar lines.
Left, view 0 with twelve colored dots on the table and bushes. Right, view 2 with twelve lines of the same colors, each passing through a dot of its color, the matched point. Each match in view 2 sits on the epipolar line of its point in view 0. The lines are nearly parallel and nearly horizontal because the camera moved mostly sideways between the two photos.

Relative pose and triangulation

Factor E=Udiag⁡(1,1,0)V⊤E = U \operatorname{diag}(1, 1, 0) V^{\top} with W=[0−10100001]W = \begin{bmatrix} 0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}. The rotation is R=UWV⊤R = U W V^{\top} or R=UW⊤V⊤R = U W^{\top} V^{\top}, and the translation direction is t=±u3\mathbf{t} = \pm \mathbf{u}_3, the last column of UU (Hartley & Zisserman, 2004). That gives four candidate poses. Only one of them puts the scene in front of both cameras; the other three place the points behind one camera or both. So you triangulate the inliers with each candidate and keep the pose with the most points at positive depth in both views. This is the cheirality check. Triangulation intersects two viewing rays. With projection matrices P=[I∣0]P = [I \mid \mathbf{0}] and P′=[R∣t]P' = [R \mid \mathbf{t}] and the homogeneous point X~\tilde{\mathbf{X}}, the constraint x×PX~=0\mathbf{x} \times P\tilde{\mathbf{X}} = \mathbf{0} gives two independent linear equations per view: (x p3⊤−p1⊤)X~=0,(y p3⊤−p2⊤)X~=0,\begin{aligned} \big(x\,\mathbf{p}_3^{\top} - \mathbf{p}_1^{\top}\big)\tilde{\mathbf{X}} &= 0, \\ \big(y\,\mathbf{p}_3^{\top} - \mathbf{p}_2^{\top}\big)\tilde{\mathbf{X}} &= 0, \end{aligned} where pk⊤\mathbf{p}_k^{\top} is the kk-th row of PP. Stack the four equations of the two views and take the null vector. This is the direct linear transform (DLT). Noise means the two rays do not quite meet, and the DLT returns a point close to both.
One candidate keeps essentially all points in front of both cameras, and the other three keep few or none. The translation has unit length because the essential matrix carries no scale. That unit is the scale of your whole reconstruction, the seventh gauge degree of freedom.

Reprojection error and bundle adjustment

The linear steps above minimize algebraic quantities that have no direct meaning in the image. The quantity you care about is the reprojection error: project each 3D point into every camera that saw it and measure the distance in pixels to the observed keypoint. With the projection π(X)=(X/Z,  Y/Z)\pi(\mathbf{X}) = \big(X/Z,\; Y/Z\big) followed by KK, camera jj predicts point ii at u^ij=K π(RjXi+tj).\hat{\mathbf{u}}_{ij} = K\,\pi\big(R_j \mathbf{X}_i + \mathbf{t}_j\big). Bundle adjustment minimizes the total reprojection error over all poses and points together (Triggs et al., 2000): min⁡{Rj,tj}, {Xi}  ∑(i,j) ∈ Oρ(∥u^ij−uij∥),\min_{\{R_j, \mathbf{t}_j\},\, \{\mathbf{X}_i\}} \;\sum_{(i,j)\,\in\,\mathcal{O}} \rho\Big(\big\lVert \hat{\mathbf{u}}_{ij} - \mathbf{u}_{ij} \big\rVert\Big), where O\mathcal{O} is the set of observations. The name comes from the bundles of rays that leave each camera and are adjusted until they pass through the points. Three details make the problem solvable in practice:
  • Each rotation is stored as an axis-angle vector ω\boldsymbol{\omega}, with R=exp⁡([ω]×)R = \exp([\boldsymbol{\omega}]_\times) given by the Rodrigues formula. Three unconstrained numbers keep RR a rotation during the optimization.
  • ρ\rho is the Huber loss, quadratic for small residuals and linear for large ones, so a few remaining bad matches cannot dominate.
  • The first camera stays fixed at the origin. That removes six of the seven gauge degrees of freedom; the scale stays at the one set by the first two views.
Production solvers use Levenberg-Marquardt with the sparse structure of the Jacobian. Here the problem is small enough for a quasi-Newton method with automatic differentiation.

Two-view initialization

The reconstruction starts from views 0 and 2. Each inlier match becomes a 3D point with two observations, and every observation remembers which keypoint it came from, so later views can find it. Points that triangulate behind a camera, or from rays that are almost parallel, are dropped: a small angle between the rays makes depth very uncertain.

Adding views

Each new photo is registered against the points you already have. Match it to the registered views, and every match whose keypoint already belongs to a 3D point gives a 2D-to-3D correspondence. Finding a camera pose from such correspondences is the perspective-n-point problem (PnP). RANSAC again separates good correspondences from bad ones. Then the matches that do not yet have a point are triangulated against the registered view they match, which adds new points, and bundle adjustment refines all poses and points together. This is incremental structure from motion, the strategy COLMAP uses. Global methods instead estimate all rotations and translations at once from the pairwise relative poses and refine them afterwards (Pan et al., 2024). After each bundle adjustment, observations with a reprojection error above four pixels are removed, so wrong matches that survived RANSAC do not accumulate.
Every new view registers with hundreds of PnP inliers, and the median reprojection error stays well below a pixel. The reconstruction keeps the scale fixed by the first pair. With a long sequence and no loop closure, small errors in each new pose add up, which is called drift; twelve photos on a short arc are too few for it to show.

Comparison with COLMAP

The dataset’s authors reconstructed all 185 photos with COLMAP, which runs the same pipeline in a more careful form (Schonberger2016-xn). Its poses serve as the reference here. Structure from Motion at Scale runs COLMAP itself on these twelve photos and lines up each of its stages with the steps above. Your reconstruction and COLMAP’s live in different gauges, with different origin, orientation and scale. To compare them you find the similarity transform y≈sRx+t\mathbf{y} \approx s R \mathbf{x} + \mathbf{t} that best maps your camera centers xk\mathbf{x}_k onto COLMAP’s centers yk\mathbf{y}_k. The least-squares solution has a closed form (the Umeyama method). Center both sets, take the SVD of their cross-covariance Σxy=UDV⊤\Sigma_{xy} = U D V^{\top}, and set R=USV⊤,s=tr⁡(DS)/σx2,t=yˉ−sRxˉ,\begin{aligned} R &= U S V^{\top}, \\ s &= \operatorname{tr}(D S) / \sigma_x^2, \\ \mathbf{t} &= \bar{\mathbf{y}} - s R \bar{\mathbf{x}}, \end{aligned} where S=diag⁡(1,1,±1)S = \operatorname{diag}(1, 1, \pm 1) makes det⁡R=+1\det R = +1, and σx2\sigma_x^2 is the mean squared distance of the xk\mathbf{x}_k from their centroid.
Left, a top view of the garden reconstruction: gray COLMAP points form the table and the surrounding garden, blue points of your reconstruction cover the same table region, and the 185 COLMAP camera centers form an ellipse around the table with your twelve cameras on a short arc of it. Right, a close-up of the twelve camera centers, where your aligned centers sit on top of COLMAP's. After the similarity alignment your twelve cameras sit on COLMAP’s. The position error is a small fraction of a percent of the distance the cameras moved. Your points are far sparser than COLMAP’s, because twelve photos at one eighth of the resolution see much less, but they lie where COLMAP’s points lie. The two pipelines agree although yours used twelve photos and COLMAP used all 185. The orientation error of each camera is under a degree and almost the same for every camera. That shared part comes from the alignment, not from the cameras: the twelve centers lie close to one arc, which pins down the rotation of the similarity transform less tightly than the cameras pin down each other. The rotation between consecutive cameras does not depend on the alignment, and there your reconstruction and COLMAP’s agree to a few hundredths of a degree.

What the reconstruction hands on

The three files are the output of structure from motion, and Structure from Motion at Scale describes their format and conventions. They are also the input of the two radiance field methods in this chapter:
  • cameras.bin: the intrinsics KK of each camera model.
  • images.bin: for every photo, its rotation (as a quaternion) and translation, plus the keypoints it contributed.
  • points3D.bin: the sparse points, each with a color, a mean reprojection error and the list of photos that saw it.
Neural Radiance Fields and Gaussian Splatting both read these files for the same garden scene. Both use the poses to turn each pixel into a ray in the scene. Gaussian splatting also uses the sparse points: it places one Gaussian at each of them as its starting scene, and the optimization grows the rest.

References

  • Hartley, R., Zisserman, A. (2004). Multiple View Geometry in Computer Vision.
  • Pan, L., Baráth, D., Pollefeys, M., Schönberger, J. (2024). Global Structure-from-Motion Revisited. arXiv [cs.CV]. Link
  • Triggs, B., Mc Lauchlan, P., Hartley, R., Fitzgibbon, A. (2000). Bundle Adjustment — A Modern Synthesis. Plan. Perspect. Link

Found an error on this page? Report an issue.