lyglay1991
帖子: 33
注册时间: 2013-01-05 18:15

求助:AVS下BalanceBorders在VS里该怎么写

如题,想知道AVS下BalanceBorders在VS里该怎么写,求助
头像
Muonium
帖子: 42
注册时间: 2016-06-18 8:48

Re: 求助:AVS下BalanceBorders在VS里该怎么写

2017/8/17更新:合并 MakeDiff / MergeDiff / Expr 组合,优化下性能
2017/8/18更新:收到报错, 参考 fdar0536的版本 把 CropAbs 都改成了 CropRel

BalanceBorders.py:

代码: 全选

import vapoursynth as vs
import mvsfunc as mvf
import math

# BalanceBorders by PL — [2009.09.25] v0.2
# Port by Muonium 2017/8/14
# Source: https://www.dropbox.com/s/v8fm6om7hm1dz0b/BalanceBorders.avs

def scale(val, bits):
    return val * ((1 << bits) - 1) // 255


def TurnRight(clip):
    core = vs.get_core()

    return core.std.FlipVertical(clip).std.Transpose()


def BalanceTopBorder(c, cTop, thresh, blur):
    core = vs.get_core()

    cWidth = c.width
    cHeight = c.height
    cTop = min(cTop, cHeight - 1)
    blurWidth = max(4, math.floor(cWidth / blur))
    
    c2 = mvf.PointPower(c, 1, 1)

    #last = core.std.CropAbs(c2, cWidth * 2, 2, 0, cTop * 2)
    last = core.std.CropRel(c2, 0, 0, cTop*2, (cHeight - cTop - 1) * 2)
    last = core.resize.Point(last, cWidth * 2, cTop * 2)
    last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2)
    last = core.std.Convolution(last, [1, 1, 1], mode='h')
    last = core.resize.Bilinear(last, cWidth * 2, cTop * 2)
    referenceBlur = last

    original = core.std.CropRel(c2, 0, 0, 0, (cHeight - cTop) * 2)

    last = original
    last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2)
    last = core.std.Convolution(last, [1, 1, 1], mode='h')
    last = core.resize.Bilinear(last, cWidth * 2, cTop * 2)
    originalBlur = last

    """
    balanced = core.std.Expr([original, originalBlur, referenceBlur], ['z y - x +'])
    difference = core.std.MakeDiff(balanced, original)

    tp = scale(128 + thresh, c.format.bits_per_sample)
    tm = scale(128 - thresh, c.format.bits_per_sample)
    difference = core.std.Expr([difference], ['x {tp} min {tm} max'.format(tp=tp, tm=tm)])

    last = core.std.MergeDiff(original, difference)
    """
    tp = scale(thresh, c.format.bits_per_sample)
    tm = -tp
    last = core.std.Expr([original, originalBlur, referenceBlur], ['z y - {tp} min {tm} max x +'.format(tp=tp, tm=tm)])

    return core.std.StackVertical([last, core.std.CropRel(c2, 0, 0, cTop*2, 0)]).resize.Point(cWidth, cHeight)


def BalanceBorders(c, cTop=0, cBottom=0, cLeft=0, cRight=0, thresh=128, blur=999):
    core = vs.get_core()

    last = c

    if cTop > 0:
        last = BalanceTopBorder(last, cTop, thresh, blur)

    last = TurnRight(last)

    if cLeft > 0:
        last = BalanceTopBorder(last, cLeft, thresh, blur)

    last = TurnRight(last)

    if cBottom > 0:
        last = BalanceTopBorder(last, cBottom, thresh, blur)

    last = TurnRight(last)

    if cRight > 0:
        last = BalanceTopBorder(last, cRight, thresh, blur)

    last = TurnRight(last)

    return last
删掉了 avsi 中对色彩空间的限制。自己注意 blur 和 thresh 两个参数大于0就好,懒得写范围检查了。
thresh 会自动根据视频的位深进行调整,范围和 avsi 中的一样。

俄文文档就不翻译了 {:husky}

这论坛的Codebox是怎么用的?
上次由 Muonium 在 2017-08-20 23:43,总共编辑 5 次。
被噪点番支配的噩梦
被Waifu2x吊打的噩梦
不会写代码不会写脚本的我连咸鱼都不如了orz
eedi3好慢啊…………
为什么某SSIM的计算值大于1……
被一个月前自己写的脚本吊打,很爽
CropAbs上也能掉坑……
不要乱搞什么对数亮度作图…
魔幻炼丹主义
lyglay1991
帖子: 33
注册时间: 2013-01-05 18:15

Re: 求助:AVS下BalanceBorders在VS里该怎么写

Muonium 写了:BalanceBorders.py:

代码: 全选

import vapoursynth as vs
import mvsfunc as mvf
import math

# BalanceBorders by PL — [2009.09.25] v0.2
# Port by Muonium 2017/8/14
# Source: https://www.dropbox.com/s/v8fm6om7hm1dz0b/BalanceBorders.avs

def scale(val, bits):
    return val * ((1 << bits) - 1) // 255


def TurnRight(clip):
    core = vs.get_core()

    return core.std.FlipVertical(clip).std.Transpose()


def BalanceTopBorder(c, cTop, thresh, blur):
    core = vs.get_core()

    cWidth = c.width
    cHeight = c.height
    cTop = min(cTop, cHeight - 1)
    blurWidth = max(4, math.floor(cWidth / blur))
    
    c2 = mvf.PointPower(c, 1, 1)

    last = core.std.CropAbs(c2, cWidth * 2, 2, 0, cTop * 2)
    last = core.resize.Point(last, cWidth * 2, cTop * 2)
    last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2)
    last = core.std.Convolution(last, [1, 1, 1], mode='h')
    last = core.resize.Bilinear(last, cWidth * 2, cTop * 2)
    referenceBlur = last

    original = core.std.CropAbs(c2, cWidth * 2, cTop * 2, 0, 0)

    last = original
    last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2)
    last = core.std.Convolution(last, [1, 1, 1], mode='h')
    last = core.resize.Bilinear(last, cWidth * 2, cTop * 2)
    originalBlur = last

    balanced = core.std.Expr([original, originalBlur, referenceBlur], ['z y - x +'])
    difference = core.std.MakeDiff(balanced, original)

    tp = scale(128 + thresh, c.format.bits_per_sample)
    tm = scale(128 - thresh, c.format.bits_per_sample)
    difference = core.std.Expr([difference], ['x {tp} min {tm} max'.format(tp=tp, tm=tm)])

    last = core.std.MergeDiff(original, difference)

    return core.std.StackVertical([last, core.std.CropAbs(c2, cWidth * 2, (cHeight - cTop) * 2, 0, cTop * 2)]).resize.Point(cWidth, cHeight)


def BalanceBorders(c, cTop=0, cBottom=0, cLeft=0, cRight=0, thresh=128, blur=999):
    core = vs.get_core()

    last = c

    if cTop > 0:
        last = BalanceTopBorder(last, cTop, thresh, blur)

    last = TurnRight(last)

    if cLeft > 0:
        last = BalanceTopBorder(last, cLeft, thresh, blur)

    last = TurnRight(last)

    if cBottom > 0:
        last = BalanceTopBorder(last, cBottom, thresh, blur)

    last = TurnRight(last)

    if cRight > 0:
        last = BalanceTopBorder(last, cRight, thresh, blur)

    last = TurnRight(last)

    return last
删掉了 avsi 中对色彩空间的限制。自己注意 blur 和 thresh 两个参数大于0就好,懒得写范围检查了。
thresh 会自动根据视频的位深进行调整,范围和 avsi 中的一样。

俄文文档就不翻译了 {:husky}

这论坛的Codebox是怎么用的?
感谢移植,试了一下,预览效果不错,不知压制怎么样了,期待中

回到 “VapourSynth”