Advertisement
torchlight

SNN (symmetric nearest neighbour) filter

Jun 30th, 2014
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* SNN (symmetric nearest neighbour) filter; cf. http://subsurfwiki.org/wiki/Symmetric_nearest_neighbour_filter
  2.  
  3. This is some sort of a denoising filter with an edge enhancing effect. What's implemented here is not strictly an SNN filter, but instead the pixels in each pair are weighted by the negative fourth power of their differences relative to the reference pixel and blended. If edge enhancement is not desired, setting ee=false takes the median of the reference pixel and the pixels in the pair (i.e. the closer of the two pixels, unless their differences have opposite sign).
  4.  
  5. Why did I even write this filter?
  6.  
  7. int radius: filtering radius (default 1)
  8. bool ee: edge enhancement (default true)
  9. string chroma: chroma processing; if not set to "process", the returned clip is Y8 (default "process")
  10.  
  11. Dependencies: ApplyEvery, Masktoolsv2, RgTools. Avisynth 2.6 or Avisynth+ also required.
  12. */
  13.  
  14. function snn(clip c,int "radius",bool "ee",string "chroma")
  15. {
  16.     chroma = default(chroma,"process")
  17.     r = default(radius,1)
  18.     ee = default(ee,true)
  19.     function shift(clip c,int x,int y) {c.pointresize(c.width(),c.height(),x,y)}
  20.     function closer(clip a,clip b,clip c,bool ee)
  21.     {
  22.         ab = mt_makediff(a,b)
  23.         ac = mt_makediff(a,c)
  24.         mindiff = mt_lutxy(ab,ac,"x 128 - y 128 - * x 128 - 3 ^ y 128 - 3 ^ + x 128 - 4 ^ y 128 - 4 ^ 0.0001 + + / * round 128 +")
  25.         mt_makediff(a,mindiff)
  26.         ee ? last : interleave(a,b,c).clense().selectevery(3,1)
  27.     }
  28.     function point(clip c,int n,int r,bool ee)
  29.     {
  30.         x=n%(2*r+1)
  31.         y=(n-x)/(2*r+1)
  32.         c
  33.         closer(last,shift(x-r,y-r),shift(r-x,r-y),ee)
  34.     }
  35.     function points(clip c,int n,int r,bool ee)
  36.     {
  37.         n > 0 ? interleaveevery(points(c,n-1,r,ee),point(c,n,r,ee),n+1,0) : point(c,0,r,ee)
  38.     }
  39.     assert(c.isyuv(),"snn: input clip must be YUV")
  40.     c.converttoy8()
  41.     points(last,2*(r*r+r),r,ee)
  42.     temporalsoften(r*r+r,255,255)
  43.     selectevery(2*(r*r+r)+1,r*r+r)
  44.     chroma == "process" ? ytouv(c.utoy().snn(radius,ee,"ignore"),c.vtoy().snn(radius,ee,"ignore"),last) : last
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement