TA的每日心情 | 衰 2024-12-7 18:09 |
---|
签到天数: 6 天 [LV.2]偶尔看看I
注册会员
- 积分
- 740
|
本帖最后由 nyaru 于 2025-1-5 16:44 编辑
你可以使用旧版
- def HQDeringmod2(input, p=None, ringmask=None, mrad=1, msmooth=1, incedge=False, mthr=60, minp=1, nrmode=None, sharp=1, drrep=24, thr=12.0, elast=2.0, darkthr=None, planes=[0], show=False):
- if not isinstance(input, vs.VideoNode):
- raise vs.Error('HQDeringmod: this is not a clip')
- if input.format.color_family == vs.RGB:
- raise vs.Error('HQDeringmod: RGB format is not supported')
- if p is not None and (not isinstance(p, vs.VideoNode) or p.format.id != input.format.id):
- raise vs.Error("HQDeringmod: 'p' must have the same format as input")
- if ringmask is not None and not isinstance(ringmask, vs.VideoNode):
- raise vs.Error("HQDeringmod: 'ringmask' is not a clip")
-
- import math
- def cround(x):
- return math.floor(x + 0.5) if x > 0 else math.ceil(x - 0.5)
- def drscale(value, peak):
- return cround(value * peak / 255) if peak != 1 else value / 255
- isGray = (input.format.color_family == vs.GRAY)
- neutral = 1 << (input.format.bits_per_sample - 1)
- peak = (1 << input.format.bits_per_sample) - 1
- if isinstance(planes, int):
- planes = [planes]
- if nrmode is None:
- nrmode = 2 if input.width > 1024 or input.height > 576 else 1
- if darkthr is None:
- darkthr = thr / 4
- # Kernel: Smoothing
- if p is None:
- p = MinBlur(input, r=nrmode, planes=planes)
- # Post-Process: Contra-Sharpening
- matrix1 = [1, 2, 1, 2, 4, 2, 1, 2, 1]
- matrix2 = [1, 1, 1, 1, 1, 1, 1, 1, 1]
- if sharp <= 0:
- sclp = p
- else:
- pre = p.std.Median(planes=planes)
- if sharp == 1:
- method = pre.std.Convolution(matrix=matrix1, planes=planes)
- elif sharp == 2:
- method = pre.std.Convolution(matrix=matrix1, planes=planes).std.Convolution(matrix=matrix2, planes=planes)
- else:
- method = pre.std.Convolution(matrix=matrix1, planes=planes).std.Convolution(matrix=matrix2, planes=planes).std.Convolution(matrix=matrix2, planes=planes)
- sharpdiff = core.std.MakeDiff(pre, method, planes=planes)
- allD = core.std.MakeDiff(input, p, planes=planes)
- ssDD = core.rgvs.Repair(sharpdiff, allD, mode=[1 if i in planes else 0 for i in range(input.format.num_planes)])
- expr = f'x {neutral} - abs y {neutral} - abs <= x y ?'
- ssDD = core.std.Expr([ssDD, sharpdiff], expr=[expr if i in planes else '' for i in range(input.format.num_planes)])
- sclp = core.std.MergeDiff(p, ssDD, planes=planes)
- # Post-Process: Repairing
- if drrep <= 0:
- repclp = sclp
- else:
- repclp = core.rgvs.Repair(input, sclp, mode=[drrep if i in planes else 0 for i in range(input.format.num_planes)])
- # Post-Process: Limiting
- if (thr <= 0 and darkthr <= 0) or (thr >= 128 and darkthr >= 128):
- limitclp = repclp
- else:
- limitclp = mvf.LimitFilter(repclp, input, thr=thr, elast=elast, brighten_thr=darkthr, planes=planes)
- # Post-Process: Ringing Mask Generating
- if ringmask is None:
- expr = f'x {drscale(mthr, peak)} < 0 x ?'
- prewittm = AvsPrewitt(input, planes=[0]).std.Expr(expr=[expr] if isGray else [expr, ''])
- fmask = core.misc.Hysteresis(prewittm.std.Median(planes=[0]), prewittm, planes=[0])
- if mrad > 0:
- omask = mt_expand_multi(fmask, planes=[0], sw=mrad, sh=mrad)
- else:
- omask = fmask
- if msmooth > 0:
- omask = mt_inflate_multi(omask, planes=[0], radius=msmooth)
- if incedge:
- ringmask = omask
- else:
- if minp > 3:
- imask = fmask.std.Minimum(planes=[0]).std.Minimum(planes=[0])
- elif minp > 2:
- imask = fmask.std.Inflate(planes=[0]).std.Minimum(planes=[0]).std.Minimum(planes=[0])
- elif minp > 1:
- imask = fmask.std.Minimum(planes=[0])
- elif minp > 0:
- imask = fmask.std.Inflate(planes=[0]).std.Minimum(planes=[0])
- else:
- imask = fmask
- expr = f'x {peak} y - * {peak} /'
- ringmask = core.std.Expr([omask, imask], expr=[expr] if isGray else [expr, ''])
- # Mask Merging & Output
- if show:
- if isGray:
- return ringmask
- else:
- return ringmask.std.Expr(expr=['', repr(neutral)])
- else:
- return core.std.MaskedMerge(input, limitclp, ringmask, planes=planes, first_plane=True)
- def MinBlur(clp, r=1, planes=None):
- if not isinstance(clp, vs.VideoNode):
- raise vs.Error('MinBlur: this is not a clip')
- if planes is None:
- planes = list(range(clp.format.num_planes))
- elif isinstance(planes, int):
- planes = [planes]
- matrix1 = [1, 2, 1, 2, 4, 2, 1, 2, 1]
- matrix2 = [1, 1, 1, 1, 1, 1, 1, 1, 1]
- if r <= 0:
- RG11 = sbr(clp, planes=planes)
- RG4 = clp.std.Median(planes=planes)
- elif r == 1:
- RG11 = clp.std.Convolution(matrix=matrix1, planes=planes)
- RG4 = clp.std.Median(planes=planes)
- elif r == 2:
- RG11 = clp.std.Convolution(matrix=matrix1, planes=planes).std.Convolution(matrix=matrix2, planes=planes)
- RG4 = clp.ctmf.CTMF(radius=2, planes=planes)
- else:
- RG11 = clp.std.Convolution(matrix=matrix1, planes=planes).std.Convolution(matrix=matrix2, planes=planes).std.Convolution(matrix=matrix2, planes=planes)
- if clp.format.bits_per_sample == 16:
- s16 = clp
- RG4 = clp.fmtc.bitdepth(bits=12, planes=planes, dmode=1).ctmf.CTMF(radius=3, planes=planes).fmtc.bitdepth(bits=16, planes=planes)
- RG4 = mvf.LimitFilter(s16, RG4, thr=0.0625, elast=2, planes=planes)
- else:
- RG4 = clp.ctmf.CTMF(radius=3, planes=planes)
- expr = 'x y - x z - * 0 < x x y - abs x z - abs < y z ? ?'
- return core.std.Expr([clp, RG11, RG4], expr=[expr if i in planes else '' for i in range(clp.format.num_planes)])
- def AvsPrewitt(clip, planes=None):
- if not isinstance(clip, vs.VideoNode):
- raise vs.Error('AvsPrewitt: this is not a clip')
- if planes is None:
- planes = list(range(clip.format.num_planes))
- elif isinstance(planes, int):
- planes = [planes]
- return core.std.Expr([clip.std.Convolution(matrix=[1, 1, 0, 1, 0, -1, 0, -1, -1], planes=planes, saturate=False),
- clip.std.Convolution(matrix=[1, 1, 1, 0, 0, 0, -1, -1, -1], planes=planes, saturate=False),
- clip.std.Convolution(matrix=[1, 0, -1, 1, 0, -1, 1, 0, -1], planes=planes, saturate=False),
- clip.std.Convolution(matrix=[0, -1, -1, 1, 0, -1, 1, 1, 0], planes=planes, saturate=False)],
- expr=['x y max z max a max' if i in planes else '' for i in range(clip.format.num_planes)])
- def mt_inflate_multi(src, planes=None, radius=1):
- if not isinstance(src, vs.VideoNode):
- raise vs.Error('mt_inflate_multi: this is not a clip')
- for i in range(radius):
- src = core.std.Inflate(src, planes=planes)
- return src
复制代码 |
|