Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.57 KB | None | 0 0
  1. import vapoursynth as vs
  2. import functools
  3.  
  4. def logic_andn_clip(clipa, clipb):
  5.     return vs.get_core().std.Lut2(clipa=clipa, clipb=clipb,
  6.                                    function=lambda x, y: x & (~y))
  7.  
  8. def logic_and_clip(clipa, clipb):
  9.     return vs.get_core().std.Lut2(clipa=clipa, clipb=clipb,
  10.                                    function=lambda x, y: x & y)
  11.  
  12. def make_nmasks(c1, c2, thr=10):
  13.     core = vs.get_core()
  14.     cm = functools.partial(core.comb.CombMask, cthresh=thr, mthresh=thr,
  15.                            planes=[0, 1, 2])
  16.  
  17.     cmask = cm(c2)
  18.     nmask1 = core.generic.Invert(clip=cmask, planes=[0, 1, 2]) # 255(combed) -> 0(combed)
  19.     nmask2 = logic_andn_clip(cmask, cm(c1))
  20.     return nmask1, nmask2
  21.  
  22. def flexible_merge(clip1, clip2, nmask1=None, nmask2=None, weight=0.5,
  23.                      weight2=0.9, thr=10, exc=0, nmax=0.12, cmthr=10, diffthr=0,
  24.                      diffthr2=0.90, show=0, plane=0) :
  25.     core = vs.get_core()
  26.  
  27.     planes = range(clip1.format.num_planes)
  28.     if len(planes) > 3:
  29.         raise ValueError('flexible_merge: invalid the number of planes!')
  30.  
  31.     def select_noiseless_clip(n, f, clip1, clip2):
  32.         if f[0].props.PlaneAverage < f[1].props.PlaneAverage:
  33.             return clip1
  34.         return clip2
  35.  
  36.     def select_clip_by_threshold(n, f, clip1, clip2, core, threshold,
  37.                                     show=0, plane=0):
  38.         diff = abs(f[0].props.PlaneAverage - f[1].props.PlaneAverage)
  39.         clip = clip1 if diff < threshold else clip2
  40.         if show:
  41.             yuv = ["\nY: ", "\n\nU: ", "\n\n\nV: "]
  42.             text = "{yuv} {n} selective {diff} >= {th}".format(
  43.                 yuv=yuv[plane], n=n, diff=diff, th=threshold)
  44.             clip = core.text.Text(clip, text=text)
  45.         return clip
  46.  
  47.     def noise_exclude(clip, clip1, clip2, mode=1, thr=0.12, cmthresh=10,
  48.                         showi=0, plane=0):
  49.         partial = functools.partial
  50.         cm = partial(core.comb.CombMask, cthresh=cmthresh, planes=planes)
  51.         show = showi == 3
  52.  
  53.         if show:
  54.             clip1 = core.text.Text(clip1, text="1")
  55.             clip2 = core.text.Text(clip2, text="2")
  56.  
  57.         noise_avg = [core.std.PlaneAverage(cm(clip1), plane=0),
  58.                      core.std.PlaneAverage(cm(clip2), plane=0)]
  59.  
  60.         scbt = partial(select_clip_by_threshold, clip1=clip, core=core,
  61.                        threshold=thr, show=show, plane=plane)
  62.  
  63.         if mode == 1:
  64.             return core.std.FrameEval(clip, eval=partial(scbt, clip2=clip1),
  65.                                        prop_src=noise_avg)
  66.  
  67.         snc = partial(select_noiseless_clip, clip1=clip1, clip2=clip2)
  68.         clip_select = core.std.FrameEval(clip, eval=snc, prop_src=noise_avg)
  69.  
  70.         return core.std.FrameEval(clip, eval=partial(scbt, clip2=clip_select),
  71.                                    prop_src=noise_avg)
  72.  
  73.     def exclude_overdiff(clip1, clip2) :
  74.         def difference_pixel_with_thr(x, y) :
  75.             return (abs(x - y) < diffthr) * 255
  76.  
  77.         clip = core.std.Lut2(clipa=clip1, clipb=clip2,
  78.                              function=difference_pixel_with_thr)
  79.  
  80.         # greater value means both clips are closed to.
  81.         avgc = core.std.PlaneAverage(clip, plane=0)
  82.  
  83.         zero = core.std.BlankClip(clip=clip, color=[0]*clip.format.num_planes)
  84.         zero = core.std.PlaneAverage(zero, plane=0)
  85.  
  86.         scbt = functools.partial(select_clip_by_threshold, clip1=zero,
  87.                                  clip2=clip, core=core, threshold=diffthr2,
  88.                                  show=0, plane=plane)
  89.  
  90.         return core.std.FrameEval(clip, eval=scbt, prop_src=[avgc, zero])
  91.  
  92.     if !isinstance(nmask1, vs.VideoNode) or !isinstance(nmask2, vs.VideoNode):
  93.         nmask1, nmask2 = make_nmasks(clip1, clip2, thr=thr)
  94.  
  95.     if diffthr > 0 :
  96.         dmask = exclude_overdiff(clip1, clip2)
  97.         nmask1 = logic_and_clip(dmask, nmask1)
  98.         nmask2 = logic_and_clip(dmask, nmask2)
  99.         if exc == 2 :
  100.             exc = 1
  101.  
  102.     clip3 = core.std.Merge(clip1, clip2, weight)
  103.     clip4 = core.std.Merge(clip1, clip2, weight2)
  104.     clip = core.std.MaskedMerge(clip1, clip3, nmask1, planes=planes)
  105.     clip = core.std.MaskedMerge(clip,  clip4, nmask2, planes=planes)
  106.  
  107.     if exc > 0 :# and diffthr == 0 :
  108.         clip = noise_exclude(clip, clip1, clip2, mode = exc, thr = nmax,
  109.                              cmthresh = cmthr, showi = show, plane=plane)
  110.  
  111.     return clip
  112.  
  113. def flexible_merge3(c1, c2, weight=0.5, weight2=0.5, thr=10, exc=0,
  114.                       nmax=0.12, cmthr=10, diffthr_y=0, diffthr_uv=0,
  115.                       diffthr2=0.95, show_y=0, show_u=0, show_v=0):
  116.     core = vs.get_core()
  117.  
  118.     def extract_clip(clip):
  119.         sp = core.std.ShufflePlanes
  120.         return (sp(clips, planes=p, colorfamily=vs.GRAY) for p in range(3))
  121.  
  122.     y1, u1, v1 = extract_clip(c1)
  123.     y2, u2, v2 = extract_clip(c2)
  124.  
  125.     nmask1, nmask2 = make_nmasks(c1, c2, thr=thr)
  126.  
  127.     nmask1_y, nmask1_u, nmask1_v = extract_clip(nmask1)
  128.     nmask2_y, nmask2_u, nmask2_v = extract_clip(nmask2)
  129.  
  130.     fm = functools.partial(flexible_merge, weight=weight, weight2=weight2,
  131.                            thr=thr, exc=exc, diffthr2=diffthr2)
  132.     y3 = fm(y1, y2, nmask1=nmask1_y, nmask2=nmask2_y, nmax=nmax, cmthr=cmthr,
  133.             diffthr=diffthr_y, show = show_y, plane=0 )
  134.     u3 = fm(u1, u2, nmask1=nmask1_u, nmask2=nmask2_u, nmax=nmax*2,
  135.             cmthr=cmthr//2, diffthr=diffthr_uv, show = show_u, plane=1 )
  136.     v3 = fm(v1, v2, nmask1=nmask1_v, nmask2=nmask2_v, nmax=nmax*2,
  137.             cmthr=cmthr//2, diffthr=diffthr_uv, show = show_v, plane=2 )
  138.  
  139.     return core.std.ShufflePlanes(clips=[y3, u3, v3], planes=[0, 0, 0],
  140.                                    colorfamily=vs.YUV)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement