Depth from CW-ToF¶
A CW-ToF camera measures depth from the phase of its modulated light. This page renders the four phase-shifted
measurements of the bunny scene with CWToFPathTracerInline, turns them into depth, and compares naive and
antithetic sampling at equal time.
Four phases¶
With the light at the camera, the path length of the direct light is twice the depth \(d\). A measurement with the modulation \(\cos(2\pi\,\ell / \lambda - 2\pi\phi)\) at the phases \(\phi = 0, 1/4, 1/2, 3/4\) gives \(I_0, I_1, I_2, I_3\), and the phase of the path length is
The constant part of the light (the albedo, the distance falloff) cancels in the differences. The indirect light does not, and it biases \(\theta\): this is the multipath error of CW-ToF cameras.
def capture(wavelength, method, seconds=None, frames=None):
images, spp = [], 0
for phase in PHASES:
properties = {**method, "modulationWavelength": wavelength, "phase": phase}
if frames is not None:
properties["samplesPerPixel"] = 16
image, count = render(create_graph(properties), seconds, frames)
images.append(image)
spp += count * properties.get("samplesPerPixel", 1)
return images, spp // len(PHASES)
# 5. Phase to depth
def depth_from_phases(images, wavelength, coarse_depth):
"""The phase gives the path length l (twice the depth: the light is at the camera)
up to multiples of the wavelength; np.unwrap picks the multiple closest to a coarse
depth, as from a lower modulation frequency or another sensor."""
i0, i1, i2, i3 = images
phase = np.arctan2(i1 - i3, i0 - i2) # 2 pi l / wavelength, wrapped to (-pi, pi]
coarse_phase = 2.0 * np.pi * (2.0 * coarse_depth) / wavelength
phase = np.unwrap(np.stack([coarse_phase, phase]), axis=0)[1]
return phase / (2.0 * np.pi) * wavelength / 2.0
# Here the coarse depth is the true depth: it only picks the multiple, and the
# measured phase still decides the depth within a quarter wavelength of it.
coarse = true_depth
results = []
long_images, spp = capture(1.0, NAIVE, frames=CONVERGED_FRAMES)
results.append((f"$\\lambda$ = 1, converged ({spp} spp)", depth_from_phases(long_images, 1.0, coarse)))
for name, method in [("naive", NAIVE), ("antithetic", ANTITHETIC)]:
images, spp = capture(0.02, method, seconds=SECONDS)
results.append((f"$\\lambda$ = 0.02, {name} ({spp} spp)", depth_from_phases(images, 0.02, coarse)))
\(\theta\) gives the path length only up to multiples of \(\lambda\). A real camera resolves them with a second, lower
modulation frequency; here np.unwrap picks, for every pixel, the multiple closest to a coarse depth, taken from the
true depth so that only the measured phase sets the depth within a quarter wavelength.
Wavelength and precision¶
A short wavelength measures depth more precisely, since a given error in \(\theta\) is a smaller distance, and it suppresses the multipath error, since the indirect light cancels more. The figure compares:
\(\lambda = 1\), converged (4,096 samples per pixel per phase): the depth is off by a few millimeters, in rings that follow the indirect light, the largest along the edges of the box;
\(\lambda = 0.02\), 0.02 seconds per phase: the median error drops below 0.1 mm. Naive sampling (28 samples per pixel) leaves noise on the walls, where the light bounces between them; antithetic sampling (18 samples per pixel, with the barycentric shift) cancels it.
Measurement |
Samples per pixel |
Median depth error |
|---|---|---|
\(\lambda = 1\), converged |
4,096 |
2.7 mm |
\(\lambda = 0.02\), naive, 0.02 s per phase |
28 |
0.039 mm |
\(\lambda = 0.02\), antithetic, 0.02 s per phase |
18 |
0.021 mm |
(Scene units are taken as meters.)
The script¶
The render graph is that of CW-ToF rendering with computeDirect on; the true
depth comes from the world positions of GBufferRT.
The full script: cwtof_depth.py. Run it next to the unzipped
cornell-box-bunny-diffuse folder; it takes about a minute.