From 2D Images to 3D Worlds: The Linear Algebra of Structure from Motion
Bach Pham
Abstract
This paper explores the essence of Structure from Motion (SfM), a technique in computer vision that can reconstruct 3D scenes from some 2D images with good overlap. The paper aims to apply linear algebra, especially Singular Value Decomposition (SVD), to deeply explore the mathematics behind SfM. I focus mostly on the Fundamental Matrix and the Eight-Point Algorithm. Specifically, I demonstrate how geometric constraints are transformed into a homogeneous system of linear equations () and prove why the Singular Value Decomposition provides the optimal solution to this problem.
Introduction: The Inverse Problem
Sometimes we only have a handful of images of a scene, but we really want to know what the whole scene looks like. People have thought of combining images to create panoramas using feature matching, but this is still just a larger image. We want to move from a flat 2D image to a 3D structure.
In the 1970s, structure from motion was developed. It has been the ground to many modern techniques to reconstruct 3d scene, especially all kinds of Neural Radiance Field methods (NeRFs) and Gaussian Splatting.
In rendering (forward problem), we project 3D coordinates onto a 2D plane, which causes the loss of depth information. On the other hand, Structure from Motion (SfM) tries to reverse this by using the parallax effect which is the relative motion of points between different viewpoints.
A prominent implementation of this is Google Map. It has been using SfM all the time until 2023 when Google released the "immersive view" option on Google Maps which uses another technology built on top of SfM: NeRF. The NeRF paper was released in 2020 by Ben Mildenhall, Pratul P. Srinivasan, Matthew Tancik, Jonathan T. Barron, Ravi Ramamoorthi, and Ren Ng. Later on in 2024, Google released a research paper about a hybrid technology (combining the high quality of NeRF and the ultra fast rendering of Gaussian Splatting): RadSplat
The SfM pipeline generally consists of:
- Feature Matching (SIFT) and Outlier Rejection (RANSAC).
- Estimating the Fundamental Matrix () (The focus of this paper).
- Estimating the Essential Matrix ().
- Triangulation and Bundle Adjustment.
Epipolar Geometry
Before we get to the Fundamental Matrix, we have to know what Epipolar geometry is.
The Coplanarity Constraint
Consider a point in 3D space viewed by two cameras centered at and .
- Let be the projection of onto the first image.
- Let be the projection of onto the second image.
As illustrated in Figure 1, the points , , and form a triangle. Consequently, the vectors , , and must lie on the same plane, called the epipolar plane. And since they are on the same plane, their scalar triple product is zero:
Epipoles and Epipolar Lines
If we know a point in the above image, we cannot determine its exact depth. However, the ray from through looks like a line when viewed from the second camera. This is the Epipolar Line (). The matching point must lie somewhere on this line. This constraint significantly reduces the search for matches because we just need to search on a line and do not need to search for the entire image anymore.
The Fundamental Matrix
The Fundamental Matrix is the algebraic representation of epipolar geometry. It is a matrix. The relationship between a point in image 1, , and a point in image 2, , is given by the epipolar constraint:
How does this connect to the three arrows in Figure 1? We started with two viewing rays and the line joining the cameras:
We will rewrite this same equation using pixel coordinates. Each step below replaces one part of the notation. We follow one matched point, so we leave out the subscript for now.
1. Connect the pixels to the three arrows
A pixel point and a viewing ray are different things. stores a location in the image. The arrow points from the camera through that location into the 3D scene.
The camera's intrinsic matrix describes how its focal lengths and principal point convert a ray direction into pixel coordinates. Its inverse converts a pixel point back into the direction of its viewing ray. In each camera's own coordinate frame:
So gives the direction of the arrow from through , and gives the direction of the arrow from through . These are the two viewing rays from Figure 1, each expressed using its own camera's axes.
Now describe all three arrows using camera 2's axes. The relative rotation rotates directions from camera 1's axes into camera 2's. The translation is the position of camera 1's center in this second frame. Camera 2's center is at the origin.
- First viewing ray: has direction . This is the same ray direction as before, now expressed using camera 2's axes.
- Baseline: becomes . Camera 1 is at and camera 2 is at zero, so this arrow points opposite to .
- Second viewing ray: has direction . It is already expressed in camera 2's axes.
Substitute those three directions into the earlier equation. They lie in the same plane, so their triple product is zero:
Next, move the second ray to the front. Reversing a cross product changes its sign, and cycling the three vectors in a scalar triple product leaves its value unchanged:
We now have the second ray dotted with a normal to the plane containing the translation and first ray. This is still the original coplanarity condition.
Check this using the rotation and translation equation
Let and be the coordinates of the same scene point in the two camera frames. With the convention above:
A ray gives a direction but not the point's depth. For unknown depth scales and , substitute:
The second ray is a linear combination of the rotated first ray and the translation. All three therefore lie in one plane, exactly as the triple-product equation requires.
2. Group the camera motion into E
The next change is just algebra: replace the cross product with a matrix multiplication. For , define:
Multiplying by this matrix does the same thing as crossing with . In our equation:
A dot product can also be written as a transposed vector times a column. Make those two replacements, then give the middle pair of matrices the name :
This is the essential matrix. It contains the rotation and translation, and it acts on the normalized ray coordinates.
3. Include the pixel conversion in F
We started with pixel matches, so put and back into the equation we just obtained:
Transposing a product reverses its order. This explains the inverse transpose on the left:
Now group everything between the two pixel points into one matrix, called :
This is the equation we wanted. The first equation used three arrows in 3D. The last equation uses two pixel points. Both say that the viewing rays and the baseline lie in one plane.
- groups the camera motion and works with normalized coordinates.
- also includes the pixel conversions, so it works directly with pixel coordinates.
Where does the epipolar line fit?
Look at the final equation in two steps. First compute . Its three entries are the coefficients of the epipolar line in the second image:
Then multiply by the matching pixel point. The result is exactly the familiar equation for a point lying on a line:
We used camera motion to explain the form of . In practice, we can estimate its entries from pixel matches without first knowing the motion or calibration. The next section turns each match into one equation in those nine unknown entries.
Deriving the System of Equations ()
To find , we need to solve for its 9 entries. We can write the matrix product explicitly for a single pair of points:
First multiply the matrix by the column. Each row multiplies the column entry by entry, then adds the three products:
Now multiply by the row on the left. The first entry gets multiplied by, the second by , and the third by 1:
Distribute those multipliers to get nine terms, one for each entry of. We order the unknowns column by column: first, then the second column, then the third. Reordering a sum does not change its value:
The image coordinates are known; the nine entries of are unknown. Collecting their coefficients gives one row of :
Check the multiplication with numbers
Take the following matrix and points. This is an arithmetic check; the points were not chosen to satisfy the epipolar constraint.
Multiplying the matrix and vectors gives:
Using the nine terms in column order gives the same result:
The two calculations agree. The nonzero result means this point pair does not satisfy this matrix's epipolar constraint.
If we have correspondences (where ), we can stack these linear equations into a large matrix system. By defining a vector that contains the entries of the matrix , we can rewrite the system as:
This can be written compactly as .
Multiplying every entry of by the same nonzero scale leaves the constraint unchanged. Its nine entries therefore have eight independent parameters before enforcing rank 2. The linear eight-point method needs at least eight independent equations from matches in a nondegenerate configuration.
With exactly eight independent rows, the eight-by-nine matrix has a nonzero null vector even when the image coordinates contain noise. With more noisy correspondences, an exact nonzero solution generally does not exist. We then find the that makes the result as close to zero as possible. We also fix the length of to 1 so the solution isn't just a vector of all zeros.
Mathematical Proof: Solving via SVD
The proof below uses for a generic unknown vector. In our application, that vector is , the nine entries of , not an image point. The last column of the full matrix gives the required direction. With exactly eight rows, we can append a zero row to for the proof. This leaves the error norm unchanged and makes the ninth, zero singular value explicit.
Understand SVD geometrically
We have already known that . Geometrically, multiplying a vector by happens in three steps:
- Rotate (): The matrix is orthogonal. It takes the input vector and rotates it to a new coordinate system.
- Stretch (): The matrix is diagonal. It stretches the vector along the coordinate axes. The amount of stretching is determined by the singular values . When there are more rows than columns, any extra zero rows of are omitted from the stretching diagram below.
- Rotate (): The matrix is orthogonal. It rotates the stretched vector one final time.
We want the final result to be as small as possible.
Proof
Step 1: Ignore the Final Rotation () We have:
The matrix is orthogonal. A property of orthogonal matrices is that they act like rotation or reflection matrix. That means they change the direction of a vector but not the length. Therefore, for any vector , .
Step 2: Change Variables. Let's define a temporary vector . Since is orthogonal, is also orthogonal. This means just rotates . Since we are constrained to unit vectors (), and rotation preserves length, our new vector is also a unit vector ().
Our problem is now much simpler: Minimize .
Step 3: Analyze the Stretching ()
The squared length of this resulting vector is the sum of its squared components:
We want to make this sum as small as possible. We know the singular values are sorted in descending order: is the largest (maximum stretch) and is the smallest (minimum stretch). We also know that (because is a unit vector).
To get the smallest total sum, we should put all of our weight into the term multiplied by the smallest number.
- If we choose , the result is (The Maximum).
- If we choose , the result is (The Minimum).
Therefore, the optimal vector is all zeros, except for a 1 in the last position:
Step 4: Convert back to . We found the best , but we need to find the original . We defined . Therefore, x is
By the rules of matrix-vector multiplication, this operation selects the last column of the matrix .
Conclusion: The vector that minimizes the error is the last column of .
Rank Enforcement
After using the theorem above, we have a solution for and reshape it into a matrix . However, we have one problem.
The Rank 2 Constraint
Return to the formula we derived from rotation and translation:
For nonzero translation, crossing a vector parallel to with gives zero. No other direction maps to zero, so the cross-product matrix loses exactly one dimension and has rank 2. The rotation and intrinsic matrices are invertible, so multiplying by them preserves that rank:
This is why one singular value must be zero. It comes from the camera geometry, not an arbitrary adjustment to the matrix. Pure rotation, with zero translation, is a degenerate case: there is no baseline to recover depth from parallax.
However, because our input images have noise (pixels aren't perfect), the we estimated from the point matches will likely be Rank 3 (Full Rank). It will satisfy the algebraic least squares, but it won't satisfy the strict geometry (the epipolar lines won't intersect perfectly at one point).
The Eckart-Young-Mirsky Theorem
We need to find a new matrix that is Rank 2 but is as close as possible to our calculated . The Eckart-Young-Mirsky theorem tells us exactly how to do this:
- Decompose: Take the SVD of our noisy matrix : .
- Inspect : The diagonal matrix will look like this:Because of noise, will be a small non-zero number (e.g., 0.005). This tiny number is what makes the matrix Rank 3.
- Truncate: We manually set the smallest singular value to zero.
- Reconstruct: We multiply the matrices back together: .
This is our second use of SVD. The first finds the nine entries that best fit the linear equations. The second enforces the rank required by the camera geometry. If the cameras are calibrated, we can use the earlier relation with the corrected matrix to estimate the essential matrix:
This result, , is the Fundamental Matrix used in Structure from Motion. Of course this is just the very basic idea. We then need to use RANSAC again for outlier rejection to find best F, then estimate the essential matrix, then the triangulation, then PnP, and a lot of optimizations. However, I will not include those in this paper because we only focus on the core math, not the programming aspect.
Conclusion
SfM is a very interesting application of Linear Algebra. The math behind is so beautiful, especially for the minimization problem. It is also very convenient when we can just decompose, force an entry in the diagonal matrix to be zero, and multiply everything back. After taking linear algebra course, I finally understand deeply what is happening behind the libraries I imported in my code.
References
[1] University of Maryland. (2022). CMSC733 Project 3: Build your own SfM! Retrieved from https://cmsc733.github.io/2022/proj/p3/