Skip to content

blur

Functions:

  • bilateral
  • box_blur
  • flux_smooth

    FluxSmoothT examines each pixel and compares it to the corresponding pixel in the previous and next frames.

  • gauss_blur
  • median_blur
  • min_blur

    MinBlur by Didée (http://avisynth.nl/index.php/MinBlur)

  • sbr

    A helper function for high-pass filtering a blur difference, inspired by an AviSynth script by Didée.

  • side_box_blur

bilateral

bilateral(
    clip: VideoNode,
    ref: VideoNode | None = None,
    sigmaS: float | Sequence[float] | None = None,
    sigmaR: float | Sequence[float] | None = None,
    backend: BilateralBackend = CPU,
    **kwargs: Any
) -> ConstantFormatVideoNode
Source code
337
338
339
340
341
342
343
344
345
346
347
348
def bilateral(
    clip: vs.VideoNode, ref: vs.VideoNode | None = None, sigmaS: float | Sequence[float] | None = None,
    sigmaR: float | Sequence[float] | None = None, backend: BilateralBackend = BilateralBackend.CPU, **kwargs: Any
) -> ConstantFormatVideoNode:
    assert check_variable_format(clip, bilateral)

    if backend == BilateralBackend.CPU:
        bilateral_args = KwargsT(ref=ref, sigmaS=sigmaS, sigmaR=sigmaR, planes=normalize_planes(clip))
    else:
        bilateral_args = KwargsT(ref=ref, sigma_spatial=sigmaS, sigma_color=sigmaR)

    return getattr(clip, backend).Bilateral(**bilateral_args | kwargs)

box_blur

box_blur(
    clip: VideoNode,
    radius: int | list[int] = 1,
    passes: int = 1,
    mode: OneDimConvModeT | TempConvModeT = HV,
    planes: PlanesT = None,
    **kwargs: Any
) -> ConstantFormatVideoNode
Source code
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
def box_blur(
    clip: vs.VideoNode, radius: int | list[int] = 1, passes: int = 1,
    mode: OneDimConvModeT | TempConvModeT = ConvMode.HV, planes: PlanesT = None, **kwargs: Any
) -> ConstantFormatVideoNode:
    assert check_variable(clip, box_blur)

    if isinstance(radius, list):
        return normalize_radius(clip, box_blur, radius, planes, passes=passes, mode=mode, **kwargs)

    if not radius:
        return clip

    if mode == ConvMode.TEMPORAL:
        return BlurMatrix.MEAN(radius, mode=mode)(clip, planes, passes=passes, **kwargs)

    if not TYPE_CHECKING:
        if mode == ConvMode.SQUARE:
            raise CustomValueError("Invalid mode specified", box_blur, mode)

    box_args = (
        planes,
        radius, 0 if mode == ConvMode.VERTICAL else passes,
        radius, 0 if mode == ConvMode.HORIZONTAL else passes
    )

    return clip.vszip.BoxBlur(*box_args)

flux_smooth

flux_smooth(
    clip: VideoNode,
    temporal_threshold: float | Sequence[float] = 7.0,
    spatial_threshold: float | Sequence[float] | None = None,
    scalep: bool = True,
) -> ConstantFormatVideoNode

FluxSmoothT examines each pixel and compares it to the corresponding pixel in the previous and next frames. Smoothing occurs if both the previous frame's value and the next frame's value are greater, or if both are less than the value in the current frame.

Smoothing is done by averaging the pixel from the current frame with the pixels from the previous and/or next frames, if they are within temporal_threshold.

FluxSmoothST does the same as FluxSmoothT, except the pixel's eight neighbours from the current frame are also included in the average, if they are within spatial_threshold.

The first and last rows and the first and last columns are not processed by FluxSmoothST.

Parameters:

  • clip

    (VideoNode) –

    Clip to process.

  • temporal_threshold

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

    Temporal neighbour pixels within this threshold from the current pixel are included in the average. Can be specified as an array, with values corresonding to each plane of the input clip. A negative value (such as -1) indicates that the plane should not be processed and will be copied from the input clip.

  • spatial_threshold

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

    Spatial neighbour pixels within this threshold from the current pixel are included in the average. A negative value (such as -1) indicates that the plane should not be processed and will be copied from the input clip.

  • scalep

    (bool, default: True ) –

    Parameter scaling. If set to true, all threshold values will be automatically scaled from 8-bit range (0-255) to the corresponding range of the input clip's bit depth.

Returns:

  • ConstantFormatVideoNode

    Smoothed clip.

Source code
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def flux_smooth(
    clip: vs.VideoNode,
    temporal_threshold: float | Sequence[float] = 7.0,
    spatial_threshold: float | Sequence[float] | None = None,
    scalep: bool = True,
) -> ConstantFormatVideoNode:
    """
    FluxSmoothT examines each pixel and compares it to the corresponding pixel in the previous and next frames.
    Smoothing occurs if both the previous frame's value and the next frame's value are greater,
    or if both are less than the value in the current frame.

    Smoothing is done by averaging the pixel from the current frame with the pixels from the previous
    and/or next frames, if they are within temporal_threshold.

    FluxSmoothST does the same as FluxSmoothT, except the pixel's eight neighbours from the current frame
    are also included in the average, if they are within spatial_threshold.

    The first and last rows and the first and last columns are not processed by FluxSmoothST.

    :param clip:                    Clip to process.
    :param temporal_threshold:      Temporal neighbour pixels within this threshold from the current pixel
                                    are included in the average. Can be specified as an array,
                                    with values corresonding to each plane of the input clip.
                                    A negative value (such as -1) indicates that the plane should not be processed
                                    and will be copied from the input clip.
    :param spatial_threshold:       Spatial neighbour pixels within this threshold from the current pixel
                                    are included in the average. A negative value (such as -1) indicates that the plane
                                    should not be processed and will be copied from the input clip.
    :param scalep:                  Parameter scaling. If set to true, all threshold values
                                    will be automatically scaled from 8-bit range (0-255) to the corresponding range
                                    of the input clip's bit depth.
    :return:                        Smoothed clip.
    """

    assert check_variable_format(clip, flux_smooth)

    if spatial_threshold:
        return core.zsmooth.FluxSmoothST(clip, temporal_threshold, spatial_threshold, scalep)

    return core.zsmooth.FluxSmoothT(clip, temporal_threshold, scalep)

gauss_blur

gauss_blur(
    clip: VideoNode,
    sigma: float | list[float] = 0.5,
    taps: int | None = None,
    mode: ConvMode = HV,
    planes: PlanesT = None,
    **kwargs: Any
) -> ConstantFormatVideoNode
Source code
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
def gauss_blur(
    clip: vs.VideoNode, sigma: float | list[float] = 0.5, taps: int | None = None,
    mode: ConvMode = ConvMode.HV, planes: PlanesT = None,
    **kwargs: Any
) -> ConstantFormatVideoNode:
    assert check_variable(clip, gauss_blur)

    planes = normalize_planes(clip, planes)

    if isinstance(sigma, list):
        return normalize_radius(clip, gauss_blur, ('sigma', sigma), planes, mode=mode)

    fast = kwargs.pop("_fast", False)

    sigma_constant = 0.9 if fast and not mode.is_temporal else sigma
    taps = BlurMatrix.GAUSS.get_taps(sigma_constant, taps)

    if not mode.is_temporal:
        def _resize2_blur(plane: ConstantFormatVideoNode, sigma: float, taps: int) -> ConstantFormatVideoNode:
            resize_kwargs = dict[str, Any]()

            # Downscale approximation can be used by specifying _fast=True
            # Has a big speed gain when taps is large
            if fast:
                wdown, hdown = plane.width, plane.height

                if ConvMode.VERTICAL in mode:
                    hdown = round(max(round(hdown / sigma), 2) / 2) * 2

                if ConvMode.HORIZONTAL in mode:
                    wdown = round(max(round(wdown / sigma), 2) / 2) * 2

                resize_kwargs.update(width=plane.width, height=plane.height)

                plane = Bilinear.scale(plane, wdown, hdown)  # type: ignore[assignment]
                sigma = sigma_constant
            else:
                resize_kwargs.update({f'force_{k}': k in mode for k in 'hv'})

            return Gaussian(sigma, taps).scale(plane, **resize_kwargs | kwargs)  # type: ignore[return-value]

        if not {*range(clip.format.num_planes)} - {*planes}:
            return _resize2_blur(clip, sigma, taps)

        return join([
            _resize2_blur(p, sigma, taps) if i in planes else p
            for i, p in enumerate(split(clip))
        ])

    kernel = BlurMatrix.GAUSS(taps, sigma=sigma, mode=mode, scale_value=1023)

    return kernel(clip, planes, **kwargs)

median_blur

median_blur(
    clip: VideoNode,
    radius: int = ...,
    mode: Literal[TEMPORAL] = ...,
    planes: PlanesT = ...,
) -> ConstantFormatVideoNode
median_blur(
    clip: VideoNode,
    radius: int | list[int] = ...,
    mode: SpatialConvModeT = ...,
    planes: PlanesT = None,
) -> ConstantFormatVideoNode
median_blur(
    clip: VideoNode,
    radius: int | list[int] = ...,
    mode: ConvMode = ...,
    planes: PlanesT = None,
) -> ConstantFormatVideoNode
median_blur(
    clip: VideoNode,
    radius: int | list[int] = 1,
    mode: ConvMode = SQUARE,
    planes: PlanesT = None,
) -> ConstantFormatVideoNode
Source code
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
def median_blur(
    clip: vs.VideoNode, radius: int | list[int] = 1, mode: ConvMode = ConvMode.SQUARE, planes: PlanesT = None
) -> ConstantFormatVideoNode:
    assert check_variable(clip, median_blur)

    if mode == ConvMode.TEMPORAL:
        if isinstance(radius, int):
            return clip.zsmooth.TemporalMedian(radius, planes)

        raise CustomValueError("A list of radius isn't supported for ConvMode.TEMPORAL!", median_blur, radius)

    radius = to_arr(radius)

    if (len((rs := set(radius))) == 1 and rs.pop() == 1):
        if mode == ConvMode.SQUARE:
            return remove_grain(clip, norm_rmode_planes(clip, RemoveGrainMode.MINMAX_MEDIAN, planes))
        if mode == ConvMode.VERTICAL:
            return vertical_cleaner(clip, norm_rmode_planes(clip, VerticalCleanerMode.MEDIAN, planes))

    expr_plane = list[list[str]]()

    for r in radius:
        expr_passes = list[str]()

        for mat in ExprOp.matrix('x', r, mode, [(0, 0)]):
            rb = len(mat) + 1
            st = rb - 1
            sp = rb // 2 - 1
            dp = st - 2

            expr_passes.append(f"{mat} sort{st} swap{sp} min! swap{sp} max! drop{dp} x min@ max@ clip")

        expr_plane.append(expr_passes)

    for e in zip(*expr_plane):
        clip = norm_expr(clip, e, planes, func=median_blur)

    return clip

min_blur

min_blur(
    clip: VideoNode,
    radius: int | list[int] = 1,
    mode: tuple[ConvMode, ConvMode] = (HV, SQUARE),
    planes: PlanesT = None,
    **kwargs: Any
) -> ConstantFormatVideoNode

MinBlur by Didée (http://avisynth.nl/index.php/MinBlur) Nifty Gauss/Median combination

Source code
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def min_blur(
    clip: vs.VideoNode, radius: int | list[int] = 1,
    mode: tuple[ConvMode, ConvMode] = (ConvMode.HV, ConvMode.SQUARE), planes: PlanesT = None,
    **kwargs: Any
) -> ConstantFormatVideoNode:
    """
    MinBlur by Didée (http://avisynth.nl/index.php/MinBlur)
    Nifty Gauss/Median combination
    """
    assert check_variable(clip, min_blur)

    planes = normalize_planes(clip, planes)

    if isinstance(radius, list):
        return normalize_radius(clip, min_blur, radius, planes)

    mode_blur, mode_median = normalize_seq(mode, 2)

    blurred = BlurMatrix.BINOMIAL(radius=radius, mode=mode_blur)(clip, planes=planes, **kwargs)
    median = median_blur(clip, radius, mode_median, planes=planes)

    return MeanMode.MEDIAN([clip, blurred, median], planes=planes)

sbr

sbr(
    clip: VideoNode,
    radius: int | Sequence[int] = 1,
    mode: ConvMode = HV,
    blur: _SbrBlurT | VideoNode = BINOMIAL,
    blur_diff: _SbrBlurT = BINOMIAL,
    planes: PlanesT = None,
    *,
    func: FuncExceptT | None = None,
    **kwargs: Any
) -> ConstantFormatVideoNode

A helper function for high-pass filtering a blur difference, inspired by an AviSynth script by Didée. https://forum.doom9.org/showthread.php?p=1584186#post1584186

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • radius

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

    Specifies the size of the blur kernels if blur or blur_diff is a BlurMatrix enum. Default to 1.

  • mode

    (ConvMode, default: HV ) –

    Specifies the convolution mode. Defaults to horizontal + vertical.

  • blur

    (_SbrBlurT | VideoNode, default: BINOMIAL ) –

    Blur kernel to apply to the original clip. Defaults to binomial.

  • blur_diff

    (_SbrBlurT, default: BINOMIAL ) –

    Blur kernel to apply to the difference clip. Defaults to binomial.

  • planes

    (PlanesT, default: None ) –

    Which planes to process. Defaults to all.

  • **kwargs

    (Any, default: {} ) –

    Additional arguments passed to blur kernel call.

Returns:

  • ConstantFormatVideoNode

    Sbr'd clip.

Source code
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def sbr(
    clip: vs.VideoNode,
    radius: int | Sequence[int] = 1,
    mode: ConvMode = ConvMode.HV,
    blur: _SbrBlurT | vs.VideoNode = BlurMatrix.BINOMIAL,
    blur_diff: _SbrBlurT = BlurMatrix.BINOMIAL,
    planes: PlanesT = None,
    *,
    func: FuncExceptT | None = None,
    **kwargs: Any
) -> ConstantFormatVideoNode:
    """
    A helper function for high-pass filtering a blur difference, inspired by an AviSynth script by Didée.
    `https://forum.doom9.org/showthread.php?p=1584186#post1584186`

    :param clip:        Source clip.
    :param radius:      Specifies the size of the blur kernels if `blur` or `blur_diff` is a BlurMatrix enum.
                        Default to 1.
    :param mode:        Specifies the convolution mode. Defaults to horizontal + vertical.
    :param blur:        Blur kernel to apply to the original clip. Defaults to binomial.
    :param blur_diff:   Blur kernel to apply to the difference clip. Defaults to binomial.
    :param planes:      Which planes to process. Defaults to all.
    :param **kwargs:    Additional arguments passed to blur kernel call.
    :return:            Sbr'd clip.
    """
    func = func or sbr

    if isinstance(radius, Sequence):
        return normalize_radius(clip, min_blur, list(radius), planes)

    def _apply_blur(clip: ConstantFormatVideoNode, blur: _SbrBlurT | vs.VideoNode) -> ConstantFormatVideoNode:
        if isinstance(blur, Sequence):
            return BlurMatrixBase(blur, mode=mode)(clip, planes, **kwargs)

        if isinstance(blur, BlurMatrix):
            return blur(taps=radius, mode=mode)(clip, planes, **kwargs)

        blurred = blur(clip) if callable(blur) else blur

        assert check_variable_format(blurred, func)

        return blurred

    assert check_variable(clip, func)

    planes = normalize_planes(clip, planes)

    blurred = _apply_blur(clip, blur)

    diff = clip.std.MakeDiff(blurred, planes=planes)
    blurred_diff = _apply_blur(diff, blur_diff)

    return norm_expr(
        [clip, diff, blurred_diff],
        'y neutral - D1! y z - D2! D1@ D2@ xor x x D1@ abs D2@ abs < D1@ D2@ ? - ?',
        planes=planes, func=func
    )

side_box_blur

side_box_blur(
    clip: VideoNode,
    radius: int | list[int] = 1,
    planes: PlanesT = None,
    inverse: bool = False,
) -> ConstantFormatVideoNode
Source code
 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
def side_box_blur(
    clip: vs.VideoNode, radius: int | list[int] = 1, planes: PlanesT = None,
    inverse: bool = False
) -> ConstantFormatVideoNode:
    assert check_variable_format(clip, side_box_blur)

    planes = normalize_planes(clip, planes)

    if isinstance(radius, list):
        return normalize_radius(clip, side_box_blur, radius, planes, inverse=inverse)

    half_kernel = [(1 if i <= 0 else 0) for i in range(-radius, radius + 1)]

    conv_m1 = partial(core.std.Convolution, matrix=half_kernel, planes=planes)
    conv_m2 = partial(core.std.Convolution, matrix=half_kernel[::-1], planes=planes)
    blur_pt = partial(box_blur, planes=planes)

    vrt_filters, hrz_filters = list[list[partial[ConstantFormatVideoNode]]](
        [
            partial(conv_m1, mode=mode), partial(conv_m2, mode=mode),
            partial(blur_pt, hradius=hr, vradius=vr, hpasses=h, vpasses=v)
        ] for h, hr, v, vr, mode in [
            (0, None, 1, radius, ConvMode.VERTICAL), (1, radius, 0, None, ConvMode.HORIZONTAL)
        ]
    )

    vrt_intermediates = (vrt_flt(clip) for vrt_flt in vrt_filters)
    intermediates = list(
        hrz_flt(vrt_intermediate)
        for i, vrt_intermediate in enumerate(vrt_intermediates)
        for j, hrz_flt in enumerate(hrz_filters) if not i == j == 2
    )

    comp_blur = None if inverse else box_blur(clip, radius, 1, planes=planes)

    if complexpr_available:
        template = '{cum} x - abs {new} x - abs < {cum} {new} ?'

        cum_expr, cumc = '', 'y'
        n_inter = len(intermediates)

        for i, newc, var in zip(count(), ExprVars[2:26], ExprVars[4:26]):
            if i == n_inter - 1:
                break

            cum_expr += template.format(cum=cumc, new=newc)

            if i != n_inter - 2:
                cumc = var.upper()
                cum_expr += f' {cumc}! '
                cumc = f'{cumc}@'

        if comp_blur:
            clips = [clip, *intermediates, comp_blur]
            cum_expr = f'x {cum_expr} - {ExprVars[n_inter + 1]} +'
        else:
            clips = [clip, *intermediates]

        cum = norm_expr(clips, cum_expr, planes, func=side_box_blur)
    else:
        cum = intermediates[0]
        for new in intermediates[1:]:
            cum = limit_filter(clip, cum, new, LimitFilterMode.SIMPLE2_MIN, planes)

        if comp_blur:
            cum = clip.std.MakeDiff(cum).std.MergeDiff(comp_blur)

    if comp_blur:
        return box_blur(cum, 1, min(radius // 2, 1))

    return cum