Log in

View Full Version : help with polish expression


Motenai Yoda
26th February 2011, 14:13
Yo Guys, I need some help, this function should sharp with some limits. Can be optimized?

function BSharp(clip c, float "strength", float "lim", float "lim2", int "tlim",int "tlim2", bool "debug")
{
#~ v20f
strength = default( strength, 1.0 )
lim = default( lim, 10.0 )
lim2 = default( lim2, lim)
tlim = default( tlim, 16)
tlim2 = default( tlim2, 64)
debug = default( debug, false)
tlim= (tlim<0.0) ? 0.0 : tlim
tlim2= (tlim2<tlim) ? tlim + 2.0 : tlim2

stre=string(strength)
slim=string(lim)
slim2=string(lim2)

b1=c.removegrain(11,-1)#blur 1px radius
b2=b1.removegrain(11,-1)#blur 2px radius
b3=b2.removegrain(11, -1).removegrain(11,-1)#blur 4px radius

#use an inverted sigmoid curve to reduce sharp at noise level (lim2)
l1 = mt_lutxy(c, b1, yexpr = "x y - abs 1 < x - y "+slim2+" 4 < x y - x y - y x - "+slim2+" 2.0 / / exp x y - "+slim2+" 2.0 / / exp - y x - "+slim2+" 2.0 / / exp x y - "+slim2+" 2.0 / / exp + / abs * ? ? 0.5 * 128 +" , U = 1, V = 1)
l2 = mt_lutxy(c, b2, yexpr = "x y - abs 1 < x - y "+slim2+" 2 < x y - x y - y x - "+slim2+" / exp x y - "+slim2+" / exp - y x - "+slim2+" / exp x y - "+slim2+" / exp + / abs * ? ? 128 +" , U = 1, V = 1)
l3 = mt_lutxy(c, b3, yexpr = "x y - abs 1 < x - y "+slim2+" 1 < x y - x y - y x - "+slim2+" 2.0 * / exp x y - "+slim2+" 2.0 * / exp - y x - "+slim2+" 2.0 * / exp x y - "+slim2+" 2.0 * / exp + / abs * ? ? 0.5 * 128 +" , U = 1, V = 1)

sharped=mt_adddiff(l1,l2, U = 1, V = 1).mt_adddiff(l3, U = 1, V = 1)#.RequestLinear(rlim=5,clim=5)

#use a sigmoid curve to reduce sharp at edge level (lim)
limitstring=slim+" 0 == x 128 - x 128 - "+slim+" x 128 - / exp 1.0 "+slim+" x 128.0 - / exp / - "+slim+" x 128.0 - / exp 1.0 "+slim+" x 128.0 - / exp / + / abs * ?"
#use a composite function (parabola+parabola) to reduce sharp at dynamics edge (16=0,235=0,128=1.0)
parabola="x y 128 - x 128 == 1.0 x 16 <= 0.0 x 235 >= 0.0 x 128 < x 15.0 - x 2 ^ 256.0 / - 49.0 / x 256.0 * 4935.0 - x 2 ^ - 11449.0 / ? ? ? ? * +"

#use a motion mask to stabize sharp like soothe (tlim,tlim2)
tlimited=mt_lutxy(sharped, c.mt_motion(thy1=tlim, thy2=tlim2,thT=10, u=1,v=1) , yexpr = "x 128 == 128 "+limitstring+" 255.0 y - 255.0 / * "+stre+" * 128 + ? ", U = 1, V = 1)

return (debug) ? mt_lutxy(c,tlimited,parabola,u=128,v=128) : mt_lutxy(c,tlimited,parabola,u=2,v=2)
}

:thanks:

-Vit-
26th February 2011, 16:03
Are you asking to simplify the polish? The complexity of the polish expression in an mt_lut??? should have no effect on frame serving speed, unless you're calling this function from within a ScriptClip or similar. Those huge expressions would typically be evaluated once only at script start-up, and the actual frame processing done with a look up table (lut), which is the same speed for any expression. To speed this up you need less or faster operations, or to re-evaluate the algorithm itself. However, I don't have the time to look at it in detail.

Didée
26th February 2011, 17:14
Haven't fully evaluated the lut expressions, but the following in l1/l2/l3 seems fishy to me:

x y - abs 1 < x -

If abs(x-y)<1 Then (x-??)

There is no ?? that could be subtracted from x, or from which x could be subtracted.

Or am I misreading the expression?

Motenai Yoda
26th February 2011, 18:06
it should be (ie l2)
if(abs(x - y) < 1) then 0
elseif(lim2< 2) then x-y
else (x-y)*abs(tanh((x-y)/lim2))
+ 128

"x y - abs 1 < 128 "+slim2+" 2 < x y - x y - y x - "+slim2+" / exp x y - "+slim2+" / exp - y x - "+slim2+" / exp x y - "+slim2+" / exp + / abs * ? 128 + ?"

Didée
26th February 2011, 19:07
Okay ... it's easier to start from scratch.

Case l2. For tanh, I'm using tanh(z) = (exp(2z)-1) / (exp(2z)+1)

(edit - removed orphan leading paranthesis)

You have [ (x-y)/lim2 ] as argument for tanh. Thus, for tanh( (x-y)/lim2 ) I get the following in rev.Polish:
x y - lim2 / 2 * exp 1 - x y - lim2 / 2 * exp 1 + /


Puzzling together the following if(abs(x - y) < 1) then 0
elseif(lim2< 2) then x-y
else (x-y)*abs(tanh((x-y)/lim2))
+ 128

into rev.Polish, I get the following:
x y - abs 1 < 0 lim2 2 < x y - x y - x y - lim2 / 2 * exp 1 - x y - lim2 / 2 * exp 1 + / abs * ? ? 128 +


Written in MaskTools-usable string format, and comparing my result (top) to yours (below)
"x y - abs 1 < 0 "+slim2+" 2 < x y - x y - x y - "+slim2+" / 2 * exp 1 - x y - "+slim2+" / 2 * exp 1 + / abs * ? ? 128 +" # my result

"x y - abs 1 < 128 "+slim2+" 2 < x y - x y - y x - "+slim2+" / exp x y - "+slim2+" / exp - y x - "+slim2+" / exp x y - "+slim2+" / exp + / abs * ? 128 + ?" # your result

Motenai Yoda
28th February 2011, 21:02
thks so I've changed to, about 10% faster...
function BSharp(clip c, float "strength", float "lim", float "lim2", int "tlim",int "tlim2", bool "debug")
{
#~ v20i
strength = default( strength, 1.0 )
lim = default( lim, 10.0 )
lim2 = default( lim2, lim)
tlim = default( tlim, 16)
tlim2 = default( tlim2, 64)
debug = default( debug, false)
tlim= (tlim<0.0) ? 0.0 : tlim
tlim2= (tlim2<tlim) ? tlim + 2 : tlim2

stre=string(strength)
slim=string(lim)
slim2=string(lim2)

b1=c.removegrain(11,-1)#blur 1px radius
b2=b1.removegrain(11,-1)#blur 2px radius
b3=b2.removegrain(11, -1).removegrain(11,-1)#blur 4px radius

#use an inverted sigmoid curve to reduce sharp at noise level (diff/lim2)
l1 = mt_lutxy(c, b1, yexpr = "x y - abs 1 < 128 "+slim2+" 4 < x y - x y - x y - "+slim2+" / 4.0 * exp 1.0 - x y - "+slim2+" / 4.0 * exp 1.0 + / abs * ? 0.5 * 128 + ?" , U = 1, V = 1)
l2 = mt_lutxy(c, b2, yexpr = "x y - abs 1 < 128 "+slim2+" 2 < x y - x y - x y - "+slim2+" / 2.0 * exp 1.0 - x y - "+slim2+" / 2.0 * exp 1.0 + / abs * ? 128 + ?" , U = 1, V = 1)
l3 = mt_lutxy(c, b3, yexpr = "x y - abs 1 < 128 "+slim2+" 1 < x y - x y - x y - "+slim2+" / exp 1.0 - x y - "+slim2+" / exp 1.0 + / abs * ? 0.5 * 128 + ?" , U = 1, V = 1)

sharped=mt_adddiff(l1,l2, U = 1, V = 1).mt_adddiff(l3, U = 1, V = 1)

#use a sigmoid curve to reduce sharp at edge level (lim/diff)
limitstring=slim+" 0 == 1.0 2.0 "+slim+" * y 128 - / exp 1 - 2.0 "+slim+" * y 128 - / exp 1 + / abs ? *"

#use a composite function (parabola+parabola) to reduce sharp at dynamics edge (16=0,235=0,128=1.0)
parabola="x 128 == 1.0 x 16 <= 0.0 x 235 >= 0.0 x 128 < x 15.0 - x 2 ^ 256.0 / - 49.0 / x 256.0 * 4935.0 - x 2 ^ - 11449.0 / ? ? ? ? *"

plimited=mt_lutxy(c,sharped,"y 128 == x x y 128 - "+limitstring+" "+parabola+" "+stre+" * + ? ",u=1,v=1)

#use a motion mask to stabize sharp like soothe (tlim,tlim2)
tlimited=mt_merge( plimited, c, c.mt_motion(thy1=tlim, thy2=tlim2,thT=32, u=1,v=1) , U = 4, V = 4)

return (debug) ? mt_makediff(c,tlimited,u=128,v=128) : tlimited
}