Skip to content

warp

This module implements dehaloing functions using warping-based techniques as the core processing method.

Functions:

  • YAHR

    Applies YAHR (Yet Another Halo Remover) to reduce halos in a video clip.

  • edge_cleaner

    Cleans edges in a video clip by applying edge-aware processing.

YAHR

YAHR(
    clip: VideoNode,
    blur: int = 3,
    depth: int | Sequence[int] = 32,
    expand: int | Literal[False] = 5,
    shift: int = 8,
    planes: PlanesT = 0,
) -> VideoNode

Applies YAHR (Yet Another Halo Remover) to reduce halos in a video clip.

Parameters:

  • clip

    (VideoNode) –

    The input video clip to process.

  • blur

    (int, default: 3 ) –

    The blur strength for the warping process. Default is 3.

  • depth

    (int | Sequence[int], default: 32 ) –

    The depth of the warping process. Default is 32.

  • expand

    (int | Literal[False], default: 5 ) –

    The expansion factor for edge detection. Set to False to disable masking. Default is 5.

  • shift

    (int, default: 8 ) –

    Corrective shift for fine-tuning mask iterations.

  • planes

    (PlanesT, default: 0 ) –

    The planes to process. Default is 0 (luma only).

Returns:

  • VideoNode

    The processed video clip with reduced halos.

Source code in vsdehalo/warp.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def YAHR(  # noqa: N802
    clip: vs.VideoNode,
    blur: int = 3,
    depth: int | Sequence[int] = 32,
    expand: int | Literal[False] = 5,
    shift: int = 8,
    planes: PlanesT = 0,
) -> vs.VideoNode:
    """
    Applies YAHR (Yet Another Halo Remover) to reduce halos in a video clip.

    Args:
        clip: The input video clip to process.
        blur: The blur strength for the warping process. Default is 3.
        depth: The depth of the warping process. Default is 32.
        expand: The expansion factor for edge detection. Set to False to disable masking. Default is 5.
        shift: Corrective shift for fine-tuning mask iterations.
        planes: The planes to process. Default is 0 (luma only).

    Returns:
        The processed video clip with reduced halos.
    """
    warped = awarpsharp(clip, blur=blur, depth=depth, planes=planes)

    blur_diff, blur_warped_diff = [
        c.std.MakeDiff(BlurMatrix.BINOMIAL()(min_blur(c, 2, planes=planes), planes=planes), planes)
        for c in (clip, warped)
    ]

    rep_diff = repair(blur_diff, blur_warped_diff, 13, planes)
    yahr = norm_expr([clip, blur_diff, rep_diff], "x y z - -", planes)

    if expand is not False:
        v_edge = norm_expr(
            [clip, Morpho.maximum(clip, iterations=2, planes=planes)],
            "y x - {shift} range_max * 255 / - 128 *",
            shift=shift,
            planes=planes,
            func=YAHR,
        )

        mask = norm_expr(
            [
                BlurMatrix.BINOMIAL(radius=expand * 2)(v_edge, planes=planes),
                BlurMatrix.BINOMIAL()(v_edge, planes=planes),
            ],
            "x 16 * range_max y - min",
            planes,
            func=YAHR,
        )

        yahr = clip.std.MaskedMerge(yahr, limiter(mask, planes=planes), planes)

    return yahr

edge_cleaner

edge_cleaner(
    clip: VideoNode,
    strength: int = 5,
    rmode: int | Mode = 17,
    hot: bool = False,
    smode: bool = False,
    edgemask: EdgeDetectT = PrewittStd,
    planes: PlanesT = 0,
) -> VideoNode

Cleans edges in a video clip by applying edge-aware processing.

Parameters:

  • clip

    (VideoNode) –

    The input video clip to process.

  • strength

    (int, default: 5 ) –

    The strength of the edge cleaning. Default is 5.

  • rmode

    (int | Mode, default: 17 ) –

    Repair mode to use for edge refinement. Default is 17.

  • hot

    (bool, default: False ) –

    If True, applies additional repair to hot edges. Default is False.

  • smode

    (bool, default: False ) –

    If True, applies a stronger cleaning mode. Default is False.

  • edgemask

    (EdgeDetectT, default: PrewittStd ) –

    The edge detection method to use. Default is PrewittStd.

  • planes

    (PlanesT, default: 0 ) –

    The planes to process. Default is 0 (luma only).

Returns:

  • VideoNode

    The processed video clip with cleaned edges.

Source code in vsdehalo/warp.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def edge_cleaner(
    clip: vs.VideoNode,
    strength: int = 5,
    rmode: int | Repair.Mode = 17,
    hot: bool = False,
    smode: bool = False,
    edgemask: EdgeDetectT = PrewittStd,
    planes: PlanesT = 0,
) -> vs.VideoNode:
    """
    Cleans edges in a video clip by applying edge-aware processing.

    Args:
        clip: The input video clip to process.
        strength: The strength of the edge cleaning. Default is 5.
        rmode: Repair mode to use for edge refinement. Default is 17.
        hot: If True, applies additional repair to hot edges. Default is False.
        smode: If True, applies a stronger cleaning mode. Default is False.
        edgemask: The edge detection method to use. Default is PrewittStd.
        planes: The planes to process. Default is 0 (luma only).

    Returns:
        The processed video clip with cleaned edges.
    """
    edgemask = EdgeDetect.ensure_obj(edgemask, edge_cleaner)
    lthr = scale_mask(4, 8, 32)
    expr = "x {thr} > range_max x ?"

    if smode:
        strength += 4

    warped = awarpsharp(clip, blur=2, depth=strength, planes=planes)
    warped = repair(warped, clip, rmode, planes)

    mask = edgemask.edgemask(clip, lthr, planes=planes)
    mask = norm_expr(mask, expr, planes, thr=scale_mask(32, 8, clip), func=edge_cleaner)
    mask = box_blur(mask, planes=planes)

    final = warped.std.MaskedMerge(clip, mask, planes)

    if hot:
        final = repair(final, clip, 2, planes)

    if smode:
        clean = remove_grain(clip, 17)
        diff = clip.std.MakeDiff(clean)

        mask = edgemask.edgemask(
            diff.std.Levels(scale_value(40, 8, clip), scale_value(168, 8, clip), 0.35), lthr, planes=planes
        )
        mask = norm_expr(remove_grain(mask, 7, planes), expr, planes, thr=scale_mask(16, 8, clip), func=edge_cleaner)

        final = final.std.MaskedMerge(clip, mask, planes)

    return final