« | »

Over Node

Thursday, February 11th, 2010 at 1:44 AM

Why does an over node in (insert compositing software here) not behave like I think it should?

I get asked this question a lot, so I thought I’d answer it here:

The answer is in the math. Over is defined as an expression: A+B(1-a). A is the rgb values of image A. B is the rgb values of image B. a is the alpha of image A and b, while not used in an over, is the alpha of image B.

take these two images:

rgb of image A rgb of image B

Our goal is to put the teapot, (image A) over the grass (image B). To do this, we need an alpha channel of image A to cut out the teapot:

alpha of image A

Simple enough right? If you just plug in the teapot with its alpha channel over the grass, we should see just that, right? wrong! you will see this:

unpremultiplied result

Here’s the problem: A+B(1-a). Let’s consider a point in the middle of the teapot. It as an rgb value greater than 0. it also has an alpha value of 1. therefore, A+B(1-1)=A. That’s great. we got just A. Now let’s consider a point we expect to be just grass. it also has an rgb value greater than 0, but an alpha value of 0. therefore, A+B(1-0)=A+B. Oh no! we didn’t want A+B. that means, we’re adding the rgb values together, which in this example produces a gray tint over the grass since we’re adding that gray value behind the teapot to the grass. The only case where this works is if the rgb of A is 0 where the alpha of A is also 0 since 0+B(1-0)=B.

In order to get the result we expect, we must first premultiply A by its alpha channel. This is expressed like this: A*a. Then, plug that in to the over expression and you get A*a+B(1-a). Now take the point over the grass again. plug everything in and you get 0+B(1-0)=B. Yes! That’s exactly what we wanted!

premultiplied result

That’s why you have to premultiply to use over correctly.

Tags:

This entry is filed under How To.

One Response to “Over Node”

  1. Nik says:

    Great………..

Leave a Reply