Focus, defocus and focal stacks
Frédo Durand
MIT EECS
Depth of field
Misfocus
•Points too far focus too close and project into a circle
–called circle of confusion
lens
focal
plane
Misfocus
•Points too far focus too close and project into a circle
–called circle of confusion
lens
focal
plane
Depth of focus: more accurate view
•Points too far focus too close and project into a circle
•Points too close focus too far and project into a circle
•When the circle exceeds a given size, we declare the point out of focus
lens
focal
plane
Depth of focus: more accurate view
•Points too far focus too close and project into a circle
•Points too close focus too far and project into a circle
•When the circle exceeds a given size, we declare the point out of focus
lens
focal
plane
Depth of focus: more accurate view
•Max circle of confusion can be achieved in two ways
–point that focuses too close, or too far
•The distance between those points is the depth of focus
–Conjugate of depth of field
Depth
of focus
lens
focal
plane
Depth
of field
Bottom line
•Depth of field is increased by using a smaller aperture
•But lets in less light =>noise
•And also diffraction gets worse
•Can only be done up to a point
•Especially challenging for macro photography
•Bottom light: even at smallest possible aperture,
often not enough depth of field
Depth of field calculation
Depth of focus: more accurate view
•Goal: compute depth of field
lens
Depth of focus: more accurate view
•Goal: compute depth of field
–distance d1 and d2 in front and behind the focal plane
–given max circle of confusion
lens
Depth
of field
d1
d2
Depth of focus: more accurate view
•Goal: compute depth of field
–distance d1 and d2 in front and behind the focal plane
–given max circle of confusion
•Solution: similar triangles
lens
Depth
of field
d1
d2
Depth of focus: more accurate view
•Circle of confusion C,
•Focusing distance D, focal length f, aperture N
lens
d1
d2
D
~f
C
Depth of focus: more accurate view
•Backproject the circle of confusion diameter C onto the focal plane
–Depends on magnification factor ~ f/D
lens
CD/f
D
~f
C
d1
d2
f/N
Depth of focus: derivation
lens
CD/f
D
~f
C
d1
d2
f/N
Depth of focus: more accurate view
lens
CD/f
D
~f
C
d1
d2
Deriving depth of field
CD/f
f/N
D
d1
d2
Deriving depth of field
CD/f
f/N
D
d1
d2
N2C2D2 term can often be neglected when DoF is small
(conjugate of circle of confusion is smaller than lens aperture)
Depth of field and aperture
CD/f
f/N
d1
d2
Depth of field and aperture
•Linear: proportional to f number
•Recall: big f number N
means small physical aperture
CD/f
f/N
d1
d2
SLR viewfinder & aperture
•By default, an SLR always shows you the biggest aperture
•Brighter image
•Shallow depth of field help judge focus
•Depth of field preview button:
–Stops down to the aperture you have chosen
–Darker image
–Larger depth of field
Depth of field and focusing distance
•Quadratic (bad news for macro)
(but careful, our simplifications
are not accurate for macro)
f/N
d1
d2
D
CD/f
Depth of field & focusing distance
From Photography, London et al.
Hyperfocal distance
From Basic Photographic Materials and Processes, Stroebel et al.
Hyperfocal distance
•Recall full formula
CD/f
f/N
d1
d2
CD/f
Hyperfocal distance
•When CD/f becomes bigger than f/N
•focus at D=f2/NC and sharp from D/2 till infinity
•Our other simplifications do not work anymore there: the denominator term has to be taken into account in
CD/f
f/N
d1
d2
CD/f
Depth of field and focal length
•Inverse quadratic:
the lens gets bigger, 
the magnification is higher
CD/f
f/N
D
d1
d2
Depth of field & focal length
•Recall that to get the same image size,
we can double the focal length and the distance
•Recall what happens to physical aperture size when we double the focal length for the same f number?
–It is doubled
24mm
50mm
•Same image size (same magnification),
same f number
•Same depth of field!
Depth of field & focal length
Wide-angle
lens
Telephoto
lens (2x f), same aperture
DoF
DoF
DoF & Focal length
50mm f/4.8
200mm f/4.8
(from 4 times farther)
Important conclusion
•For a given image size and a given f number, the depth of field (in object space) is the same.
–Might be counter intuitive.
–Although only true if assumption respected
(far from hyperlocal distance)
•Very useful for macro where DoF is critical. You can change your working distance without affecting depth of field
•Now what happens to the background blur far far away?
Important conclusion
•For a given image size and a given f number, the depth of field (in object space) is the same.
–The depth of acceptable sharpness is the same
•But background far far away looks more blurry
Because it gets magnified more
•Plus, usually, you don't keep magnification constant
To learn more
Figure 2. The circle of confusion versus distance at a fixed image magnification for a range of focal lengths.
Figure 3. A: Gromit photographed with a 100-mm lens at f/4. B: Gromit photographed with a 28-mm lens at f/4. DOF is the same.
Same object space blur
Figure 4. Background crop from the photographs in Fig. 3. A: 100-mm lens. B: 28-mm lens.
Recap
Effect of parameters
From applied photographic optics
aperture
focusing distance
focal length
DoF guides
From "The Manual of Photography" Jacobson et al
Is depth of field good or evil?
•It depends, little grasshopper
•Want huge DoF: landscape,
photojournalists, portrait with
environment
•Shallow DoF: portrait, wildlife
Steve McCurry
Michael Reichman
Crazy DoF images
•By Matthias Zwicker
•The focus is between the two sticks
Sharp version
Really wide aperture version
Is depth of field a blur?
•Depth of field is NOT a convolution of the image
•The circle of confusion varies with depth
•There are interesting occlusion effects
•(If you really want a convolution, there is one, but in 4D space… more about this in ten days)
From Macro Photography
Sensor size
Depth of field
•It’s all about the size of the lens aperture
lens
sensor
Point in focus
Object with texture
lens
sensor
Point in focus
Object with texture
Equation
•Smaller sensor
–smaller C
–smaller f
•But the effect of f is quadratic
•Bottom line: the size of the sensor linearly scales the depth of field
–for same field of view, focusing distance and f number (aperture).
The coolest depth of field solution
lens
diffuser
Point in focus
Object
with
texture
lens
sensor
The coolest depth of field solution
lens
diffuser
Point in focus
Object
with
texture
lens
sensor
The Frazier lens
•The opposite: relay optics for large depth of field in macro shots.
•Simulate larger sensor using panorama stitching
Seeing through occlusion
Seeing beyond occlusion
•Photo taken through zoo
bars
•Telephoto at full aperture
•The bars are so blurry
that they are invisible
Synthetic aperture
•Stanford Camera array (Willburn et al.
http://graphics.stanford.edu/papers/CameraArray/)
Aperture
Why a bigger aperture
•To make things blurrier
–Depth of field
–outside the focal plane
•To make things sharper
–Diffraction limit
–on the focal plane
Sharpness & aperture (e.g. for the Canon 50mm f/1.4)
•f/1.4: soft (geometrical aberrations), super shallow Dof. Lots of light!
•f/2.8 getting really sharp, shallow depth of field
•f/5.6: best sharpness
•f/16: diffraction kicks in, loses sharpness. But dpoth of field is big
Depth of field
•More narrow when focusing close
•More narrow with bigger sensors
•For the same magnification, independent of focal length
Focal stack
aka
Focus stack
Problem: limited depth of field
Idea
•Take images with different focusing distances
•Merge into a full-focus image
-For each pixel, pick sharpest image
-Similar to autofocus
•Similar to HDR, panorama
-But along focus dimension
focus
focus
focus
Evaluating sharpness
•Local amount of high-frequency content
green: positive, red: negative
here image focused in the back
Need for smoothing
•The high frequency around an edge goes from positive to negative, through zero
•We want high “sharpness” all around the edge
•Plus we want spatial coherence in our choice of pixels
We can’t smooth directly
•High frequency has positive and negative values
•Local average is zero
•By construction: we removed low frequencies!
Local high frequency energy
•Take the square of the high frequencies
-All positive
Local high frequency energy
•Take the square of the high frequencies
-All positive
•Take local average using Gaussian filter
Focal Stack Compositing
•Weighted sum of all input images (normalize!)
•Each pixel weighted by sharpness value
x
x
x
+
and divide by sum of weights
Compositing (17 input images)
•Weighted sum of all input images
•Each pixel weighted by sharpness value
Compare to single image
Bells and whistle
•So far, simple weights
•But we want to give more weight to best image
•Potential solution: just use the max
•Simpler solution: raise the sharpness to a high exponent (e.g. 4)
With exponent 1
•(The previous version)
With exponent 4
•Subtle difference, but sharper
Visualization of image weights
•Random color for each input image
Visualization of image weights
•Random color for each input image
•With exponent =1 : blurrier
​
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results: input
More results
More results
More advanced
•Choose max image for each pixel
•Regularize for spatial coherence
•Poisson (gradient) blending
Interactive Digital Photomontage
Compared to our result
Macro montage
•55 images here
•Not my code
How to get the focal stack?
•Although here we change the viewpoint as well
Swept under the rug
•We also need to scale images
•recall that changing the focus changes the field of view / magnification
film
focused
at
infinity
film
focused
close
Macro
Scanning: combination in 1 exposure
From Macro photography: Learning from a Master
Macro is easy with small sensors
•1/ minimum focusing distance is way smaller
•2/ depth of field is bigger
•Summary: you've scaled down the camera, you can take pictures of a scaled-down world
sensor
lens
sensor
Point in focus
Object with texture
lens
Point in focus
Object with texture
Well, except for diffraction
(we did not scale wavelength!)
Fake depth of field
Challenge
•Shallow depth of field hard with small sensors
sensor
lens
sensor
Point in focus
Object with texture
lens
Point in focus
Object with texture
Challenge
•Shallow depth of field hard with small sensors
sensor
lens
sensor
Point in focus
Object with texture
lens
Point in focus
Object with texture
•Machine learning-based foreground-background segmentation
•Use dual pixels to get a depth map
(or analyze local defocus)
•Predict desired per-pixel blur radius
•Render with non-linear blur
(more later)
Challenge with defocus blur
•Remember, not a convolution!
•Blur radius depends on input scene depth,
not output pixel location
•Plus occlusion matters
*
lens
sensor
Point in focus
Occlusion
•Divide the image into k (=5) depth layers with opacity values
•Process each layer independently
•Composite back to front (with “over” operator)
Challenges
•Masks are hard, especially around hair!
•Need notion of sub-pixel coverage