<p>The depth of field is a characteristic of a camera lens setting, although the name "Depth of Field" is commonly used to describe the effect caused by such characteristic. </p>

<p>Camera lenses can only perfectly focus on one single point, but there is a distance for which the image will still look reasonably sharp. Such distance is what actually the Depth Of Field is. This distance is variable depending on various factors, but let's say for the sake of brevity that the easiest way to adjust it is by changing your camera aperture. </p>

<p><a href="https://i.sstatic.net/ezhSb.jpg" rel="noreferrer"><img src="https://i.sstatic.net/ezhSb.jpg" alt="enter image description here"></a></p>

<p>As for the circle of confusion it is helpful to look a pic from wikipedia: </p>

<p><a href="https://i.sstatic.net/g7MnD.png" rel="noreferrer"><img src="https://i.sstatic.net/g7MnD.png" alt="enter image description here"></a> </p>

<p>In all of the three cases a single point is "projected" onto our image plane, but as you can see that is at different distances from the lens.</p>

<p>Basically because, as I said, not everything is perfectly in focus (that is the focal point is on the image plane), a point can be projected to our image plane not to a single point but to an area. This, as it is clear from the pic, is because the focal point is either before or after the image plane. This "circle" that is created on the image plane is what is called the circle of confusion. </p>

<p>Now from this should be clear that varying the depth of field you vary the CoC size, hence it can be used as a measure of the DoF itself and an intuitive one when thinking in terms of blurring. </p>

<p>As per how it is implemented in rendering there are really lots of methods out there. Usually in games is done as a post process using the depth information to split your scene in different "planes"; for example a focus plane that does not need to be blurred, a near plane and a far/background plane that need to be blurred. Once you blurred your planes you can composite them back together to get your final image. 
How much you blur the various planes it is dependent on the effect you want to achieve. Advanced system usually implement this and other "lenses" effect using appropriate parameters coming from the photography world like the aperture. </p>

<p>Note that there are many ways to implement the effect, the one I described is just one way that also has variations (for example performing the whole thing at lower res). The limit is dependent on what your target is and how much you budgeted for this effect. You can go from adopting the "3 planes" method at extremely low res to computing the circle of confusion for each pixel and applying the consequent ad-hoc blur to that pixel. </p>

<hr>

<p>EDIT: The comment from @NathanReed needs to be more in evidence as part of the answer: </p>

<blockquote>
  <p>Matt Pettineo's blog posts <a href="https://mynameismjp.wordpress.com/2011/02/28/bokeh/" rel="noreferrer">How To Fake Bokeh</a> and <a href="https://mynameismjp.wordpress.com/2011/04/19/bokeh-ii-the-sequel/" rel="noreferrer">Bokeh II: The Sequel</a> are great introductions to how to practically implement post-process DoF and address the typical artifacts you get from it. </p>
</blockquote>

CC BY-SA 3.0
2017-07-05T03:55:46.300Z
A cheap way to do this is simply use Gaussian blur on your final scene and store two of them. One for foreground and one for background. They can have the same blur level or different and it is up to you. Upon finishing the blur, simply lerp from depth 0 to some determined (and tweak-able) depth (say 0.10). What you are learning is this blurred scene to the perfectly in-focused scene. For the background blur you can simply do the same except you are blurring from perfect in-focus to the blurred image. Leave some room for the middle to create a perfectly in focus region.
ChaoSXDemon
CC BY-SA 3.0
2015-08-05T17:03:21.057Z
2015-08-05T21:07:18.557Z
2015-08-05T21:07:18.557Z
cifz
<p>I know that depth of field involves blurring.</p>

<p>There's some great information about how to do a Gaussian blur in the question <a href="https://computergraphics.stackexchange.com/questions/39/how-is-gaussian-blur-implemented">How is Gaussian Blur Implemented?</a></p>

<p>But, other than that, how is depth of field implemented?</p>

<p>What are the rules about how you blur each pixel, and how do you handle the case of when there is a pixel that wants to blur a lot next to a pixel that doesn't want to blur as much?</p>

<p>I've also heard of the "circle of confusion" but have no idea what that is.</p>

<p>Can anyone explain DOF in a plain, easy to understand way?</p>

<blur><post-processing><depth-of-field>
How is Depth of Field Implemented?
Alan Wolfe
# How is Depth of Field Implemented?

(Asked by: username_0 on 2015-08-05)

I know that depth of field involves blurring.

There's some great information about how to do a Gaussian blur in the question [How is Gaussian Blur Implemented?](https://computergraphics.stackexchange.com/questions/39/how-is-gaussian-blur-implemented)

But, other than that, how is depth of field implemented?

What are the rules about how you blur each pixel, and how do you handle the case of when there is a pixel that wants to blur a lot next to a pixel that doesn't want to blur as much?

I've also heard of the "circle of confusion" but have no idea what that is.

Can anyone explain DOF in a plain, easy to understand way?

**Tags:** blur, post-processing, depth-of-field


---

## 1 answers

### Answer by username_1 on 2015-08-05. Score: 22 | Accepted answer

The depth of field is a characteristic of a camera lens setting, although the name "Depth of Field" is commonly used to describe the effect caused by such characteristic.

Camera lenses can only perfectly focus on one single point, but there is a distance for which the image will still look reasonably sharp. Such distance is what actually the Depth Of Field is. This distance is variable depending on various factors, but let's say for the sake of brevity that the easiest way to adjust it is by changing your camera aperture.

[![enter image description here](https://i.sstatic.net/ezhSb.jpg)](https://i.sstatic.net/ezhSb.jpg)

As for the circle of confusion it is helpful to look a pic from wikipedia:

[![enter image description here](https://i.sstatic.net/g7MnD.png)](https://i.sstatic.net/g7MnD.png)

In all of the three cases a single point is "projected" onto our image plane, but as you can see that is at different distances from the lens.

Basically because, as I said, not everything is perfectly in focus (that is the focal point is on the image plane), a point can be projected to our image plane not to a single point but to an area. This, as it is clear from the pic, is because the focal point is either before or after the image plane. This "circle" that is created on the image plane is what is called the circle of confusion.

Now from this should be clear that varying the depth of field you vary the CoC size, hence it can be used as a measure of the DoF itself and an intuitive one when thinking in terms of blurring.

As per how it is implemented in rendering there are really lots of methods out there. Usually in games is done as a post process using the depth information to split your scene in different "planes"; for example a focus plane that does not need to be blurred, a near plane and a far/background plane that need to be blurred. Once you blurred your planes you can composite them back together to get your final image.
How much you blur the various planes it is dependent on the effect you want to achieve. Advanced system usually implement this and other "lenses" effect using appropriate parameters coming from the photography world like the aperture.

Note that there are many ways to implement the effect, the one I described is just one way that also has variations (for example performing the whole thing at lower res). The limit is dependent on what your target is and how much you budgeted for this effect. You can go from adopting the "3 planes" method at extremely low res to computing the circle of confusion for each pixel and applying the consequent ad-hoc blur to that pixel.

---

EDIT: The comment from @NathanReed needs to be more in evidence as part of the answer:

> Matt Pettineo's blog posts [How To Fake Bokeh](https://mynameismjp.wordpress.com/2011/02/28/bokeh/) and [Bokeh II: The Sequel](https://mynameismjp.wordpress.com/2011/04/19/bokeh-ii-the-sequel/) are great introductions to how to practically implement post-process DoF and address the typical artifacts you get from it.

> **Comments:**
> **username_2**: A cheap way to do this is simply use Gaussian blur on your final scene and store two of them. One for foreground and one for background. They can have the same blur level or different and it is up to you. Upon finishing the blur, simply lerp from depth 0 to some determined (and tweak-able) depth (say 0.10). What you are learning is this blurred scene to the perfectly in-focused scene. For the background blur you can simply do the same except you are blurring from perfect in-focus to the blurred image. Leave some room for the middle to create a perfectly in focus region.  

---

