Advertisement
Zastin

aa prefilter for damaged line art

Dec 10th, 2020 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. from math import log2
  2.  
  3. def unsharp(clip, radius=1, strength=log2(3), custom=None):
  4.     if callable(custom):
  5.         return clip.std.MergeDiff(clip.std.MakeDiff(custom(clip)))
  6.    
  7.     strength = max(1e-6, min(strength, log2(3)))
  8.    
  9.     weight = 0.5 ** strength / ((1 - 0.5 ** strength) / 2)
  10.    
  11.     # find matrix with least rounding error when using integer clips
  12.     if clip.format.sample_type == 0:
  13.         all_matrices = [[x] for x in range(1, 1024)]
  14.         for x in range(1023):
  15.             while len(all_matrices[x]) < radius * 2 + 1:
  16.                 all_matrices[x].append(all_matrices[x][-1] / weight)
  17.         error = [sum([abs(x - round(x)) for x in matrix[1:]]) for matrix in all_matrices]
  18.         matrix = [round(x) for x in all_matrices[error.index(min(error))]]
  19.     else:
  20.         matrix = [1]
  21.         while len(matrix) < radius * 2 + 1:
  22.             matrix.append(matrix[-1] / weight)
  23.    
  24.     matrix = [matrix[x] for x in [(2,1,2,1,0,1,2,1,2), (4,3,2,3,4,3,2,1,2,3,2,1,0,1,2,3,2,1,2,3,4,3,2,3,4)][radius-1]]
  25.    
  26.     return clip.std.MergeDiff(clip.std.MakeDiff(clip.std.Convolution(matrix)))
  27.  
  28.  
  29.  
  30. y = depth(get_y(clip), 16)
  31. y0=y
  32.  
  33. clips = [y, y.znedi3.nnedi3(1), y.znedi3.nnedi3(0)]
  34.  
  35. s = unsharp(y, radius=1, strength=log2(3))
  36.  
  37. out = core.std.Expr([s]+clips, 'x y z a max max min y z a min min max')
  38.  
  39. y = out.std.Transpose()
  40.  
  41. clips = [y, y.znedi3.nnedi3(1), y.znedi3.nnedi3(0)]
  42.  
  43. s = unsharp(y, radius=1, strength=log2(3))
  44.  
  45. out = core.std.Expr([s]+clips, 'x y z a max max min y z a min min max').std.Transpose()
  46.  
  47. core.std.Interleave(y0, out).set_output()
  48.  
  49.  
  50.  
  51.  
  52. """ dunno what went wrong here but dont wanna delete it
  53. import math
  54.  
  55. def unsharp(clip, radius=1, strength=math.log2(3), custom=None):
  56.    if callable(custom):
  57.        return clip.std.MergeDiff(clip.std.MakeDiff(custom(clip)))
  58.    
  59.    strength = max(1e-6, min(strength, math.log2(3)))
  60.    
  61.    weight = 0.5 ** strength / ((1 - 0.5 ** strength) / 2)
  62.    
  63.    # find matrix with least rounding error when using integer clips
  64.    if clip.format.sample_type == 0:
  65.        all_matrices = [[x] for x in range(1, 1024)]
  66.        for x in range(1023):
  67.            while len(all_matrices[x]) < radius * 2 + 1:
  68.                all_matrices[x].append(all_matrices[x][-1] / weight)
  69.        error = [sum([abs(x - round(x)) for x in matrix[1:]]) for matrix in all_matrices]
  70.        matrix = [round(x) for x in all_matrices[error.index(min(error))]]
  71.    else:
  72.        matrix = [1]
  73.        while len(matrix) < radius * 2 + 1:
  74.            matrix.append(matrix[-1] / weight)
  75.    
  76.    matrix = [matrix[x] for x in [(2,1,2,1,0,1,2,1,2), (4,3,2,3,4,3,2,1,2,3,2,1,0,1,2,3,2,1,2,3,4,3,2,3,4)][radius-1]]
  77.    
  78.    return clip.std.MergeDiff(clip.std.MakeDiff(clip.std.Convolution(matrix)))
  79.  
  80. def pre_aa(clip, radius=1, strength=math.log2(3), pp=None, **nnedi3_params):
  81.  
  82.    sharp = unsharp(clip, radius=radius, strength=strength)
  83.  
  84.    nedi = clip.znedi3.nnedi3(3, **nnedi3_params).std.SeparateFields(True).std.SelectEvery(4, [0, 3]).std.DoubleWeave()[::2]
  85.  
  86.    if callable(pp):
  87.        nedi = pp(nedi)
  88.  
  89.    clip = core.std.Expr([sharp, clip, nedi], 'x y z max min y z min max').std.Transpose()
  90.  
  91.    sharp = unsharp(clip, radius=radius, strength=strength)
  92.  
  93.    nedi = clip.znedi3.nnedi3(3, **nnedi3_params).std.SeparateFields(True).std.SelectEvery(4, [0, 3]).std.DoubleWeave()[::2]
  94.  
  95.    if callable(pp):
  96.        nedi = pp(nedi)
  97.  
  98.    return core.std.Expr([sharp, clip, nedi], 'x y z max min y z min max').std.Transpose()
  99. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement