View Single Post
Old 6th November 2011, 02:28   #341  |  Link
redfordxx
Registered User
 
Join Date: Jan 2005
Location: Praha (not that one in Texas)
Posts: 863
I made a function for convolutions, so I am sharing it here.
Code:
function Convolve(clip c, val hor, val ver, float "tot", bool "lsb", int "hctr", int "vctr", int "y", int "u", int "v", bool "info")
{
#Performs flat convolution of given dimensions...v0.1 by redfordxx 
#needed Dither 1.12
# issues: Dither_resize16 applies kernel in reverse 
#         Dither_resize16 normalizes kernel, so tot parameter is ignored in 16bits
#Usage parameters:
#hor, ver arguments...dimensions of the convolution or kernels
#tot...divider, negative values are relative to sum of elements, so for 3x3 convo and tot=-2 the resul is tot=18, tot=0 is automatic. Doesnot work in 16bits
#lsb...is input stacked 16bit?
#hctr,vctr...centerpoints of the convolution string, by default in the middle...ignored if given hor and ver are kernels
#info...shows convolution strings on screen
y=default(y,3)
u=default(u,3)
v=default(v,3)
htext = IsString(hor) ? hor : "" 
vtext = IsString(ver) ? ver : "" 
hi = IsString(hor) ? 1 : int(hor) 
vi = IsString(ver) ? 1 : int(ver) 
lsb=default(lsb,false)
info=default(info,false)
horcenter=default(hctr,(hi+1)/2)
vercenter=default(vctr,(vi+1)/2)
sum=hi*vi
tot=default(tot,0.0)
tot   = (tot>0)      ? tot
    \ : (tot<0)      ? -sum*tot 
    \                : 0
hcoef = 1
vcoef = 1
uh = " "+ string(hcoef)
uv = " "+ string(vcoef)
uhl =strlen(uh)
uvl =strlen(uv)
flt=((int(tot)!=tot)&&(lsb==false)) ? ".0" : ""
shl=(hi-2*horcenter+1)*(lsb ? -1 : 1 )          #Dither applies convolution in reverse 
svl=(vi-2*vercenter+1)*(lsb ? -1 : 1 )
strh=uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh+uh
strv=uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv+uv
str0=" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
hh=leftstr(str0,max(2*shl,0))+leftstr(strh,uhl*hi)+flt+leftstr(str0,max(-2*shl,0))
vv=leftstr(str0,max(2*svl,0))+leftstr(strv,uvl*vi)+flt+leftstr(str0,max(-2*svl,0))
hh=(htext=="") ? hh : htext
vv=(vtext=="") ? vv : vtext
hvsame=(hh==vv)   # when hor and vertical is same, do 16bit in one pass
c
(!lsb && tot==0) ? mt_convolution(horizontal=hh,vertical=vv,y=y,u=u,v=v) : nop 
(!lsb && tot>0)  ? mt_convolution(horizontal=hh,vertical=vv,total=tot,y=y,u=u,v=v) : nop 
(lsb && hh!="1" &&  hvsame) ? Dither_resize16 (Width(), Height()/2, kernel="impulse "+hh, fh=-1, fv=-1, center=false) : nop
(lsb && hh!="1" && !hvsame) ? Dither_resize16 (Width(), Height()/2, kernel="impulse "+hh, fh=-1, center=false) : nop
(lsb && vv!="1" && !hvsame) ? Dither_resize16 (Width(), Height()/2, kernel="impulse "+vv, fv=-1, center=false) : nop
info ? Subtitle(" h=("+hh+") v=("+vv+")") : nop
info ? Subtitle(" 16bit="+string(lsb)+"    total=("+string(tot)+")  size=("+string(hor)+","+string(ver)+")  center=("+string(horcenter)+","+string(vercenter)+") ",y=24) : nop
return last
}
I hope it is understandable what it does, so won't comment more, if you wanna know just play with parameters with info=true, see what is the difference eg on the following
Code:
convolve(5, 4, info=true)
convolve(5, 4, tot=-2, lsb=false, hctr=-3, vctr=1, info=true)
convolve(5, 4, tot=0, lsb=true, hctr=-3, vctr=1, info=true)
convolve("1 0 -1 0 1", 4, tot=0, lsb=false, hctr=-3, vctr=1, info=true)
There are two issues introduced by dither, see them in the comments
@cretindesalpes: I wonder whether these are intents or bugs
Now I believe that it was main reason why I was confused with using Dither_resize16

Last edited by redfordxx; 6th November 2011 at 09:02. Reason: Bug
redfordxx is offline   Reply With Quote