In this tutorial I’m going to take a detailed look at how the over operation works, and we’re also going to have a quick VFX history lesson along the way.
In my previous tutorial I explained how we made a premultiplied image. In this tutorial I’m going to explain why we need to premultiply an image if we want to composite it over another image. The two concepts work together, so you could consider this the second part of the previous article. If you are unsure what a premultiplied image is I suggest you read that first, and come back to this one.
Placing one image over another image is a fundamental operation in compositing. It is probably the most basic thing you’d want a compositing application to do. And it’s usually pretty easy to achieve. In After Effects you simply place your foreground layer on top of your background layer in the timeline. In Nuke you hit M for a merge node, which defaults to an over operation, and connect the foreground to the A input and the background to the B input. Composite. Done.
We do this operation so often that it’s easy to take it for granted. But what is the software actually doing to achieve this final result?
Understanding how an over works not only gives us some insight into what the software is doing, which allows us to troubleshoot issues, but it also gives us a look at the history of compositing, as the steps taken by the merge node in Nuke are the same steps taken for the very first VFX shots, created in camera over 100 years ago, and then for decades with optical printers before digital compositing came along.
So let’s have a quick history lesson.
Back in the days of film it was possible to expose several different images onto the same frame. You could film something, then rewind the film that you just used and expose something different onto the same frames. And because light is additive, you would end up with this double exposed look that had a sense of ghostly transparency.
We can achieve the same kind of look in Nuke using a merge node set to plus. When we do this we are literally adding the pixel values of the two images together.
If we merge this checkerboard and this constant together with a plus operation you can see what happens. The constant has a value of 0.5 in each channel. The checkerboard has three different values in the squares. The brightest squares in the grid are also 0.5, the middle tone squares have a value of 0.25 and the darkest squares are black, so have a value of 0.
After the plus operation the brightest squares and the constant give us a combined value of 1, because 0.5 + 0.5 = 1.
The middle squares with a value of 0.25 give us a value of 0.75 when added to the constant, because 0.25 + 0.5 = 0.75
The black squares are 0, so when we add that to the constant we are just left with the original value of the constant because 0 + 0.5 = 0.5
We can recreate this with two photographic images, one of a bear, and one of a bird. You can see that the bird image has been scaled down, so in the areas that are black the background is unchanged, but where the bird is added onto the background we get this ghostly semi-transparent look. And if we pick a pixel we can see that the result is the value of the background, plus the value of the foreground.
So let’s go back to our film camera setup. If we were to film a scene, which was deliberately dark in a certain area, then rewind the film and expose a second image, that only had details where the first scene was dark, we would have a controlled double exposure that looked like a composite of one image on top of another.
This was how some of the first visual effects were created, like this Georges Méliès film The Man With The Rubber Head.
But filmmakers could also use mattes to block the light from certain areas of the frame. When working in camera they would create these mattes with pieces of card, or by painting black paint on a piece of glass in front of the camera. These mattes would be static and couldn’t change during the shot.
There is an article on Fstoppers with some great gifs from old movies that illustrate this technique really.
Creating these effects in camera was risky because if you made a mistake with the second take, you would also ruin the first take, so filmmakers started using optical printers to create these effects as a post-production process. Optical printers allowed the operator to take multiple pieces of developed film, and combine them onto a new strip of film to create a new image. This was a safer way to work becuase you could use copies of the footage, rather than the original master.
Optical printers also allowed the creation of moving mattes, either through colour separation, or keying, techniques, and rotoscoping.
But these mattes still worked the same way. The foreground would be masked by the matte, then the matte would be inverted and applied to the background. Then the two layers would be exposed together as a double exposure.
FXGuide has a great article and video from a few years ago, that goes into much more detail about how an optical printer works. And there is a BBC Horizons documentary from the 80s about ILM used their optical printers to create the VFX for Indian Jones and Star Wars.
I think having an understanding of how things used to be done is really useful to help us appreciate what we have now, and helps us to think about different ways to approach problems we face today.
Now that we have seen how an over operation was done before the digital days, we can look at how it’s done in a computer, and it’s actually the same process.
We can use the same images as before, but this time I have a premultiplied version of the bird.
Because this bird layer has been premultiplied we can just use a merge node set to over to combine these two layers. This is the final image we are aiming for, but I’m going to break down the steps that the over operation is doing to get here.
We previously said that a double exposure in a film camera was equivalent to adding the two images together, so let’s change the merge operation from over to plus.
Now we can see that we have the same kind of double exposed result where our foreground image had values, but where the foreground image was black, there is no change to the background.
Remember that in the film examples they had to invert the mask used for the foreground layer and apply that inverted matte to the background? We can do that in Nuke too.
We know that because the foreground image has been premultiplied it contains the matte in the alpha channel, so we can invert this matte and apply it to the background in the same way we did to the foreground when we premultiplied it.
We can create a shuffle node in a new branch from the bird and shuffle the alpha into the red, green and blue channels. Now we need to invert this matte.
This is super simple with the invert node in Nuke. We can just connect that to the shuffle node and see that the matte had been inverted. What was black is how white, and vise versa. But let’s take a look at what is happening here. It is another example of something being so simple you can work out the result in your head. If you hover over the tool tip in the invert node it tells you exactly what it is doing ‘replaces the selected channels with 1-channel’. So for each pixel the node is doing the calculation: 1 – pixel value.
If we start with a pixel that has a value of 1, and we subtract it from 1 we get 0.
If we start with a pixel that has a value of 0 and subtract it from 1 we are left with 1.
So you can see that this would flip black to white and white to black. So where the foreground is fully opaque, we will have nothing left of the background, and visa-versa.
Now let’s look at the values in between zero and one.
If we look at the pixels that started with a value of 0.5, which represents 50% transparency, we can see that 1 – 0.5, gives us 0.5. So where we have 50% of the foreground, we will also have 50% of the background.
If we look at the area that currently has values of 0.25, we can see that 1 – 0.25 will give us 0.75. So where we have 25% of our foreground, we will end up with 75% of our background.
So the percentage of the background plus the percentage of the foreground at any point of the image will always be 100%
So, now we have an inverted version of the matte used for the foreground, and now we need to apply this to the background. We can use a merge node set to multiply.
Now we have the foreground that was already masked by the matte, and now we have the background masked by the invtered matte, just like the double exposure setup in the film camera. So now all we need to do is plus the two layers together. Let’s do that with a merge node set to plus.
And if we compare this to the over node we can see that we have the identical result.
Let’s quickly recap the steps we took to merge a premultiplied image over a background.
First we inverted the alpha channel from the premultiplied foreground.
Then multiplied each channel of the background by this inverted matte.
Then we added the two layers together to create our final composite.
So as you can see the over operation isn’t complicated. We can work out the maths in our heads.
I think it’s important to realise that a lot of what Nuke is doing is actually quite simple. It’s just doing it really fast. But we can understand it, and that helps us solve problems and have a better idea of how to approach things.
If you have any questions about this explaination, please let me know in the comments below.
A few years ago I spent a lot of time doing just this, thinking about and breaking down the optical techniques. And I realized that all the digital stuff is just the optical stuff but digitized. And everything we do today was built upon those techniques. The software started there. Granted some new stuff came along the way, but much of that builds on the existing software which built on the opitcal stuff.