Best Way to Select Foreground with GIMP (A Photo-Editing Tutorial)

Original Versus GIMPed Version

When editing a photograph, it is common to apply an effect to just the foreground and not the background, or vice versa. The problem is that GIMP does a poor job of selecting the foreground for you, so you have to do it yourself (From what I’ve seen, Photoshop isn’t much better at this). This tutorial will show you a couple methods for foreground selection, how to use them together, and how to apply them with some other basic photo editing techniques to improve a photo.

The original photo we’ll use for this tutorial is below. That is the Orote Peninsula in the background, and there is a Coast Guard vessel navigating Apra Harbor to the right (Where did I take the picture?). I have the subject’s permission to use his picture for this tutorial, of course.

Original Image

Here’s what we’ll be doing to this image:

Attempt Basic Photo Editing

The first step is to open the image in GIMP. Now, look at the Layers Dialog to the right. If you don’t know how the layers work, you should spend some time playing with that dialog or read the GIMP documentation. At this point, you could duplicate the Background Layer in order to preserve the original image before changing it. However, I usually just make my basic changes on the background layer and then save the result as a separate file later, so the original is still preserved somewhere. If you change your mind and want the original as a separate layer later, just do File → Open As Layers and import the original image.

Anyway, I’ve found that most of my pictures look better after an Unsharp Mask. Just do: Filters → Enhance → Unsharp Mask as shown below:

Unsharp Mask

In the Unsharp Mask dialog that will pop up, I almost always stick to the defaults.

Next, let’s change the brightness of the image. Don’t use the Brightness/Contrast tool though, use either Levels or Curves. I’m pretty sure they do the same things, but I’ve found the Levels tool to be easier to use. Go to Colors → Levels and this dialog will pop up:

Edit Levels

Make the image brighter or darker with the Gamma slider, which is the middle slider for the Input Levels control. In this case, I thought it looked better a little brighter. It’s all up to you. Sometimes, the ideal gamma setting is right under the highest point of the biggest curve in the graph, but only if you’ve taken an excellent photo (doesn’t happen to me often).

Usually, the next thing I do is increase the color saturation. Go to Color → Hue-Saturation and try it out:

Saturation Makes Skin Orange

In this case, extreme color saturation is making the subject’s skin orange. I do like how it looks in the background though. How can we apply color saturation to the background but not the foreground? Well, that’s the whole point of this tutorial. If we can select the foreground, we will be able to invert the selection and apply color saturation to the background without messing up the foreground. Cancel the Hue-Saturation changes and read on to learn how.

Select the Foreground With a Quick Mask

In GIMP, if you want to make certain parts of a layer transparent by using the drawing/painting tools, you can use a Layer Mask (I’ll cover that later). However, you can use the same techniques to carefully design a selection with a Quick Mask. Although there is also a Free Select Tool and a Scissors Select Tool, those tools do not offer enough precision to avoid noticeable overlaps between foreground and background after editing. The Quick Mask allows you to draw the selection yourself, and if you make a mistake, just draw over it with the opposite color. You start a Quick Mask by clicking on the Toggle Quick Mask button:

Toggle Quick Mask

Assuming you hadn’t already selected an area before starting the Quick Mask, the entire image should now appear pink. The next step is to head to the Toolbox dialog, pick a painting/drawing tool (I like the pencil), adjust the scale until your brush is the right size, and make sure your foreground and background colors are set to black and white. The easy way to reset your foreground/background colors if you’ve picked different colors recently is to click on the black and white icon:

Reset Colors

The idea is to paint in black and white, then click on the Toggle Quick Mask button again to turn it into a selection. Don’t worry, you’re not altering the image itself. Pink areas are not selected. Use white to color in the areas you want to select. If you mess up, draw back over the area with black (which will turn the area pink). Confusing? Just start playing with it and you’ll get it. You can switch colors easily by pressing x. I usually crank up the scale on my brush and start with a rough outline of my selection:

Rough Quick Mask

Next, zoom in (use + to zoom in and - to zoom out) and start painting the details. If you hold shift, you can draw a straight line from the last point you painted to the current location. I use this feature constantly, as shown below:

Edit Quick Mask

You can finish the entire selection as stated above, but I find it easier to paint over the foreground instead of painting over the background, so I’m going to invert the whole thing. Click on the Toggle Quick Mask button to turn it back into a selection, then do Selection → Invert as shown below:

Invert Quick Mask Selection

You wont actually see anything change when you do this, but the difference will be apparent when you start your Quick Mask again. Hit the Toggle Quick Mask button, and you’ll notice that the pink areas are now over the foreground, and you can finish the details by painting the edges of the subject with black. Note that doing it this way is backwards, so what you’re actually doing is selecting the background. About 5-10 minutes later, you’ll have something like this:

Finished Quick Mask

Now what? I guess we could get on to selectively editing the photo, but I learned a long time to ALWAYS SAVE YOUR SELECTIONS!, at least if you had to work hard for them. There are actually a couple methods to do this, so I devoted a whole section to it. It’s the section just below this one. Stay with me, I wont make this painful.

Two Ways to Save a Selection

The easy way to save a selection is to do Select → Save to Channel:

Save Selection To Channel

After you do this, the Channels tab will automatically open up in the Layers Dialog. I make a habit of renaming the selection so I don’t forget which is which. You can rename it by right-clicking and selecting Edit Channel Attributes (the only option is to rename it, so why not call it Rename?).

Rename Selection Channel

That’s all there is to the first method. If you wanted to retrieve the selection later, just go back to the Channels tab, right-click on the selection, and select Channel to Selection. For now, though, to get back to normal operations, you need to click back to the Layers tab.

The second way to save a selection is to actually duplicate it in a separate layer. I prefer this sometimes just because it’s easier to see what exactly has been selected. The first thing we need for this process is a copy of the first layer. This is easy with the Duplicate button:

Duplicate Background

Next, it’s a good habit to keep track of your layers with appropriate names. Right click on the new layer, select Edit Layer Attributes, and name it whatever you want. I picked Quick Mask Selection.

Rename Layer

By default, if you click Delete while an area is selected in GIMP, that area will turn white. However, if you have added an Alpha Channel to the layer, each pixel can now store a transparent value in addition to the standard Red, Green, and Blue. After adding an alpha channel, clicking Delete will turn an area transparent (visualized by gray checkered squares). Add an Alpha Channel by right-clicking on the layer and selecting Add Alpha Channel.

Add Alpha Channel

Now, assuming you’ve done everything exactly as I have, all you need to do is click Delete, and the entire background will become transparent. However, if you still have the foreground selected, just invert the selection and then click Delete. Here’s how it should look:

Quick Mask Selection

Are the selection lines distracting to look at? I think so to, so I usually do Select → All (or Ctrl + A) to select the entire layer and remove the selection lines from my foreground. But wait, how do I get my selection back now!? Well, you can always try Undo, but if you’ve done a lot of work since then, it will all be undone. No, there is a much easier way. Just right-click on the layer and choose Alpha to Selection. This will select only the colored areas, and since the background is now transparent, it will result in selecting only the foreground.

Well, that’s it for saving selections. Next, let’s take a quick look at why I wrote this tutorial in the first place… the Foreground Select Tool stinks.

Demonstrate Why the Foreground Select Tool Is Worthless

I was going to post directions on how to use the Foreground Select Tool here, but I’m a little low on motivation and my whole point is that you shouldn’t use it anyway. I’ll just show you the result I got with it below. Although it’s a start, you’d need to refine the borders by hand for virtually every part of the foreground, so how’s that any different from what we did above? I prefer to start with a clean slate if I’m going to have to do all the work myself anyway.

Foreground Select Tool Results

Maybe in the future this tool will improve, but for now it’s not nearly good enough for me. However, the following technique does occasionally pay off and save me a lot of time painting everything by hand.

Easier Foreground Selection With Threshold and a Layer Mask

I’m guessing some of you were appalled by the amount of work you have to do to select a foreground manually. The good news is that there is a technique which may help you, and sometimes it may help you an aweful lot. The bad news is that sometimes it doesn’t help at all. In this case, it was partially successful in selecting the foreground. It offers a small amount of improvement in my original selection, but not a lot. However, if the subject in this photo had more hair, I believe this technique would have been essential to making a good foreground selection. If you’ve ever tried to color in someone’s hair manually, you know that it’s nearly impossible to do. Using this technique, I have successfully created a convincing selection of someone’s hair numerous times. In fact, I tailor my use of this technique for selecting hair, and I care little if anything else shows up well (the rest of the outline I can do myself easily, as we did above, but the hair is a pain!). I usually start my selection process with this technique, and then edit the resulting Layer Mask just as we edited the Quick Mask above. I decided to cover the Quick Mask technique first in this tutorial because it helps you learn the basics.

The first step is to create another copy of the Background layer, so that we don’t erase parts of the image we already edited. We already learned how to do this, so I wont repeat myself here. I named this new copy Saturation Select, but you can name it whatever you want. Next, make sure you have either the Background layer selected or the copy we just made (either one, it doesn’t matter), and go to Colors → Components → Decompose:

Decompose Layer

You’ll see the following dialog box pop up. Just stick to the default options as shown and click OK.

Decompose To Layers

Next, GIMP will open a new image with three new layers that are grayscale representations of the red, green, and blue components of the image. As you can see, this is a new image, so we can mess with this without altering the image we care about. The first thing we need to do is choose which layer will suit our needs the best. Click on the eye icon next to each layer to change its visibility (make it visible or invisible). The only way to see the blue layer is to make the other two layers invisible, as I have done below. The layer we want to keep is the one that shows the highest contrast between foreground and background. In this case, the blue layer was the best, because the foreground is dark while the background is light (especially around the hair).

Decomposed Image

Next, we are going to open the Threshold Tool by doing Colors → Threshold (while the blue layer of the decomposed image is selected).

Open Threshold Tool

Play with both sliders of the Threshold Tool until you obtain a clear outline of the subject’s hair. It doesn’t matter if the foreground is black or white, only that there’s a good outline (you can invert the colors later). It also doesn’t matter if the rest of the outline looks good. The hair is usually the hard part (although not in this case, thanks to the haircut).

Clear Outline

Now use a large brush and fill in the background so all that you have left to worry about is refining the foreground selection. If you want to fill in large areas (and make sure you don’t miss small spots), use the Rectangle Select Tool and the Bucket Fill Tool. Make sure the Bucket Fill Tool is set to Fill Whole Selection, and everything you have selected will turn white (or black, depending on which color the background turned out to be).

Outline

Earlier in this tutorial, I mentioned that a Layer Mask is just like a Quick Mask, except that a Quick Mask is used to paint a selection, while you use a Layer Mask to paint transparency. Well, the image above is going to be the beginning of a Layer Mask that we are going to apply to the new layer (the one I called Saturation Select). In order to get this outline from its current location to our original image, we just copy it to the clipboard with Ctrl + C. Then, we return to our original image, right-click on the new layer, and select Add Layer Mask.

Add Layer Mask

The Layer Mask defaults to white, and there’s no reason to change the default because we’re just going to paste in our own image anyway. Once the Layer Mask is created, just make sure the new layer is selected and press Ctrl + V to paste in our outline. Note that pasted images are inserted as a separate layer until they are anchored, so go ahead and click the Anchor button:

Anchor Layer

You’ll notice that since my foreground is black in the Layer Mask, it appears transparent in the result. I can fix that with a simple Colors → Invert. The only reason I am able to do that is because I have not Applied the Layer Mask (you can apply the mask from the right-click menu or from the Layer menu), so I am still able to edit it. As a matter of fact, I can even change the Mode of this layer to Burn (or another similar mode of your choosing), make sure the Background layer is visible below it, and edit this Layer Mask the exact same way we did with the Quick Mask above. That’s my usual method, in fact. However, it isn’t necessary here, since we’ve already done most of the work. This is what the rough outline looks like:

Layer Mask Result

If you need more contrast to see the fine details around the edges, just add an empty white layer to the bottom of the layer stack. You can do this with the New Layer button, which is located to the left in the same area as the Duplicate button.

Now we need to combine the two foreground selections to get the best of both worlds. The easiest way I can think of is to delete part of each selection layer and then merge the two together. I wanted to keep my original Quick Mask Selection, though, so I Duplicated that first. I didn’t care about the Layer Mask Selection (not as much work), so I just merged the original.

First off, we still need to finish our Layer Mask by applying the Layer Mask:

Apply Layer Mask

Now delete the parts of the Layer Mask selection that didn’t turn out well, and make a copy of the Quick Mask selection layer so you can delete part of that one too.

Getting Reading to Merge Selections

Toggle the visibilities of the two layers back and forth until you can decide on the right area to delete from the Quick Mask selection so that there is only a small amount of overlap.

Getting Reading to Merge Selections

Now make both layers visible and correct any obvious problems with the resulting foreground. Then, merge them together:

Merge Down

After merging, I renamed the resulting layer to Compiled Selection, and I saved it to a channel, just to be sure I didn’t lose it. That’s it! I know this process seems long, but once you know it well it goes fast (except for the part where you have to draw the outline by hand).

Selectively Apply Effects Using Foreground Selection

Finally, let’s put our foreground selection to good use. Retrieve your selection, either by right-clicking on one of the selection layers and choosing Alpha to Selection or by right-clicking on one of your saved channels and selecting Channel to Selection. Then, make sure your Background layer is selected and visible (and make the other layers invisible if they are above Background on the stack). Finally, invert your selection if necessary to select the background instead of the foreground. You can tell the difference by remembering that the animated selection border (the “marching ants”) will extend around all the layer borders when the background is selected, but only around the foreground when the foreground is selected. Still can’t tell? Just click the Toggle Quick Mask button twice to see what’s selected and then return to normal mode. Once you’ve got it straight, open the Hue-Saturation tool again (Colors → Hue-Saturation).

Saturated Background

Much better, right? Was it worth the work? I think so. It doesn’t take very long to do all this once you have some practice. And it’s nice to have some lush greenery without any orange skin!

Contrast Mask to Remove Shadows From Foreground

While we have this excellent foreground selection, let’s make use of it some more. This time, let’s beef up the foreground instead of the background. We’re going to apply what has been called a Contrast Mask, which I have discussed in detail before.

First, Duplicate the Background Layer, rename the copy to Contrast Mask, add an Alpha Channel, and delete the background (it should still be selected, so just press the Delete key). If you don’t remember how to do these things, they have all been demonstrated earlier in this tutorial, so just search for the key words in your browser using Ctrl + F. The next step is to Desaturate the Contrast Mask. This is what your screen should look like so far:

Desaturate Contrast Mask

In the Desaturate dialog that follows, just choose the default mode (Lightness). The next step is to invert the resulting grayscale foreground. Just do Colors → Invert, and the result should be a nice bright glowing version of the subject of our photo. We’re going to use this to brighten the shadowy parts of our subject by changing the mode of the Contrast Mask to Overlay. For some reason, the Contrast Mask can make things look a little blurry unless you blur the mask itself, so do Filters → Gaussian Blur on the Contrast Mask. I used 31.5 for my horizontal and vertical blur settings, but you can read my other tutorial for the details on the ideal Gaussian Blur settings.

Contrast Mask In Action

And finally, adjust the Contrast Mask layer to desired affect. I usually choose to leave it at about 60%-70% opacity. Adjust Contrast Mask Opacity

Final Result

Here it is! The result:

Final Result

And here it is contrasted with the original:

Original Versus GIMPed Version

I hope you’ve learned something! Happy editing.

About GreeenGuru

Computer Enthusiast
This entry was posted in Ubuntu and tagged . Bookmark the permalink.

One Response to Best Way to Select Foreground with GIMP (A Photo-Editing Tutorial)

  1. SMelanie says:

    thanks for such a helpful tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>