CS180 — Project 4: Neural Radiance Fields
In this project, we build an end-to-end nerf pipeline that is capable of reconstructing a 3D view of an object from a series of 2D pictures.
Part 0: Calibration, Scanning, and Dataset Packaging
Before training a NeRF, we need accurate camera poses for each input image. To obtain these, we use ArUco marker-based calibration. ArUco tags are fiducial markers with known geometry that can be reliably detected in images. By placing a printed ArUco board in the scene and detecting the markers across multiple frames, we can solve the Perspective-n-Point (PnP) problem to recover the camera's intrinsic parameters (focal length, principal point, distortion coefficients) and extrinsic parameters (rotation and translation relative to the board).
The PnP problem estimates the pose of a calibrated camera given a set of 3D-to-2D point correspondences. OpenCV's cv2.aruco module handles marker detection, and cv2.calibrateCamera jointly optimizes intrinsics and per-frame extrinsics. From the resulting rotation vectors and translation vectors, we construct a camera-to-world (c2w) matrix for each frame, which is the representation NeRF expects.
Part 1: Neural Field for a 2D Image
Before tackling the full 3D NeRF, we first build a simpler 2D neural field that learns to map pixel coordinates (u, v) to RGB color values. This serves as a warm-up to understand the key components: positional encoding, network architecture, and training loop.
A naive MLP that takes raw (u, v) coordinates as input struggles to capture high-frequency details in the image. This is because neural networks have a spectral bias toward learning low-frequency functions. To overcome this, we use positional encoding, which maps low-dimensional input coordinates into a higher-dimensional space using sinusoidal functions at multiple frequencies. This allows the network to represent fine details and sharp edges.
The network architecture is a simple MLP with sinusoidal positional encoding on the input coordinates. We use a learning rate of 1e-2 with the Adam optimizer and train for 2000 iterations.
Cat Image Training Progression
Emir Image Training Progression
PSNR Curve
Hyperparameter Sweep
We conduct a hyperparameter sweep over different positional encoding frequencies and network widths to understand how these choices affect the quality of the learned 2D neural field.
Part 2: Multi-view Neural Radiance Field on Lego
Now we extend from 2D to 3D. A Neural Radiance Field (NeRF) represents a scene as a continuous volumetric function that maps a 3D position (x, y, z) and viewing direction (theta, phi) to a color (r, g, b) and volume density (sigma). By querying this function along camera rays and applying volume rendering, we can synthesize novel views of the scene.
The camera-to-world (c2w) matrix is a 4x4 transformation that converts points from camera coordinates to world coordinates. Given pixel coordinates and camera intrinsics, we can compute ray origins and directions in world space. For each pixel, we cast a ray and sample points along it.
We implement a dataloader that randomly samples rays from the training images. For each ray, we sample points along it using stratified sampling, adding uniform noise to the sample positions to prevent aliasing. The sampled 3D coordinates are passed through positional encoding and fed into the MLP.
Volume rendering is the process of accumulating color and density along each ray to produce a final pixel color. For each sample point along a ray, the network predicts a color and density. The final pixel color is computed as a weighted sum of the sample colors, where the weights depend on the accumulated transmittance (how much light has been absorbed so far) and the density at each point.
Network Architecture
Ray Visualization
Lego Training Progression
Lego PSNR Curve
Lego 3D Render
Custom Dataset: Stadium
We also train NeRF on our own captured dataset of a stadium scene, using the camera poses obtained from the ArUco calibration in Part 0.
Stadium Training Progression
Stadium PSNR Curve
Stadium 3D Render