Thursday, 22 March 2007

How to do a Divide operation in After Effects

From time to time people find that some essential stuff is missing in AE (ok to be honest something is always missing in any program).
With that i don't mean plug in stuff you can build yourself with some effects etc.
i mean basic operations that a compositor needs ( i'm not starting to rant here about the lack of 16bit and 32bit effects) .

Everyone building their own colour difference keys or despill functions are irritated by the lack of an Subtract transfer mode for example.
I know there are other ways to do this but you still lose time and make your comps more complex.

A more rare one is a Divide operation. Actually it's an essential operation for working with images with premultiplied alpha channels, where one very often needs to divide the image by it's alpha channel to revert the premultiplication.
But as AE handles that in most cases in the Interpret Footage dialog and hides most of the whole concept from the user, not everybody knows of it's importance.
Ok, besides that, there are probably not that many situations where you might need it, but when you do, you're in trouble.
An example would be an Ultimatte style screen correction.

While fiddling with AE and "The Partially Transparent Alpha Pixel Problem", as described in Steve Wright's book "Digital Compositing for Film and Video", i found a workaround for Divide in AE.
AE has an un- premultiply operation outside of the the Interpret Footage dialog, one can use that for for ones Divide needs.
It's a bit far out, but it works.
Ok, here we go....

You have two grayscale layers A and B which you want to divide: A/B

Put both in a comp.
Add a "Channel-> SetMatte" Effect to A.
For "Take Matte From Layer" choose - B.
For "Use For Matte" choose - Red Channel,Green Channel or Blue Channel.
Uncheck all the other items

Add a "Channel-> Channel Combiner" to A.
Choose "From" - Straight to Premultiplied.
check "Invert"

Add another "Channel-> SetMatte" to A.
For "Use For Matte" choose - Full
Uncheck everything.

You just divided image A by the red, blue or green channel of image B.

Ok, why does this work?
Well first you set one of the color channels of image B as the alpha of
image A.
Then you added an effect where you can multiply a image with it's alpha channel (an alpha premultiply).
By checking "Invert" we use an alpha unpremultiply instead.
That means we divide the image (layer A) by its alpha channel (a channel of layer B).
In the last step we clear layer A of its temporary alpha channel by setting alpha to full (white, 1).

I'm sure you know how to make this work with non grayscale images if needed.

As a side note: "Channel Combiner" is only an 8bit effect.

2 comments:

Jesse said...

This divide does not work
I set up two solids, one with all values set to 100, the second with all values set to 2, so dividing should have ended with all values being 50, but they were all 255. your "workaroud" for dividing in AE does not work

Loga said...

Maths don't work like that in the cg image world.
In 8bit the calculations are made in normalized values, where the 8bit value of 0 is 0 and 255 is 1.
Normalized value= 8bit value/255

So in your example the math would be:
(100/255)/(2/255)=100/2=50

But the result isn't the 8 bit value of 50 it's the normalized value of 50.
In 8bit this values clips at 1 which is 255.
Hence your result.