PDA

View Full Version : Contrasharpening and dft a little help please


rkalwaitis
22nd October 2009, 10:56
Ive been playing around with Motion Compensated DFT (nice little script made by TWC). Anyways I wanted to add sharpening options to it similar to the fashion done by Spuds in his now famous script.

Here is what I put. I can not get contrasharpening to work. Im sure its something simple, but Im not clever enough to find it.

thanks in advance K.

function dfttestMC (clip input, clip "pp", bool "Y", bool "U", bool "V", int "ftype", float "ss_x", float "ss_y",float "sigma", float "sigma2", float "pmin", float "pmax",
\ int "sbsize", int "smode", int "sosize", int "tbsize", int "tmode", int "tosize", int "swin", int "twin", float "sbeta", float "tbeta", bool "zmean",
\ string "sfile", string "sfile2", string "pminfile", string "pmaxfile", float "f0beta", string "nfile", int "mcradius", bool "mdg", int "mdgthSAD",
\ int "thSAD", int "thSCD1", int "thSCD2", int "blksize", int "pel", int "overlap", int "dct", int "search", int "sharpp", int "ls_x", int "ls_y", int "lsfstr")
{

ls_x = default( ss_x , 1.25 ) # LSF Smode=4 default
ls_y = default( ss_y , 1.25 ) # LSF Smode=4 default
lsfstr = default( lsfstr , 80 ) # Strength parameter in LSF
sharpp = default( sharpp , 1 ) # sharpening function to use 1=contra, 2=limitedsharpenfaster, 3=off
sharpp = (sharpp < 0) ? 0 : (sharpp > 4) ? 4 : sharpp # which sharpening function to use in the script
sharp_clp = : (sharpp == 3) ? input.ContraSharpening(degrained,input) \
: (sharpp == 2) ? input.limitedsharpenfaster(ss_x=ls_x, ss_y=ls_y, strength=lsfstr) \
: (sharpp == 1) ? input.LSFMod() : input

pp = default(pp, input)

# mvtools-related options
mcradius = default(mcradius, 2)
mcradius = Max(Min(mcradius,5),1)
mdg = default(mdg, true)
overlap = default(overlap, 2)

# MSuper
pp_super = pp.MSuper(pel=pel)

# Motion vector search
b5vec = (mcradius>=5) ?
\ MAnalyse(pp_super,isb=true,search=search,delta=5,overlap=overlap,blksize=blksize,dct=dct) : NOP
b4vec = (mcradius>=4) ?
\ MAnalyse(pp_super,isb=true,search=search,delta=4,overlap=overlap,blksize=blksize,dct=dct) : NOP
b3vec = (mcradius>=3) ?
\ MAnalyse(pp_super,isb=true,search=search,delta=3,overlap=overlap,blksize=blksize,dct=dct) : NOP
b2vec = (mcradius>=2) ?
\ MAnalyse(pp_super,isb=true,search=search,delta=2,overlap=overlap,blksize=blksize,dct=dct) : NOP
b1vec = MAnalyse(pp_super,isb=true,search=search,delta=1,overlap=overlap,blksize=blksize,dct=dct)
f1vec = MAnalyse(pp_super,isb=false,search=search,delta=1,overlap=overlap,blksize=blksize,dct=dct)
f2vec = (mcradius>=2) ?
\ MAnalyse(pp_super,isb=false,search=search,delta=2,overlap=overlap,blksize=blksize,dct=dct) : NOP
f3vec = (mcradius>=3) ?
\ MAnalyse(pp_super,isb=false,search=search,delta=3,overlap=overlap,blksize=blksize,dct=dct) : NOP
f4vec = (mcradius>=4) ?
\ MAnalyse(pp_super,isb=false,search=search,delta=4,overlap=overlap,blksize=blksize,dct=dct) : NOP
f5vec = (mcradius>=5) ?
\ MAnalyse(pp_super,isb=false,search=search,delta=5,overlap=overlap,blksize=blksize,dct=dct) : NOP

# Optional MDegrain
super = input.MSuper(pel=pel,levels=1)
degrained = (mcradius>=3 && mdg) ? input.MDegrain3(super,b1vec,f1vec,b2vec,f2vec,b3vec,f3vec,thSAD=mdgthSAD,thSCD1=thSCD1,thSCD2=thSCD2) :
\ (mcradius==2 && mdg) ? input.MDegrain2(super,b1vec,f1vec,b2vec,f2vec,thSAD=mdgthSAD,thSCD1=thSCD1,thSCD2=thSCD2) :
\ (mdg) ? input.MDegrain1(super,b1vec,f1vec,thSAD=mdgthSAD,thSCD1=thSCD1,thSCD2=thSCD2) : input

# flow
degrained_super = (mdg) ? degrained.MSuper(pel=pel,levels=1) : super
b5clip = (mcradius>=5) ?
\ degrained.Mflow(degrained_super,b5vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
b4clip = (mcradius>=4) ?
\ degrained.Mflow(degrained_super,b4vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
b3clip = (mcradius>=3) ?
\ degrained.Mflow(degrained_super,b3vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
b2clip = (mcradius>=2) ?
\ degrained.Mflow(degrained_super,b2vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
b1clip = degrained.Mflow(degrained_super,b1vec,thSCD1=thSCD1,thSCD2=thSCD2)
f1clip = degrained.Mflow(degrained_super,f1vec,thSCD1=thSCD1,thSCD2=thSCD2)
f2clip = (mcradius>=2) ?
\ degrained.Mflow(degrained_super,f2vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
f3clip = (mcradius>=3) ?
\ degrained.Mflow(degrained_super,f3vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
f4clip = (mcradius>=4) ?
\ degrained.Mflow(degrained_super,f4vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP
f5clip = (mcradius>=5) ?
\ degrained.Mflow(degrained_super,f5vec,thSCD1=thSCD1,thSCD2=thSCD2) : NOP

# Create the flow clip
interleaved = (mcradius==5) ? Interleave(f5clip,f4clip,f3clip,f2clip,f1clip,degrained,b1clip,b2clip,b3clip,b4clip,b5clip) :
\ (mcradius==4) ? Interleave(f4clip,f3clip,f2clip,f1clip,degrained,b1clip,b2clip,b3clip,b4clip) :
\ (mcradius==3) ? Interleave(f3clip,f2clip,f1clip,degrained,b1clip,b2clip,b3clip) :
\ (mcradius==2) ? Interleave(f2clip,f1clip,degrained,b1clip,b2clip) :
\ Interleave(f1clip,degrained,b1clip)

# Perform dfttest
filtered = interleaved.dfttest(Y,U,V,ftype,sigma,sigma2,pmin,pmax,sbsize,smode,sosize,tbsize,tmode,tosize,
\ swin,twin,sbeta,tbeta,zmean,sfile,sfile2,pminfile,pmaxfile,f0beta,nfile)

output = filtered.SelectEvery(mcradius*2+1,mcradius)

return(output)
}

FUNCTION ContraSharpening(clip denoised, clip original)
{
# contra-sharpening: sharpen the denoised clip, but don't add more to any pixel than what was removed previously.
# script function from Didee from the VERY GRAINY thread
s = denoised.minblur(1,1) # Damp down remaining spots of the denoised clip.
allD = mt_makediff(original,denoised) # The difference achieved by the denoising.
ssD = mt_makediff(s,s.removegrain(11,-1)) # The difference of a simple kernel blur.
ssDD = ssD.repair(allD,1) # Limit the difference to the max of what the denoising removed locally.
ssDD = SSDD.mt_lutxy(ssD,"x 128 - abs y 128 - abs < x y ?") # abs(diff) after limiting may not be bigger than before.

denoised.mt_adddiff(ssDD,U=2,V=2) # Apply the limited difference. (Sharpening is just inverse blurring.)

RETURN (last)
}

Hopefully after I learn my lesson on this problem I can play with masks. :eek:

Didée
22nd October 2009, 11:24
You have both functions. Then you do like:

orig = last
denoised = orig.dfttestMC(...)
result = ContraSharpening(denoised,orig)

Easy, isn't it. Or did I miss something?

rkalwaitis
22nd October 2009, 11:28
I knew it was something simple. Thanks Didee, you rock.

rkalwaitis
22nd October 2009, 12:46
I guess not so easy, I am getting invalid arguments for contrasharpening. Do the values you gave me go in the Contrasharpening function or the dfttestmc function?

Probably confusing myself.

thanks
k

Didée
22nd October 2009, 13:09
Ah, I didn't see this in the 1st function:
sharpp = (sharpp < 0) ? 0 : (sharpp > 4) ? 4 : sharpp # which sharpening function to use in the script
sharp_clp = : (sharpp == 3) ? input.ContraSharpening(degrained,input) \
: (sharpp == 2) ? input.limitedsharpenfaster(ss_x=ls_x, ss_y=ls_y, strength=lsfstr) \
: (sharpp == 1) ? input.LSFMod() : input
At that point, there is no clip "degrained" defined, so it'll bail out there when sharpp=3. Hmh, but shouldn't that give "don't know 'degrained'" error, instead of "invalid arguments" ... ?

The script is confusing anyways ... that posted section creates a clip "sharp_clp", which is not used anywhere later in the script ...

Puzzling together a script with snipplets from here, there and elsewhere, it seems?

rkalwaitis
22nd October 2009, 13:23
You are right a bit from here and there, I will try to clean it up and see what happens. Thanks for your help.

Me.

Didée
22nd October 2009, 13:31
Solution is easy. Starting with the original function (http://forum.doom9.org/showthread.php?p=1308269#post1308269) of TWC, make these edits:

function dfttestMC (clip input, clip "pp", bool "Y", bool "U", bool "V", int "ftype", float "sigma", float "sigma2", float "pmin", float "pmax",
\ int "sbsize", int "smode", int "sosize", int "tbsize", int "tmode", int "tosize", int "swin", int "twin", float "sbeta", float "tbeta", bool "zmean",
\ string "sfile", string "sfile2", string "pminfile", string "pmaxfile", float "f0beta", string "nfile", int "mcradius", bool "mdg", int "mdgthSAD",
\ int "thSAD", int "thSCD1", int "thSCD2", int "blksize", int "pel", int "overlap", int "dct", int "search",
\ bool "contrasharp")
{
pp = default(pp, input)
contrasharp = default(contrasharp,false)

# mvtools-related options
mcradius = ...

...
...

output = filtered.SelectEvery(mcradius*2+1,mcradius)

output = contrasharp ? ContraSharpening(output,input) : output

return(output)
}
This way, the possibility of contra-sharpening is correctly baked-in as an option. No need anymore to use it "externally" as I had shown above.

rkalwaitis
22nd October 2009, 13:55
Works just fine, thanks Didee. Im still gonna try to clean this up and use as a learning tool. Thanks again