Skip to content

denoise

Functions:

  • smooth_dering

    Applies deringing by using a smart smoother near edges (where ringing

  • vine_dehalo

    Dehalo via non-local errors filtering.

smooth_dering

smooth_dering(
    clip: VideoNode,
    smooth: VideoNode | PrefilterLike = MINBLUR(radius=1),
    ringmask: VideoNode | None = None,
    mrad: int = 1,
    msmooth: int = 1,
    minp: int = 1,
    mthr: float = 0.24,
    incedge: bool = False,
    thr: int = 12,
    elast: float = 2.0,
    darkthr: int | None = None,
    contra: int | float | bool = 1.2,
    drrep: int = 13,
    pre_ss: float = 1.0,
    pre_supersampler: ScalerLike = NNEDI3(noshift=(True, False)),
    pre_downscaler: ScalerLike = Point,
    planes: PlanesT = 0,
    show_mask: bool = False,
) -> VideoNode

Applies deringing by using a smart smoother near edges (where ringing occurs) only. Formerly known as HQDeringmod.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • smooth

    (VideoNode | PrefilterLike, default: MINBLUR(radius=1) ) –

    Already smoothed clip, or a Prefilter, tuple for [luma, chroma] prefilter.

  • ringmask

    (VideoNode | None, default: None ) –

    Custom ringing mask.

  • mrad

    (int, default: 1 ) –

    Expanding iterations of edge mask, higher value means more aggressive processing.

  • msmooth

    (int, default: 1 ) –

    Inflating iterations of edge mask, higher value means smoother edges of mask.

  • minp

    (int, default: 1 ) –

    Inpanding iterations of prewitt edge mask, higher value means more aggressive processing.

  • mthr

    (float, default: 0.24 ) –

    Threshold of prewitt edge mask, lower value means more aggressive processing but for strong ringing, lower value will treat some ringing as edge, which "protects" this ringing from being processed.

  • incedge

    (bool, default: False ) –

    Whether to include edge in ring mask, by default ring mask only include area near edges.

  • thr

    (int, default: 12 ) –

    Threshold (8-bit scale) to limit filtering diff. Smaller thr will result in more pixels being taken from processed clip. Larger thr will result in less pixels being taken from input clip.

    • PDiff: pixel value diff between processed clip and input clip
    • ODiff: pixel value diff between output clip and input clip

    PDiff, thr and elast is used to calculate ODiff: ODiff = PDiff when [PDiff <= thr]

    ODiff gradually smooths from thr to 0 when [thr <= PDiff <= thr * elast].

    For elast>2.0, ODiff reaches maximum when [PDiff == thr * elast / 2]

    ODiff = 0 when [PDiff >= thr * elast]

  • elast

    (float, default: 2.0 ) –

    Elasticity of the soft threshold. Larger "elast" will result in more pixels being blended from.

  • darkthr

    (int | None, default: None ) –

    Threshold (8-bit scale) for darker area near edges, for filtering diff that brightening the image by default equals to thr/4. Set it lower if you think de-ringing destroys too much lines, etc. When darkthr is not equal to thr, thr limits darkening, while darkthr limits brightening. This is useful to limit the overshoot/undershoot/blurring introduced in deringing. Examples:

    • thr=0, darkthr=0 : no limiting
    • thr=255, darkthr=255: no limiting
    • thr=8, darkthr=2 : limit darkening with 8, brightening is limited to 2
    • thr=8, darkthr=0 : limit darkening with 8, brightening is limited to 0
    • thr=255, darkthr=0 : limit darkening with 255, brightening is limited to 0

    For the last two examples, output will remain unchanged. (0/255: no limiting)

  • contra

    (int | float | bool, default: 1.2 ) –

    Whether to use contra-sharpening to resharp deringed clip: False: no contrasharpening True: auto radius for contrasharpening int 1-3: represents radius for contrasharpening float: represents level for contrasharpening_dehalo

  • drrep

    (int, default: 13 ) –

    Use repair for details retention, recommended values are 13/12/1.

  • planes

    (PlanesT, default: 0 ) –

    Planes to be processed.

  • show_mask

    (bool, default: False ) –

    Show the computed ringing mask.

Returns:

  • VideoNode

    Deringed clip.

Source code
 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
 74
 75
 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def smooth_dering(
    clip: vs.VideoNode,
    smooth: vs.VideoNode | PrefilterLike = Prefilter.MINBLUR(radius=1),
    ringmask: vs.VideoNode | None = None,
    mrad: int = 1,
    msmooth: int = 1,
    minp: int = 1,
    mthr: float = 0.24,
    incedge: bool = False,
    thr: int = 12,
    elast: float = 2.0,
    darkthr: int | None = None,
    contra: int | float | bool = 1.2,
    drrep: int = 13,
    pre_ss: float = 1.0,
    pre_supersampler: ScalerLike = NNEDI3(noshift=(True, False)),
    pre_downscaler: ScalerLike = Point,
    planes: PlanesT = 0,
    show_mask: bool = False,
) -> vs.VideoNode:
    """
    Applies deringing by using a smart smoother near edges (where ringing
    occurs) only. Formerly known as HQDeringmod.

    Args:
        clip: Clip to process.

        smooth: Already smoothed clip, or a Prefilter, tuple for [luma, chroma] prefilter.

        ringmask: Custom ringing mask.

        mrad: Expanding iterations of edge mask, higher value means more aggressive processing.

        msmooth: Inflating iterations of edge mask, higher value means smoother edges of mask.

        minp: Inpanding iterations of prewitt edge mask, higher value means more aggressive processing.

        mthr: Threshold of prewitt edge mask, lower value means more aggressive processing but for strong ringing, lower
            value will treat some ringing as edge, which "protects" this ringing from being processed.

        incedge: Whether to include edge in ring mask, by default ring mask only include area near edges.

        thr: Threshold (8-bit scale) to limit filtering diff. Smaller thr will result in more pixels being taken from
            processed clip. Larger thr will result in less pixels being taken from input clip.

               - PDiff: pixel value diff between processed clip and input clip
               - ODiff: pixel value diff between output clip and input clip

            PDiff, thr and elast is used to calculate ODiff:
            ODiff = PDiff when [PDiff <= thr]

            ODiff gradually smooths from thr to 0 when [thr <= PDiff <= thr * elast].

            For elast>2.0, ODiff reaches maximum when [PDiff == thr * elast / 2]

            ODiff = 0 when [PDiff >= thr * elast]

        elast: Elasticity of the soft threshold. Larger "elast" will result in more pixels being blended from.

        darkthr: Threshold (8-bit scale) for darker area near edges, for filtering diff that brightening the image by
            default equals to thr/4. Set it lower if you think de-ringing destroys too much lines, etc. When darkthr is
            not equal to ``thr``, ``thr`` limits darkening, while ``darkthr`` limits brightening. This is useful to
            limit the overshoot/undershoot/blurring introduced in deringing. Examples:

               - ``thr=0``,   ``darkthr=0``  : no limiting
               - ``thr=255``, ``darkthr=255``: no limiting
               - ``thr=8``,   ``darkthr=2``  : limit darkening with 8, brightening is limited to 2
               - ``thr=8``,   ``darkthr=0``  : limit darkening with 8, brightening is limited to 0
               - ``thr=255``, ``darkthr=0``  : limit darkening with 255, brightening is limited to 0

            For the last two examples, output will remain unchanged. (0/255: no limiting)

        contra: Whether to use contra-sharpening to resharp deringed clip: False: no contrasharpening True: auto radius
            for contrasharpening int 1-3: represents radius for contrasharpening float: represents level for
            contrasharpening_dehalo

        drrep: Use repair for details retention, recommended values are 13/12/1.

        planes: Planes to be processed.

        show_mask: Show the computed ringing mask.

    Returns:
        Deringed clip.
    """
    func = FunctionUtil(clip, smooth_dering, planes, (vs.GRAY, vs.YUV))

    assert check_progressive(clip, func.func)

    planes = func.norm_planes
    work_clip = func.work_clip

    pre_supersampler = Scaler.ensure_obj(pre_supersampler, smooth_dering)
    pre_downscaler = Scaler.ensure_obj(pre_downscaler, smooth_dering)

    if pre_ss > 1.0:
        work_clip = cast(
            ConstantFormatVideoNode,
            pre_supersampler.scale(work_clip, mod4(work_clip.width * pre_ss), mod4(work_clip.height * pre_ss)),
        )

    darkthr = fallback(darkthr, thr // 4)

    rep_dr = [drrep if i in planes else 0 for i in range(work_clip.format.num_planes)]

    if not isinstance(smooth, vs.VideoNode):
        smoothed = smooth(work_clip, planes)
    else:
        check_ref_clip(clip, smooth)

        smoothed = plane(smooth, 0) if func.luma_only else smooth

        if pre_ss > 1.0:
            smoothed = pre_supersampler.scale(smoothed, work_clip.width, work_clip.height)

    if contra:
        if isinstance(contra, int):
            smoothed = contrasharpening(smoothed, work_clip, contra, mode=repair.Mode(13), planes=planes)
        else:
            smoothed = contrasharpening_dehalo(smoothed, work_clip, contra, planes=planes)

    repclp = repair(work_clip, smoothed, drrep) if set(rep_dr) != {0} else work_clip

    limitclp = limit_filter(repclp, work_clip, None, LimitFilterMode.CLAMPING, planes, thr, elast, darkthr)

    if ringmask is None:
        prewittm = Prewitt.edgemask(work_clip, mthr)

        fmask = median_blur(prewittm, planes=planes).hysteresis.Hysteresis(prewittm, planes)

        omask = Morpho.expand(fmask, mrad, mrad, planes=planes) if mrad > 0 else fmask

        if msmooth > 0:
            omask = Morpho.inflate(omask, iterations=msmooth, planes=planes)

        if incedge:
            ringmask = omask
        else:
            if minp <= 0:
                imask = fmask
            elif minp % 2 == 0:
                imask = Morpho.inpand(fmask, minp // 2, planes=planes)
            else:
                imask = Morpho.inpand(Morpho.inflate(fmask, planes=planes), ceil(minp / 2), planes=planes)

            ringmask = norm_expr(
                [omask, imask], [f"{ExprToken.RangeMax} {ExprToken.RangeMax} y - / x *", ExprOp.clamp()], func=func.func
            )

    dering = work_clip.std.MaskedMerge(limitclp, ringmask, planes)

    if show_mask:
        return ringmask

    if (dering.width, dering.height) != (clip.width, clip.height):
        dering = pre_downscaler.scale(work_clip, clip.width, clip.height)

    return func.return_clip(dering)

vine_dehalo

vine_dehalo(
    clip: VideoNode,
    strength: float | Sequence[float] = 16.0,
    sharp: float = 0.5,
    sigma: float | list[float] = 1.0,
    supersampler: ScalerLike = NNEDI3,
    downscaler: ScalerLike = Catrom,
    planes: PlanesT = 0,
    **kwargs: Any
) -> VideoNode

Dehalo via non-local errors filtering.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • strength

    (float | Sequence[float], default: 16.0 ) –

    Strength of nl_means filtering.

  • sharp

    (float, default: 0.5 ) –

    Weight to blend supersampled clip.

  • sigma

    (float | list[float], default: 1.0 ) –

    Gaussian sigma for filtering cutoff.

  • supersampler

    (ScalerLike, default: NNEDI3 ) –

    Scaler used for supersampling before dehaloing.

  • downscaler

    (ScalerLike, default: Catrom ) –

    Scaler used for downscaling after supersampling.

  • planes

    (PlanesT, default: 0 ) –

    Planes to be processed.

  • **kwargs

    (Any, default: {} ) –

    Additional kwargs to be passed to nl_means.

Returns:

  • VideoNode

    Dehaloed clip.

Source code
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def vine_dehalo(
    clip: vs.VideoNode,
    strength: float | Sequence[float] = 16.0,
    sharp: float = 0.5,
    sigma: float | list[float] = 1.0,
    supersampler: ScalerLike = NNEDI3,
    downscaler: ScalerLike = Catrom,
    planes: PlanesT = 0,
    **kwargs: Any,
) -> vs.VideoNode:
    """
    Dehalo via non-local errors filtering.

    Args:
        clip: Clip to process.
        strength: Strength of nl_means filtering.
        sharp: Weight to blend supersampled clip.
        sigma: Gaussian sigma for filtering cutoff.
        supersampler: Scaler used for supersampling before dehaloing.
        downscaler: Scaler used for downscaling after supersampling.
        planes: Planes to be processed.
        **kwargs: Additional kwargs to be passed to nl_means.

    Returns:
        Dehaloed clip.
    """
    func = FunctionUtil(clip, vine_dehalo, planes)

    assert check_progressive(clip, func.func)

    strength = to_arr(strength)
    supersampler = Scaler.ensure_obj(supersampler, func.func)
    downscaler = Scaler.ensure_obj(downscaler, func.func)

    sharp = min(max(sharp, 0.0), 1.0)
    simr = kwargs.pop("simr", None)

    # Only God knows how these were derived.
    constants0 = 0.3926327792690057290863679493724 * sharp
    constants1 = 18.880334973195822973214959957208
    constants2 = 0.5862453661304626725671053478676

    weight = constants0 * log(1 + 1 / constants0)
    h_refine = [constants1 * (s / constants1) ** constants2 for s in strength]

    supersampled = supersampler.supersample(func.work_clip)
    supersampled = nl_means(supersampled, strength, tr=0, simr=0, **kwargs)
    supersampled = downscaler.scale(supersampled, func.work_clip.width, func.work_clip.height)  # type: ignore[assignment]

    smoothed = nl_means(func.work_clip, strength, tr=0, simr=0, **kwargs)
    smoothed = core.std.Merge(supersampled, smoothed, weight)

    highpassed = frequency_merge(
        func.work_clip, smoothed, mode_low=func.work_clip, mode_high=smoothed, lowpass=partial(gauss_blur, sigma=sigma)
    )

    refined = func.work_clip.std.MakeDiff(highpassed)
    refined = nl_means(refined, h_refine, tr=0, simr=simr, ref=highpassed, **kwargs)
    refined = highpassed.std.MergeDiff(refined)

    return func.return_clip(refined)