Event camera (offline)¶
This tutorial simulates an event camera moving forward through the Cornell box at 1000 frames per second, and renders the brightness change between two frames with 1024 samples per pixel per frame, two ways:
Path tracer: each frame with its own random numbers (
EventDifference,sampling = independent).Correlated path tracer: each frame rendered twice, once with the previous frame’s random numbers, so that the difference compares renders with the same random numbers (
sampling = correlated; see Correlated sampling).
It uses the scene described in Event camera rendering.
1. Load the scene¶
The camera moves 2 mm per frame along its view direction. Events fire when the brightness changes by the threshold \(C = 0.2\).
import falcorcomp as falcor
import numpy as np
import matplotlib
matplotlib.use("Agg") # no window
import matplotlib.pyplot as plt
testbed = falcor.Testbed(create_window=False)
testbed.load_scene("cornell-box/scene-v4.pbrt") # with its area light
testbed.resize_frame_buffer(512, 512)
testbed.scene.camera.aspectRatio = 1.0
testbed.clock.pause()
# The camera moves forward at 1000 frames per second.
camera = testbed.scene.camera
START = np.array([camera.position.x, camera.position.y, camera.position.z])
FORWARD = np.array([camera.target.x, camera.target.y, camera.target.z]) - START
FPS = 1000
THRESHOLD = 0.2 # event threshold C on the brightness change
def move_camera(frame):
position = START + 2.0 * FORWARD * frame / FPS
camera.position = falcor.float3(*position)
camera.target = falcor.float3(*(position + FORWARD))
2. Build the render graph¶
GBufferRT finds the primary hits at the pixel centers; one PathTracer (independent) or two (correlated) render
from them. EventDifference keeps the previous frame and outputs the image, \(\Delta I\) and \(\Delta L\);
EventGenerator turns \(\Delta L\) into events. PathTracer renders at most 16 samples per pixel, so
EventDifference averages several executions into one frame (subframes).
def build_graph(correlated, subframes):
graph = testbed.create_render_graph("correlated" if correlated else "independent")
# Primary hits at the pixel centers, shared by the path tracers.
graph.create_pass("GBuffer", "GBufferRT", {"samplePattern": "Center", "sampleCount": 1})
tracers = ["TracerA", "TracerB"] if correlated else ["TracerA"]
for tracer in tracers:
graph.create_pass(tracer, "PathTracer", {"samplesPerPixel": 16, "fixedSeed": 0})
graph.add_edge("GBuffer.vbuffer", f"{tracer}.vbuffer")
# The difference of each frame with the previous one, averaged over `subframes` executions per frame.
graph.create_pass("Difference", "EventDifference",
{"sampling": "correlated" if correlated else "independent", "subframes": subframes})
for index, tracer in enumerate(tracers):
graph.add_edge(f"{tracer}.color", f"Difference.color{index + 1}")
graph.create_pass("Events", "EventGenerator", {"mode": "probabilistic", "threshold": THRESHOLD})
graph.add_edge("Difference.deltaL", "Events.deltaL")
for output in ["Difference.primal", "Difference.deltaI", "Difference.deltaL", "Events.events"]:
graph.mark_output(output)
return graph
3. Render two frames¶
Before every execution, the script sets the seeds of the path tracers, which fix their random numbers. With
correlated sampling, TracerA uses the previous frame’s seed of the same execution and TracerB a new one, so
every render of frame 1 has a partner in frame 0 with the same random numbers. The difference needs only the
previous frame, so two frames are enough.
def render(correlated, samples):
tracers = 2 if correlated else 1
subframes = samples // (16 * tracers) # 16 samples per PathTracer execution
graph = build_graph(correlated, subframes)
testbed.render_graph = graph
for frame in [0, 1]: # the difference only needs the previous frame
move_camera(frame)
for subframe in range(subframes):
# Seed of frame f in execution k; seeds are non-negative and never repeat.
seed = lambda f: (f + 1) * subframes + subframe
if correlated:
# TracerA reuses the previous frame's seed, TracerB takes the current one.
graph.get_pass("TracerA").fixedSeed = seed(frame - 1)
graph.get_pass("TracerB").fixedSeed = seed(frame)
else:
graph.get_pass("TracerA").fixedSeed = seed(frame)
testbed.frame()
luminance = np.array([0.2126, 0.7152, 0.0722])
return {
"primal": graph.get_output("Difference.primal").to_numpy()[..., :3],
"deltaI": graph.get_output("Difference.deltaI").to_numpy()[..., :3] @ luminance,
"deltaL": graph.get_output("Difference.deltaL").to_numpy(),
"events": graph.get_output("Events.events").to_numpy(),
}
SAMPLES = 1024 # per pixel per frame
results = {
"Path tracer\n(independent, 1024 spp)": render(False, SAMPLES),
"Correlated path tracer\n(512 + 512 spp)": render(True, SAMPLES),
}
4. Compare¶
Both rows render 1024 samples per pixel per frame, and their images look the same. Their differences do not:
Path tracer: \(\Delta I\) is the difference of two independent noisy images, and at 1024 samples per pixel the noise still hides the change, most of all on the dark boxes in \(\Delta L\); events fire all over the image (0.18 per pixel).
Correlated path tracer: the noise of the two renders cancels, and \(\Delta I\) shows the change: along the edges of the walls, the boxes and the light, and a slow change of the walls’ shading. Events fire mostly along the edges and the light (0.013 per pixel).
correlated = list(results.values())[1]
range_I = np.percentile(np.abs(correlated["deltaI"]), 99.5)
range_L = np.percentile(np.abs(correlated["deltaL"]), 99.5)
figure, axes = plt.subplots(len(results), 4, figsize=(13, 3.4 * len(results)))
for row, (name, result) in enumerate(results.items()):
panels = [
(np.clip(result["primal"], 0, 1) ** (1 / 2.2), None, None, "primal"),
(result["deltaI"], "RdBu_r", range_I, "ΔI"),
(result["deltaL"], "RdBu_r", range_L, "ΔL"),
(np.clip(result["events"], -1, 1), "bwr", 1, "events (red +, blue −)"),
]
for column, (image, cmap, limit, title) in enumerate(panels):
axis = axes[row, column]
axis.imshow(image, cmap=cmap, vmin=-limit if limit else None, vmax=limit)
axis.set_xticks([])
axis.set_yticks([])
if row == 0:
axis.set_title(title)
axes[row, 3].text(0.02, 0.97, f"{np.mean(np.abs(result['events'])):.4f} events / pixel",
transform=axes[row, 3].transAxes, va="top", fontsize=9,
bbox=dict(facecolor="white", alpha=0.8, edgecolor="none"))
axes[row, 0].set_ylabel(name)
figure.tight_layout()
figure.savefig("event_camera.png", dpi=80)
The full script: event_camera_offline.py. It takes about 30 seconds.
Rendering at a few samples per pixel, as for long sequences, needs denoising: see
Event rendering with denoising.