Introducing: the Blender "Überkeyer"...
This keyer was developed while making the zombie short film of the Physiker-Theater film team (2010–2014). Slides of the lightning talk at LGM 2014: here.
when we started our project, our master of story design thought, well let's write an easy story, which we can actually do.
A story, where everything is very dark most of the time and the only, small thing, we want to do, is twist space time a bit and make our actors, well, have a different size as the background. (That's 110 percent likely to happen one day or the other, belief me, I'm a scientist... )
A process, which either requires a good set artist, who can build several convincing sets in different sizes (we don't had one, since all of us are theoretical physics students / post docs / working people, who are in their heart still theoretical physicists, which means regarding traditional craftsmanship: mentally challenged :) )
So: we had to decide: scrap the whole story or find a solution which will teach us, how to work with a lot of greenscreen footage (in our case: around 11 minutes, 19000 frames). Everyone is doing it right now, why shouldn't we learn how to do it?
Well, we learned a lot, and probably made all the mistakes in the book at once...
If you ever read some tutorials on greenscreen work, you may have worked out a checklist, what you shouldn't do:
- use an inappropriate recording device that messes up all your edges by compressing the footage too hard (we used a Canon EOS 5D), CHECK
- use the wrong light to light your greenscreen (we used work lights with some baking parchment as a diffusion helper), CHECK
- don't forget additional fill lights to light away greenscreen reflections on the actors coming from the side or the front, CHECK
- don't let your actors stand too close to the greenscreen, CHECK
- don't let anything stand too close to the greenscreen (like additional props, large desks, etc), CHECK
- avoid shadows on the greenscreen at all cost, CHECK
- don't put a spot light directly into the scene pointing into the camera, thereby screwing up nearly all of the screen by the resulting lense flare, CHECK
- don't let your actors walk outside the greenscreen area, CHECK
- try to rewrite your story, so that your main actors don't have long flying hair / half transparent areas, which you might have to roto out later and won't look very convincing with most keying software packages, CHECK
- don't use green objects in front of a green screen, CHECK
- don't use tracking markers, that you can't remove easily afterwards automatically. Well, that we actually did properly, we used blue ones :)
Do we regret that? Well, uhm, doing things properly in the first place is for wimps, isn't it?
And: welcome to reality, and sure: it's good to know, that some of this can be avoided, but the real fun starts, if you find a way to deal efficiently with all of those problems at once.
And that's what this blog post will be all about: learning how to deal with the things, everybody knows how to avoid, but also knows, that the reality on set (time and money constraints in a low to no budget production) makes the team break these rules one or the other way.
If you did everything properly, you might not learn a lot from the rest of this article. You might learn maybe, how you can make your properly lit and worked out footage work even better.
TL;DR: here is the blend-file for the keyer.
Still with us? Or want to learn, how this keyer really works? (You most probably need to anyways...)
So let's start with the last point on the check list:
Long hair
As you might have noticed, when you played around with different keying software functions, what seperates the boys from the men is edge quality.
Nearly every keying algorithm can handle solid areas, but getting the half transparent stuff right is the holy grail.
You also might have read, that the best keying algorithm for this job is the channel keyer, which boils down to the following formula:
Alpha-Matte = Green-Channel - MAX(Red-Channel, Blue-Channel)(If you got red or blue-screen, change channel names accordingly.)
This is mentioned so in Steve Wrights book "Digital Compositing for Film and Video" and besides the fact, that this book is a really great read on the subject (it is in fact, the only book on that subject, I've found), it's wrong.
Ouch, that's a bummer.
Some may scream: "Sure! I knew it, use a difference keyer!" But that's actually a right point from the book: a difference keyer is worse, *if* you are working with greenscreen footage. (Difference keyers have there places, where they can shine, but it is *not* greenscreen footage!)
Have you ever worked with Prelight or IBK-Keyer in Nuke? (If you haven't, take a look at this introduction video and come back later: http://www.youtube.com/watch?v=-GmMC0AYXJ4 )
Did you notice, that there are some strange parameters, you haven't seen before, which are called "Red Weight" and "Blue Weight"?
And: that the edge quality of both keyers is way better than everything you ever got out of Blender's (or any other software packages) internal keyers?
Luckily, someone out there worked out the formula behind it and put it on the net ( http://www.vfxtalk.com/archive/index.php/t-16044.html ) :
Alpha-Matte = Green-Channel - RedWeight * Red-Channel - BlueWeight * Blue-ChannelWhere RedWeight = BlueWeight = 0.5 giving optimal edge results.
Hmm, looks quite similar, right? It is, but it is simply not the same thing and works a hell of a lot better. Why this is the case, is still a little mystery to me (well somewhat: it subtracts a weighted average of the other channels, which seems quite logical.). Obvious advantages are: it is definitely linear in all cases, so switching the most significant channel in a non-linear way (there is a call to MAX() in the other formula), won't happen. It gives you some control over the amount of influence by the two channels red and blue (so it already includes the nifty channel lifting preprocessing trick for free, you also might know from Steve's Book).
Until we figure it out, let's just stick to the (empirically proven) fact, that it works great.
Let's continue with the next point:
Avoid shadows at all costs, appropriate lighting etc. .
This is straight by the book (Steve Wright's one): build a defect map by rebuilding the green screen behind the actors (the book says: go, use photoshop, paint it), pull a key to get the defect map, divide by the defect map, you are done:
Alpha = (Green_FG - RW * Red_FG - BW * Blue_FG) / (Green_BG - RW * Red_BG - BW * Blue_BG)Where *_FG means color channels of the foreground and *_BG means color channels of the backgrounds.
Sounds great, works indeed great, but adds some additional problems, since, well, who said, that Alpha is now in the range 0...1?
We will handle this later at another point in the pipeline, since clamping at this moment is the wrong way of handling it! (I learned the hard way.)
Another problem: how on earth do we get to the proper green screen?
Painting all the backgrounds with Photoshop? Making sure, all of the background is filmed as a seperate plate? Yeah, that's gonna happen.
Luckily enough, there is a different way of doing it, and it's even automatic.
The point of this whole rebuilding the background mumbo jumbo is, you guessed it, to make the half transparent areas come out right.
Nothing more, nothing less. So the interesting stuff happens not within the solid area behind the actors (which would also be the place, where you can't easily get the necessary information ... well, maybe you are super man/woman and have that x-ray vision ... but until that happens, you simply *don't* know, what's behind the actors). So that's actually good news. And offers the solution: find a way, to cut out the foreground in a very rough way and just try to guess the background on the edge regions.
Okay: let's stick to the first part: cutting out the foreground in a rough way. Essentially you could use any keyer you like (or even draw some rough masks, whatever you like best) or, and that worked best for me: use the same keyer to do the job.
By chosing RedWeight and BlueWeight carefully (most of the time very high values), choosing a solid green color for the input (most of the time, a very dark green to capture all the shadows) feeding the output into a threshold node (math node "greater than") and eroding the mask a bit (depending on image contents between 10 and 30 steps) and finally inverting the whole thing will give you a nice and dendy green screen and holes at the places where your foreground was once.
So: still there are holes...
There is a new node in Blender for quite a while called "Inpaint".
Inpainting is the art of restoring image regions of unknown content by either looking on the existing edges or by considering the whole picture and make that look "good". There are pretty advanced algorithms out there, that can make the result completely seemless. But that's for removing people from photographs.
Blender's inpaint node isn't exactly clever, it's a simple edge convolver, but: it's a very fast one and clever isn't exactly the thing needed here. We need predictable, so that everything works well with animations, we only should consider the edges and we need fast, and that's all things, that the inpaint node does quite well. (You can inpaint with 2000 steps easily and the algorithms does run approximately the same speed as if you were just using one step...)
So we did everything like we mentioned it above and we notice that, well our matte still looks *bad* at some places, we haven't expected it to behave bad:
- the edges look somewhat right, but most if not all of them seem way to bright.
- the actors still have that greenish look from either the green shining through their hair or reflections from the side.
We would have to calculate the following compositing equation:
Result = Foreground * Alpha + Background * (1 - Alpha)Let's consider for one moment, that the thing we finally filmed (the Result) was actually created artificially using the equation above.
So the Background would be actually the Greenscreen, the Result is the thingy we got on tape (or memory card in our case) and Alpha is the Matte we just extracted.
Only thing left to do: solve for "Foreground" or in other words, subtract the Greenscreen!
If we try this naivly with the formula mentioned above, something really strange / bad happens. Remember that I mentioned, that optimal edge results are only met, when Red Weight and Blue Weight both have the value of 0.5 ?
So: what happens, if you have to choose different values, since your footage is a little bit more challenging (read: all of your footage).
The colors are totally off and the edges really look like shit (since you have to choose higher values for the weights most of the time, it's way too dark).
Hmm, damn it. Okay, we got the cake, it is sitting right before us on the table, but someone decided, that we aren't allowed to eat it.
Luckily, noone really said, that you have to use the same weights for the subtraction process and the matte generation process.
So the solution is the following: we build two pipelines: one for the matte generation and another one for the greenscreen subtraction process.
This works really great (as it always does, if you are allowed to actually have the cake and eat it) and: it does green despilling for free.
And that is also the right moment, to reconsider, when we actually have to clamp the matte values to there safe range 0...1.
We actually don't, but clamp instead the value we *subtract* to the range 0...1. That leaves all the colors intact and removes all of the spill!
So almost there, let's move to the first point of our haunting check list of green screen doom:
The wrong recording device.
Wrong device means: we got compression artefacts all over the place and restoring the greenscreen behind the actors will also fail over the noise generation problem, since the noise added by sensor and compression algorithm will end up in the fact that your carefully generated matte is looking very noisy at the edges. (This will also happen with a proper recording device but using a 5D just adds to the problem.)
People will tell you, that you have to do some blurring, but I for myself prefer to work in the image detail restauration business not image destroying business and you should do the same.
So what can be done? Well, you might have noticed, that we didn't have to do one thing, that is always done as a last step after pulling a matte: we didn't scale it. That's actually a good thing, since our defect map processing above, did in fact scale the matte in a rather optimal way depending on the background (but it's only optimal in the sense, that you were in the possession of the correct green screen, which you were not).
So: let's admit it: we have to do some matte scaling which is the same thing as choosing the black and white point of the matte to throw the noise away.
The good thing is: we don't have to do that for all of the picture regions in the same way.
One thing that has proven very powerfull is doing some mask animation on the blackpoint values of the final matte. (For locked of shots: no animation.)
Most of the time, the shadowy regions are located very low around the legs of the actors. And the high detail regions are located very high and most of the time, those don't have that much of the ugly defects that can mess up your whole matte here.
Another good news: if you choose the blackpoint in reasonable value ranges (0...0.3), probably noone notices, that you have thrown away some of the details.
And: in the problem areas you can drive it up to around 0.6 to 0.7, granted that you don't have a lot of half transparent stuff sitting around. (Read: legs, no hair)
So what's left?
The pesky detail with green spill / reflection that came from the side or the foreground.
Let's get straight to the solution (which is a variation of the hard matte / soft matte combo mentioned in Steve's book):
Yes, we got some additional green on the foreground, no, it isn't most likely as much green as we will find on the background.
So: we pull another matte, using the same keyer, choose different values for Red and Blue Weight again, so that we got a very hard matte, erode that matte, so that no green from the greenscreen is left and consider this area as reflection only.
There is only one small twist to it: you can't make that work for both, very large half transparent hair regions and body regions at the same time with the same set of parameters, so we add the same thing twice and add some masks to seperate both areas.
A lot of shots don't need that, but if your main characters have, well, a lot of hair flying around, then, you need it most of the time.
What if your actors walk out of the greenscreen area?
And... you don't notice it, until it is too late (read: everybody is home, no time for reshoot).
Depends on the background. I haven't mentioned above, but the same process (dividing a defect map, subtracting the green screen) also works with other keying methods. The method of choice for background stuff without any color / with strange colors in the background is either a difference keyer or a luma keyer.
Since the areas, where that happened for us were either very bright or very dark (yeah, I know, matte boxes shouldn't be visible, same goes for microphones...), I've chosen the later and added it with additional masks, so that you can activate it, if need arises.
Needless to say, that edge quality will suffer in the regions where you use it, but it is still better than roto, right?
Last but not least, what's about green stuff in the foreground?
Animate a holdout mask. This shouldn't be a surprise, right? :)
And, what can I do, if some parts of my background are complete bullsh*t, because of microphone, other stuff, etc?
Animate a garbage matte. Again: no surprise, right?
And, what can I do, if some parts of my foreground are complete bullsh*t, because of, well, garbage there?
Okay, let's talk about wire removal. There is a seperate nodegroup for it, that does the magic properly with the inpaint node, I already mentioned above. You have to animate the proper mask (or generate with any method you like).
For small regions, it will work quite well, for larger regions, the simple inpaint algorithm won't be enough. Feel free to add better ones to the blender patch tracker and I'll happily review and commit them...
Doing the final composite - within Blender Cycles
In a total abuse of the blender cycles engine, we do the actual composite within cycles. And, what can I say, cycles does it really great :)



