So far, we have been looking at objects located in an object plane, with the lens focused such that the image appears perfectly sharp. In the real world, however, the three-dimensional objects we are photographing are hardly ever located in a single plane, but extending somewhere before and behind it. What happens in these cases?

The situation is illustrated in fig. 5. The purple rays are indicating an object perfectly in focus, i.e. for a given focal length **f**, the object distance **g** and the image distance **h** are adjusted according to the lens equation

1 / f | = | 1 / g + 1 / h | (D1) |

In this case, the picture of a point (in the object plane) is again a point (in the image plane).

Now, imagine an object a little closer to the camera, while the settings of the lens remain unchanged. This situation is shown by the green rays. According to the lens equation

1 / f | = | 1 / g_{near} + 1 / h_{near} |
(D2a) |

the rays are now converging somewhere *behind* the original image plane. On the image plane where our image sensor is located, the rays are thus illuminating a small spot. It will have the same shape as the aperture of the lens, i.e. in most cases, it will be (more or less) circular.

Next, imagine an object a little farther away from the camera, again with the same settings of the lens. This situation is illustrated by the yellow rays. According to the lens equation

1 / f | = | 1 / g_{far} + 1 / h_{far} |
(D2b) |

the rays are now converging somewhere *before* the image plane, again resulting in a small spot on the image sensor. (Note: **g**_{far} is not drawn to scale, but somewhat shortened to fit on the page.)

In both cases, the image of a point is no longer a point, but a small circular spot. In other words, the image gets blurred, it appears slightly unsharp.

Instead of moving an object back and forth and seeing how the size of the blur spot is affected, we can look at it the other way, define a maximum size of the spot, and see how far we can move our object such that the blur spot never exceeds this size. The maximum diameter that we are willing to accept as sharp is called the *(acceptable) circle of confusion* **c**.

There is much debate on how the circle of confusion should be determined, which we will ignore for the moment. Just think of it as a small value such as 0.033 mm (one thirtieth of a mm).

The range from **g**_{near} to **g**_{far} as shown in fig. 5 for which our blur spot does not exceed a given circle of confusion **c** is called the *depth of field (DoF)*. It describes the distance of objects which will be rendered with a minimum acceptable sharpness. **g**_{near} and **g**_{far} are known as *near* and *far limit*, respectively.

The depth of field can be derived as follows. By similar triangles along the green and yellow rays behind the lens, we get

c / (h_{near} - h) |
= | a / h_{near} |
(D3a) |

c / (h - h_{far}) |
= | a / h_{far} |
(D3b) |

Solving these equations for **h**_{near} and **h**_{far} respectively gives

h_{near} |
= | h a / (a - c) | (D4a) |

h_{far} |
= | h a / (a + c) | (D4b) |

Note that **a** is a distance, which can be measured in mm, inches, or any other unit of length. In photography, however, the aperture of a lens is commonly described by the dimensionless f-stop. The *f-stop* **A** (also known as *f-number* or *focal ratio*) is defined with respect to the focal length of the lens as

A | = | f / a | (D5) |

Solving equation (D5) for **a** and substituting it into equations (D4) gives

h_{near} |
= | h f / (f - A c) | (D6a) |

h_{far} |
= | h f / (f + A c) | (D6b) |

We can now use the lens equations (D2) to calculate the distances in front of the lens:

1 / f | = | 1 / g_{near} + (f - A c) / h f |
(D7a) |

1 / f | = | 1 / g_{far} + (f + A c) / h f |
(D7b) |

With the basic lens equation (D1), we can eliminate **h** and get a simpler form

g_{near} |
= | g f^{2} / (f^{2} + A c (g - f)) |
(D8a) |

g_{far} |
= | g f^{2} / (f^{2} - A c (g - f)) |
(D8b) |

Note that these formulas measure the near and far limits from the lens. However, to keep the resulting figures comparable to the focusing distance **d**, it makes sense to measure these distances from the image plane. This can be achieved simply by adding **h** (not **h**_{near} or **h**_{far}). Thus, we define

d_{near} |
= | g f^{2} / (f^{2} + A c (g - f)) + h |
(D9a) |

d_{far} |
= | g f^{2} / (f^{2} - A c (g - f)) + h |
(D9b) |

Both definitions (D8) and (D9) are common in the literature. For the depth of field calculator, the formulas given in equations (D9) are used.

Equations (D9) are not exactly intuitive on how the depth of field depends on its various parameters. To get a better understanding, we consider the range

dof | = | d_{far} - d_{near} |
(D10) |

With equations (D9), we get

dof | = | 2 g f^{2} A c (g - f) / (f^{4} - (A c (g - f))^{2}) |
(D11) |

This doesn't look much easier yet. However, a number of approximations can be applied. If the object is reasonably far away, i.e. the focusing distance **d** is much bigger than **f**, both **g** and **(g - f)** can be approximated by **d**. Furthermore, if the focal length is not too small, the second term of the denominator can be neglected. Thus, we get

dof | ≈ | 2 d^{2} A c / f^{2} |
(D12) |

This is now easy to understand: The depth of field depends quadratically on the focusing distance and the focal length, and linearly on the f-stop and the circle of confusion. This corresponds to common wisdom: For a shallow depth of field, e.g. for a portrait, use a telephoto lens and a small f-stop (i.e. a wide open aperture). Interesting though, getting closer to your subject will actually have a greater impact than using a smaller f-stop; this is great news if your lens isn't very fast. On the other hand, for a deep depth of field, e.g. for landscape photography, use a wide angle lens and a higher f-stop.

But we should take a second look here. If you want to take a frame filling picture of someone or something, it's always the same magnification, no matter whether you are using a short or a long focal length; you just have to move a little closer or farther away. So how does the magnification come in?

With the approximation of the magnification (M16)

m | ≈ | f / d | (D13) |

which is also valid under the conditions mentioned above, approximation (D12) becomes

dof | ≈ | 2 A c / m^{2} |
(D14) |

So the depth of field depends linearly on f-stop and circle of confusion, and quadratically on the magnification. No wonder landscapes can be pictured with a great depth of field, while this is rather difficult for macro scenes.

Likewise, the smaller your image sensor, the smaller the magnification, and the deeper the depth of field. No wonder mobile phone cameras will render everything more or less sharp, while you can create tack sharp portraits with a lovely blurry background with larger image sensors (although applications of computational photography with dual cameras such as Apple's portrait mode are increasingly successful in bokeh simulation even with small sensors).

The depth of field also provides the basis to calculate the hyperfocal distance, which is often recommended for landscape photography.