The Ultimate Keyer


Now THAT'S a sweet key!

If you understand color difference keying, then you have a valuable tool in your toolkit that will serve you well through all but the most demanding greenscreen extractions.

That said, what about when you have to deal with THE MOST DEMANDING situation? You kow the one I'm talking about. it's when you have to deal with fine wisps of hair, semi transparent elements, in-camera motion blur -

In short, it's the greenscreen job from hell.

This shot is from that music video I've worked on a few years back. We had several shots like this (my apologies to Natalie for yet another unflattering screenshot, btw).

Back to our problem...You've been able to pull a basic matte using color difference techniques, but you're getting too much opacity on your fringes, and when you try to layer in edge detail, you can't seem to get things opaque enough before things start to fall apart.

May I be candid for a moment? It's always seemed to me that there ought to be some reliable approach that would make even the most challenging greenscreen process a matter of sound theory and practice instead of luck. In other words, I want to spend my energy on pulling a perfect key and being creative, not seeing how I can hide the flaws in my composite.

Good news, friends - I do believe I have stumbled upon the ultimate greenscreen technique, and if you're willing to go with me on a slightly wild ride into the mathematical basis for a perfect matte, I think you will agree. And, believe it or not, we will NOT be starting out with a green minus red color difference matte!

Well, ok, that's not completely true, but humor me for a bit, and let's assume that you HAVE pulled a reasonably good color difference key, but you went ahead and blurred the matte and eroded the edges a couple pixels. So what you have is a great key of your subject, but basically, the edges suck.

From that auspiscious starting we point we ask ourselves - how to restore EXQUISITE edge detail, losing nothing in the process. Well, let's start with a little theory, some assumptions that can guide us on the way. This may not all make sense right now but try to stay with me.

Assumption #1 is that curves or ramps that are used to increase contrast in a matte are a Good Thing but only when used judiciously, because WHENEVER WE INCREASE CONTRAST WE ARE THROWING DETAIL AWAY. Why?

Well, just figure it this way. If your matte is right, the subject has to be pure white. The keyed area has to be black (fully transparent). The problem is the border between these two. The devil is in the edges - that vexing area where detail happens and semi transparency makes the difference between feature film quality composites, and your local TV weatherman.

See, when you increase the contrast in a matte to an acceptable level, you are, by definition pushing some pixels to black or white that, given a choice, you would rather leave some shade of grey. Heck, that's WHY the edges of the matte so often look bad! It's why getting those fine wisps of blond hair are so tough. What is needed is a sound way to control how much contrast adjustment occurs such that you never unwittingly throw out precious pixel data.

I know, I'm rambling. So here's Assumption #2 : Really good mattes usually consist of several constituent mattes combined to provide a final, perfect matte.

Therefore (and this is CRUCIAL to understand) : IT IS NEVER IMPORTANT FOR A CONSTIUENT MATTE TO BE HIGH IN CONTRAST. ALL THAT MATTERS IS THAT THE CONSTITUENT MATTES ADDED TOGETHER HAVE HIGH CONTRAST. The converse corrollary applies. If you add together two mattes where, say, the white levels would add up to more that 255, then you can be sure that at some point in the matte creation process, you introduced excessive contrast and chucked some detail you didn't have to.

Confused? Good! It'll all make sense very soon. Stay with me and you will want to send me a check for all the money you didn't have to spend on Digital Fusion and Ultimatte!

Here's a theory to chew on. Every color channel of your greenscreen footage contains valuable color information that can assist in pulling the perfect matte. If you had a smart way to use all this color information to your advantage, perhaps the perfect matte is hidden in those channels. Indeed it is, but how to get at that data?

Let' start with the obvious. Insert a color separation node to split the footage into RBG channels.

Raw Red Channel

Raw Green Channel

Raw Blue Channel

Now, we want a matte where the subject is white and the background is black, so do me a favor. Feed the green channel into a ramp and reverse the black and white so the green channel is inverted. Trust me.

Inverted Green Channel

Now what we have is three color channels where, visually, you can see just by eyeballing it that the background (greenscreen) is darker than the foreground (subject), right? What we need to do at this stage is figure out how to make the most of this contrast and recombine the channels in a way that results in a killer matte. We are about to discover that this is deceptively simple.

Step one. Feed each of the three color channels to a viewer node, and in your preview, use your mouse to sample the RGB values in the background area.

Sampled Color Values

Blender is kind enough to convert all of our pixel information into floating point AND show us the RGB values too. Simply make a note of what the average floating point value of the background is. In this case you can see it's 0.443.

Fine. Pop up a math node and feed this color channel into it. Make it a subtract operation that subtracts a constant value of 0.443 from this channel. 

Subtracting a constant from each channel

Green Channel with constant value subtracted

What does this do?

Well, it pushes the black level of this channel to 0, WITHOUT THROWING OUT VALUABLE CONTRAST INFORMATION. All we did was offset the level of the channel, but the overall contrast relationship between blacks, greys and whites in this channel has been unaltered.

Do this same thing to all three channels :

Offset Red Channel

Offset Blue Channel

Of course, now you are complaining that this matte's going to stink because there's nowhere near enough contrast to make the subject area useful - it's not even close to white!

Patience, grasshopper! Don't forget we're going to add these channels together. All that matters is that, once we add the grey values of each of the three channels together, then add this result to a basic color difference matte, the final result is at or near white.

Er, this introduces a teeny weeny problem. If the black areas of the matte are not at true zero, then when you add them together, they could be greater than zero, meaning we will lose our perfectly black (read : opaque) background.

And let me digress a moment. The pixel values are floating point numbers that may in fact be negative. Theoretically adding negative numbers results in even lower negative numbers (duh), which shouldn't alter the black levels in my matte (ie the values are still less than zero, or true black), but my tests have revealed more predictable matte behavior (as we pass our footage through nodes that do various requisite math) if the blacks are black - that is, exactly ZERO.

No problem - just feed each of the three channels through a math node set to MAXIMUM with a numerical constant of zero. That means that if the dark areas are not quite zero, now they will be. You can see this in the previous screenshot, but here it is again :

Now you have the peace of mind of knowing that if you add the channels together, black will stay black, since 0+0=0.

Add 'em up!

Channels added together

You'll find the contrast is not what it needs to be, and in the screenshot you can see I used a ramp to clamp it. So here is where we risk our first loss of pixel information, but we will apply some mathmatical good sense to ensure the losses are minimal. How?

Well, remember we're creating a detail matte and it is going to be added to a basic green minus red color difference matte. We know all the wispy fringes are partially transparent anyway, so let's apply this rule : We will only apply enough of a contrast increase on EITHER MATTE to ensure that the two mattes will ADD UP to white in the areas common to both mattes (ie NOT the fringes).

So to reiterate, we are theorizing that if the "white" areas of the color difference matte are actually gray, we are probably losing less detail than if we clamped them to white. Similarly, if the "white" areas of the detail matte are really gray, we are saving detail here as well. What matters is that when we sample the subject areas on either matte, they each need to be of sufficiently high contrast to guarantee that the subject areas will come to white WHEN THE MATTES ARE ADDED TOGETHER.

Our theory leads us to this further assumption : If we achieve this balance of opacity on the subject areas common to both mattes, it is likely that the fringe areas of the detail matte will be opaque enough to make the details visible, but sufficently transparent to preserve all the detail we need, without nasty compositing artifacts so common to keying.

Could it be true?

In my example case, I had to push the whites down about halfway on the detail matte.

It appears to me that I get more value out of adding contrast to the detail matte instead of the g-r one because the areas I am most concerned about are ONLY coverd by the detail matte - the wispy stuff. Just make sure you only increase the contrast enough to get solid coverage on the areas common to both mattes. Any more and you are going to start wrecking your fringes. In other words, if the wispy stuff is still partially transparent - that's ok! In fact, that's the whole idea.

Color Difference Matte

This is the other matte - the G-R color difference matte with blurring and eroding applied. Pay attention class, because this is really important! DON'T CLAMP YOUR COLOR DIFFERENCE MATTE TO BLACK AND WHITE!!! I left the subject area grey because it only need to be bright enough so that when I add the detail matte to it, the result (in the subject area) is pretty close to white. This is the key to retaining amazing detail.

Rough Summed Matte

When we add the two mattes together, here is the result. Some minor clamping with a ramp node tightened it up a bit :

Clamped Summed Matte

Side Note : Notice the white dot in the background. This is a tracking marker, which got removed in the "real" composite. Perhaps another day I will delve into the topic of tracking with Voodoo, garbage matting, and combining elements in 3D to add motion blur and/or focal blur.

Well, we're almost there. If my math makes sense, I should have a nearly perfect key. Before we find out, let's make sure the footage is spill-suppressed. If you want to learn how to do that, you can download the blend file or watch the video on greenscreen color-difference keying.

For the record, I used the detail matte for spill supression. I simply took the three color channels of my subject and multiplied the detail matte by the green channel at a factor of 0.4 to arrive at a modified, spill suppressed green chanel for the subject footage. Of course, this modified footage was ultimately used in the key.

Holy cow, that's some sweet spill suppression!

Here's the result :

It's positively unreal how all the wispy semi-transparent details of the subject were retained. This keying technique delivers a result that is, to my eyes, perfect. Of course, the final comp included color correction, focal blur, etc., but I trust you agree that this technique for pulling a key, not only works, but it makes sense.

Conclusion : Think, think, think, but then - SAMPLE, SAMPLE, SAMPLE! Know what your black and white levels are each step of the way. Use viewer nodes liberally to look at the grey levels in your mattes at each step so you only add as much contrast enhancement as is absolutely required in the final matte. If you do this, you will get a suberb matte as good as what you'll get with Primatte or Ultimatte, all right here in humble, open source Blender!

- Dave

How to Connect with Blended Planet Pictures :


For the icon-challenged :

Follow BlendedPlanet on Twitter 
BlendedPlanet Youtube channel 
Follow BlendedPlanet Pictures on Facebook

In a hurry to reach me?

Just call 260-489-4212 - you will get my voicemail, but leave a quick message and I will get back with you promptly. - Dave