View Full Version : Divide one clip by another?
Leo D9
12th April 2014, 15:17
I'm looking for a way to divide one clip by another without using any plug-ins.
I'll then multiply it by another clip, i.e.:
a = a/b * c
The a * c bit I can do with Layer() using "mul" mode, but the division seems impossible without using a plug-in. Why doesn't Layer() have a "div" mode, I wonder? From what's posted above it looks like I could use MaskTools, so I'll probably end up doing that, but I'm writing a script for other people to use so I'm trying to keep the plug-ins they will have to download and install to a minimum.
Furthermore, it doesn't have to be a "perfect" division, which got me wondering if there is any mathematical way to approximate division using only add, subtract and multiply functions, or anything else in AviSynth itself. There may be some way to achieve a/b by e^(ln(a)-ln(b)) using the gamma function of Levels(), but I'm not sure if/how that could work.
Thanks.
raffriff42
12th April 2014, 19:18
You are restricted to the 0-255 range when passing clips from filter to filter. Masktools is able to hold internal values far outside this range, and can do your a/b*c with a single function call. So, MaskTools is easier, far more powerful, and does not cause banding from repeated level manipulation.
Just for the heck of it though...## MaskTools Divide:
mt_lutxy(Base, Mask, "x 256 * y 1 + / ", U=2, V=2)
# http://docs.gimp.org/2.8/en/gimp-concepts-layer-modes.html#idp7563168
## Poor Imitation:
Overlay(Base.Invert, mask, mode="multiply").Invert
Overlay(Base, mode="chroma")
https://www.dropbox.com/s/doelihpcd0pjspo/mt_lut-divide-vs-overlay-768x432.jpg?raw=1
Leo D9
13th April 2014, 00:05
Thanks raffriff42, the MaskTools example looks good, but I'm a bit confused how the Overlay() would could achieve division. I see the MT one is doing:
(x*256) / (y+1)
But to me it looks like the Overlay example is doing:
256 - ((256 - x) * y)
Which is:
256 - 256y + xy
Or am I somehow misunderstanding how Overlay works here? Maybe there needs to be a third "mask" clip for the overlay function?
Thanks.
raffriff42
13th April 2014, 06:17
In Layer and Overlay, "Multiply" has an implied "divide" operation to bring the output within the range 0-255.
"multiply" := x * y / 255
"invert" := -x + 256
...etc.
It's all explained here: http://docs.gimp.org/2.8/en/gimp-concepts-layer-modes.html
bxyhxyh
13th April 2014, 08:00
If you really need to use internal functions I've made a list of "what we can do"
z=x+y - add
z=x-y - subtract
z=x*y/255 - multiply
z=(a*x+b*y)/2 - blend, a and b are constant numbers limited in [0,2] and a+b=2
z=min(x,y) - darken
z=max(x,y) - lighten
Also we can use
Invert() - 256-x
Tweak(bright=k,coring=false) - x+k, k is an integer limited in [-255,255]
Tweak(cont=k,coring=false) - x*k, k is a real number limited in [0,10]
Levels(coring=false) - [(x - input_low) / (input_high - input_low)]^(1/gamma) * (output_high - output_low) + output_low
Limitations and Notes
x,y and z are limited in [0,255]
Output of Overlay(), Tweak() and Levels() are limited in [0,255]. If you use them without care, result of imitation would be a wrong.
Can we imitate z=x/y using all of above?
SoftLight and HardLight seem not so useful for this. And anybody who knows formula of "exclusion"?
Leo D9
13th April 2014, 21:10
Yes, raffriff42, I missed out the /256 accidentally, but even with that it doesn't divide one clip by the other does it? It would be:
256 - ((256 - x) * y/256)
Both x and y are still numerators; my problem is getting one of them to be a denominator.
And, bxyhxyh, yeah, I've looked at the possible options but can't see a way of achieving division or even approximating it. I have a feeling that there may be strange way of achieving division using ConvertToRGB and ConvertToYUV2 but it would be quite lossy and inefficient.
colours
13th April 2014, 21:39
This wouldn't be hard if only Levels supported negative gamma. (It doesn't.)
Anyway, with Masktools v2, you can just use mt_lutxyz(clip1,clip2,clip3,"x y 1 + * z 1 + / round"). mt_lutxyz uses a fair amount of RAM, takes a considerable amount of time to initialise, and its use is generally discouraged (http://tp7ptr.wordpress.com/2013/09/13/masktools-luts/), but it works!
raffriff42
13th April 2014, 23:16
Look, Leo D9, my good friend, forget about not using plugins for this task. :)
Wouldn't you rather have double-precision floating point math (as per the link from colours - thanks for that) instead of 8-bit (per channel) integer math??
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.