GIMP Guru

SimulatedDOF

Simulating Shallow Depth of Field with The GIMP

Before After

One of the facts of life of shooting with consumer digicams and their associated small imaging sensors is that it can be difficult to achieve a shallow depth of field. Having large depth of field is usually a good thing, leading to pictures in which all of the important parts of the picture are in focus. But sometimes you really want shallow depth of field to isolate a subject against a blurred background, as is often done in e.g. portraiture.

In this tutorial I’ll show you how to simulate a shallow depth of field using image editing techniques. It is painstaking enough that it is no substitute for having camera equipment that is capable of the job. Nevertheless, it can be useful in rescuing the occasional picture.

The Procedure

As with many of the other tutorials on this site, the beauty of this method is in the use of layers. A duplicate layer is blurred and a layer mask added to make the subject transparent. The original image remains unsullied in its own layer.

Load your image into the GIMP.I thought this portrait would look better if the background wasn’t quite so distracting.
The most difficult thing we will have to do is creating a mask for the subject. We will try to find the “natural mask” that is already in the image and so use this to make our work much less tedious.The best contrast between items can often be found in the green channel. It’s a good place to start looking, in any case. So the first thing we will do is decompose to individual RGB channels.

Right-click in the image and choose (Image/Mode/Decompose). In the Decompose dialog box, choose RGB and click OK.

Examine each channel to find which has the best contrast for isolating the subject. In this case the green channel image was the best.

Close the other two images that you aren’t going to use. With a little work this green channel image will become our mask.First we’ll minimize our work by drawing a selection outline around the subject with the lasso tool ().
Now invert the selection (Ctrl+I), choose the fill tool (), make sure Black is selected as the foreground color and click in the image.Clear the selection (Shift+Ctrl+A). You should see something as shown on the right.
Now we want to try to force the outline of the subject to white. Right-click in the image and choose (Image/Colors/Threshold).In the Threshold dialog click in the histogram and drag to the right, as if you were selecting the right half. You will see the image go to black and white, with everything to the left of the selection going to black, and everything in the selection going to white. Experiment with different selections until you have one that gives the best isolation of the subject in white.

When you are satisfied, click OK.

We’re going to manually touch up the mask to fix any last imperfections. Here’s where the work can get a bit tedious. If you’ve found a good contrast between subject and background and exploited the lasso, fill and threshold steps well you shouldn’t have too much drudge work at this step.To help us out we’re going to composite the mask and the image. Open the Layers dialog (Ctrl+L) and click on the New Layer button (). Name the new layer “Mask Guide”.

Now go to the original image window (you didn’t close it did you?). Select All and Copy (Ctrl+A then Ctrl+C). Now go back to the mask image and paste into the new layer (Ctrl+V). It should paste as a grayscale image. Finally, in the Layers dialog, anchor the pasted image and reduce the opacity of the Mask Guide (upper) layer to around 70%, as shown. The image should look something like what is shown in the lower right.

What this does is allow you to view the image and the mask at the same time, overlayed, while you work on the mask.

Before you leave the Layers dialog, select the Background layer in the mask image (that’s the layer we want to work on), as shown at right. The Mask Guide is just to guide us.Select a reasonable brush (usually small and fuzzy) from the Brushes Dialog (Ctrl+B), select the Paint tool () and begin painting the mask. You want the subject in White, anything that is not the subject should be Black.

You’ll probably need to use a few different brushes. Zoom and pan around the image. Switch back and forth between Black and White (x) and take lots of small steps so that you can make use of the excellent Undo capabilities of the GIMP.

Periodically you can turn off visibility of the Mask Guide (by clicking on the “eye” next to that layer in the Layers dialog) to view your mask’s progress.

When you are all done, I suggest saving the mask (as an XCF file) so that all that painstaking work does not have to be replicated later if you decide to re-edit the image.

Do a small Gaussian Blur (Filters/Blur/Gaussian Blur (IIR)) of about 5 pixels or so on the mask.Then invert the mask (Image/Colors/Invert).
In the Layers dialog, select the original image in the drop down box. Then click the Duplicate Layer button ().Double-click on the name of the top layer. In the Edit Layer Attributes dialog, rename the new layer “Blurred”.
In the original image window, right-click and select (Filters/Blur/Gaussian Blur (IIR)). Experiment to find a value that works well for your desired “depth of field”. Generally you won’t want to go too crazy with this or the look will be all wrong. Here I used a value of 40.
Go back to the Layers dialog and right-click on the “Blurred” layer; select “Add Layer Mask”. In the Add Mask Options dialog make sure White (Full Opacity) is selected. Your Layers dialog should now look as shown at right.
Go to the mask image window and Select All and Copy (Ctrl+A then Ctrl+C). In the original image window, Paste (Ctrl+V). This should paste the inverted mask into the layer mask.In the Layers dialog, click on the anchor button to anchor the pasted image.

Voila! Your subject should be clear and sharp, while the background appears soft and out-of-focus.

Tips

  • You can fine-tune (lessen) the effect of the blur by adjusting the opacity of the blurred layer in the Layers dialog. If you want to do this it makes more sense to blur aggressively and “back off” using the opacity slider.
  • On Creating a Mask
    The layer mask determines how strongly the layers are combined: assuming a “Normal” blending mode as we have used here, a white pixel in the mask makes the corresponding pixel in the upper layer opaque, black makes it transparent and a shade of gray makes it somewhere in between (translucent), depending on how dark the gray is. You can use this knowledge to make parts of the image have more or less “DOF” than the others; if you want it sharper, make that part of the layer mask darker, if you want it more blurry, make it lighter. Sooo…If your subject and background are less defined than what is shown in this example (e.g. a landscape with a prominent foreground) you may be able to get away with simply using an appropriate black-white gradient as the layer mask.

    Another possibility is to duplicate the layer, use the “lasso select” tool to select around your subject and then feather the selection heavily. Fill the selection with black then invert the selection and fill with white. Select all and copy to the mask.

  • If there are “edge effects” around the “in focus” part, try adding the layer mask first, checking the “Keep Trans.” (keep transparency) checkbox in that layer, and then blurring. Note: for this to work it may be necessary to turn the mask into a selection, erase the selection to transparency, and then blur. This keeps the subject color from blurring into the background.
  • Adjusting Levels or Curves on either the background (blurred layer) or foreground (lower layer) may help increase the DOF effect. Try darkening the background slightly, for example.

For Further Reading

2 Comments »

  1. Hi there,
    I have followed your instructions on simulating shallow depth of field but cant get past mode as the decompose window with the extract channels dos`nt show!
    What should I do, thank you.

    Comment by rockycutting — August 14, 2011 @ 5:13 pm

  2. Rocky,
    Are you saying you don’t know where the Decompose menu item is? Take a look under Colors->Components->Decompose. The GIMP developers are forever shuffling the menus. Makes it hard to write documentation…

    Comment by Eric Jeschke — August 15, 2011 @ 9:21 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.