View Full Version : Request: translate avisynth NonlinUSM to vapoursynth function
poisondeathray
20th February 2017, 17:56
I find this a useful function for some situations when you want LCE . It would be nice to have in vapoursynth-ese :)
Can someone please translate it ?
Thanks!
PS I'm aware there is a retinex function, but it doesn't quite do what I want
original reference
https://forum.doom9.org/showthread.php?p=1555234#post1555234
function NonlinUSM(clip o, float "z", float "pow", float "str", float "rad", float "ldmp")
{
z = default(z, 6.0) # zero point
pow = default(pow, 1.6) # power
str = default(str, 1.0) # strength
rad = default(rad, 9.0) # radius for "gauss"
ldmp= default(ldmp, 0.001) # damping for verysmall differences
g = o.bicubicresize(round(o.width()/rad/4)*4,round(o.height()/rad/4)*4).bicubicresize(o.width(),o.height(),1,0)
mt_lutxy(o,g,"x x y - abs "+string(z)+" / 1 "+string(pow)+" / ^ "+string(z)+" * "+string(str)+
\ " * x y - 2 ^ x y - 2 ^ "+string(ldmp)+" + / * x y - x y - abs 0.001 + / * +",U=2,V=2)
#interleave(o,last) # just for visualisation, you don't want the function to do this
return(last)
}
Are_
20th February 2017, 20:50
I will just leave it like this, but original defaults look wrong (super over-sharpened). Maybe something behaves different? I can't use avisynth so I can't compare.
def nonlinusm(clip, zero_point=6.0, power=1.6, strength=1.0, radius=9.0, ldmp=0.001, show=False):
expr = 'x x y - abs {z} / 1 {p} / pow {z} * {s} * x y - 2 pow x y - 2 pow {l} + / * x y - x y - abs 0.001 + / * +'
expr = expr.format(z=zero_point, p=power, s=strength, l=ldmp)
gray = core.std.ShufflePlanes(clip, planes=[0], colorfamily=vs.GRAY)
last = core.resize.Bicubic(gray, round(clip.width / radius / 4) * 4, round(clip.height / radius / 4) * 4)
last = core.resize.Bicubic(last, clip.width, clip.height, filter_param_a=1, filter_param_b=0)
last = core.std.Expr([gray, last], expr)
last = core.std.ShufflePlanes([last, clip], planes=[0, 1, 2], colorfamily=clip.format.color_family)
if show is True:
last = core.std.Interleave([core.text.Text(clip, 'Source clip'), core.text.Text(last, 'Processed clip')])
return last
jackoneill
20th February 2017, 20:51
Maybe like this:
def NonlinUSM(o, z=6.0, pow=1.6, str=1.0, rad=9.0, ldmp=0.001):
from vapoursynth import core
g_width = int(o.width / rad / 4 + 0.5) * 4
g_height = int(o.height / rad / 4 + 0.5) * 4
g = core.resize.Bicubic(o, width=g_width, height=g_height)
g = core.resize.Bicubic(g, width=o.width, height=o.height, filter_param_a=1, filter_param_b=0)
expression = "x x y - abs {z} / 1 {pow} / pow {z} * {str} * x y - 2 pow x y - 2 pow {ldmp} + / * x y - x y - abs 0.001 + / * +".format(z=z, pow=pow, str=str, ldmp=ldmp)
return core.std.Expr(clips=[o, g], expr=[expression, "", ""])
poisondeathray
20th February 2017, 21:59
Thanks guys.
I'm missing something very basic here. What is the namespace or how do you call it ?
I put jackoneill's NonlinUSM as a python script in my site-packages directory
eg. image test
import vapoursynth as vs
import NonlinUSM
core = vs.get_core()
v = core.imwrif.Read(r'F:\test\test.png')
v = core.resize.Bicubic(v, matrix=1, format=vs.YUV444P8)
#v = NonlinUSM.NonlinUSM(v)
v.set_output()
Newbie Question#2 : How do I call or import a vapoursynth function directly in a script , as opposed to import / site-packages method ? For example, with avisynth you could Import() some .avs then call the function similar to what is being done with the site-packages method; or you could put the function definition directly in the script instead of using Import() and call the function directly
(and yes, the avisynth version has defaults set to strong for most sources)
Thanks
jackoneill
20th February 2017, 22:52
That is how you call it. Does it not work?
Edit: Nevermind. I guess you have VapourSynth R36 or older. Change the import line in the function to this:
def NonlinUSM(.....):
import vapoursynth as vs
core = vs.get_core()
If you want to import a module from a non-standard location, you have to modify sys.path first:
import sys
sys.path += "."
import NonlinUSM
This lets you import modules located in the same folder as this script.
poisondeathray
20th February 2017, 23:00
Yes that was it. Older version
It works now.
Thanks again
jackoneill
20th February 2017, 23:39
Are_'s version should be faster than mine, because it doesn't resize the chroma channels.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.