Bilateral Filter

Overview

The Bilateral Filter sample demonstrates edge-preserving image smoothing using CV-CUDA’s GPU-accelerated bilateral filter operator. Unlike a standard Gaussian blur, the bilateral filter weighs contributions by both spatial proximity and color similarity, so it reduces noise in flat regions while leaving edges sharp.

Usage

Basic Usage

Apply bilateral filter to an image with default parameters:

python3 bilateral_filter.py -i input.jpg

Custom Parameters

Specify a custom output path:

python3 bilateral_filter.py -i input.jpg -o cat_bilateral_filter.jpg

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

--output

-o

cvcuda/.cache/cat_bilateral_filter.jpg

Output image file path

Implementation

Bilateral Filter Application

# Apply bilateral filter: preserves edges while smoothing flat regions.
# - diameter: pixel neighborhood size (larger = stronger smoothing but slower)
# - sigma_color: color space standard deviation; larger values allow more
#   dissimilar colors to be blended together, weakening edge preservation
# - sigma_space: spatial standard deviation; larger values mean pixels
#   farther away influence each other, similar to a Gaussian blur radius
output_image: cvcuda.Tensor = cvcuda.bilateral_filter(
    input_image,
    diameter=9,
    sigma_color=75,
    sigma_space=75,
    border=cvcuda.Border.REFLECT,
)
write_image(output_image, args.output)

Key points:

  1. Edge Preservation: Unlike Gaussian blur, bilateral filter preserves sharp edges by weighting pixel contributions by color similarity (sigma_color) as well as spatial distance (sigma_space).

  2. Diameter: Controls the size of the pixel neighborhood considered for each output pixel. Larger values produce stronger smoothing but increase runtime.

  3. Sigma Color: Higher values allow more dissimilar colors to be blended, reducing edge-preservation strength toward a plain Gaussian blur.

  4. Sigma Space: Controls spatial falloff; behaves like the radius of a Gaussian blur and determines how far neighboring pixels contribute.

  5. Border Mode: cvcuda.Border.REFLECT mirrors edge pixels outward, avoiding darkening or artifacts at image boundaries.

Expected Output

The output retains sharp edges (fur markings, whiskers) while noise and texture in flat regions is smoothed:

../../_images/tabby_tiger_cat.jpg

Original Input Image

../../_images/cat_bilateral_filter.jpg

Output: Edge-Preserving Bilateral Filter

CV-CUDA Operators Used

Operator

Purpose

cvcuda.bilateral_filter()

Apply edge-preserving bilateral smoothing to an image

Common Utilities Used

See Also