Assignment 4: Lens Simulator

Cecilia Zhang

In this project, I extented my ray tracer with pinhole camera to more realistic camera modeling with lens stacks. This more realistic modeling allows me to programmatically create depth of field, simulating defocus blur and inspecting on how lenses with different focal lengths affect the perspective of the scenes.

A lens tester is also created to simulate ray paths through different lens stacks. This simulation allows for straightforward visualization of how convexity of lenses affect the rays' traces.

Part 1: Tracing Rays through Lenses

Real camera and pinhole camera

Pinhole camera has no lens but a single small aperture, while real camera has a lens stack with finite aperture. For a pinhole camera,
1) the pinhole is usually smaller than a single pixel size; no defocus blur or depth of field can be observed, since the entire scene is in focus.
2) the image has the same brightness since each sensor pixel is hit by approximately the same amount of light rays.
3) since no depth information can be captured by pinhole cameras, no perspective of the scene can be observed either.
While for a real camera,
1) we are able to observe depth of focus since lenses with different focal lengths focus on different depths of the scene. Objects that are not at the focus depth of the scene will appear blurred.
2) the effect of vignette can be observed since rays hit the boundary of the lenses have larger chance to be missed, and thus resulting in darker appearance towards image boundaries.
3) since there is depth information captured by real cameras, cameras with different focal lengths can also result in different perspectives of the scene.

Lens tester

Below I show the screenshots of my lens tester that simulates ray paths through the following four different types of lenses. The overall pipeline is to check whether a ray intersects with a lens element, and if there is an intersection point, calculate its refraction and keep tracing to the next lens element.
lens tester for the 4 types of lenses: lens 1 is D-GAUSS F/2 22deg HFOV; lens 2 is Wide-angle (38-degree) lens. Nakamura; lens 3 is SIGLER Super achromate telephoto, EFL=254mm, F/5.6; lens 4 is Muller 16mm/f4 155.9FOV fisheye lens

Lens properties

The table shows the focal length, infinity focus sensor depth, close focus distance in front of the lens, and close focus sensor depth for the 4 types of lenses. We can verify from these numbers (near focus to be the right of infinity focus) that real lenses have a closest possible object depth that their focus mechanisms can achieve.

For infinity focus, I trace parallel rays (infinitely far away on the optical axis) through lens and find their intersection points with the optical axis. To calculate the focal length, I used a thick-lens approximation to get its principal planes.

For near focus, I trace a ray from closest object focus depths that approximately equals 5 times the focal length. And then find their intersections with the optical axis.

lens properties in terms of their focal length, infinity focus sensor depth, close focus distance and close focus sensor depth

Inverse relationship

Below, for each of the 4 types of lenses, I plot sensor depth vs. conjugate to show the their inverse relationship. The sensor depth is calculated from 100 evenly spaced sensor depths ranging from close focus to infinity.
sensor depth vs. conjugate for the 4 types of lenses

Manual focus

Now I'm able to change the focus of the scene, and am able to manually focus on object of interests. Below I show the dragon rendering at: sensor plane moved to 58.2522 mm (sensor-side), focus at -406.033 mm (object-side), with 1024 samples per pixel and 100 bounces.

high-res rendering of dragon with depth of focus effect using lens D-GAUSS
high-res rendering of lucy with depth of focus effect using lens D-GAUSS

Cosine factor

To ensure enough amount of rays being able to trace through lens stack, I give each ray 5 'chances' to go through the lens, and then normalize the returned spectrum accordingly. Then I also considered the cosine factor to be included when calculating spectrum. With adjusted normalizing, I'm able to simulate the vignetting effect of real camera lens. This effect (darker at image boundaries) can be observed in my rendered images shown above.

Part 2: Contrast-based autofocus

Focus metric --- image variance

In order to evaluate image quality, I used image variance as the simple focus metric (equation shown below). Blurrier image patches will have lower variance while sharper ones will have higher variance. This could be intuitively understand in that blurrier image patches have more uniformly distributed pixel values (no edge-like peak pixel values). Sharper image patches usually contain more significant differences within a single patch (e.g. a sharp edge induces a big pixel gradient). Therefore the variance would be higher. All these above analysis is based on relatively low-noise rendering. If the rendering is noisy, the variance would be high due to noise and thus be meaningless.

I calculated the variance for each of the r, g, b channel and sum them up.

Autofocus heuristic

A linear search is performed in order to get auto-focus. Ranging from near focus depth to infinity focus depth, there should be a depth that produces the highest image patch variance, which produces the highest image quality based on the focus metric we used. The step size is chosen to ensure image point to be blurred within one output pixel.

compute the circle of confusion

Focus metric vs. sensor depth

I picked the dragon scene and selected a patch to test the relationship between sensor depth and focus metric.

(Left) the curve that shows the relationship between sensor depth and image patch variance (Right) the rendered dragon scene with only the image patch be in focus (auto-focus)

High quality rendering

I rendered a high-quality image of the dragon scene using auto-focus for each of the 4 lenses. They are shown below:

High quality rendering for lens 1 and lens 2 for the dragon scene

High quality rendering for lens 3 and lens 4 for the dragon scene