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 |
|---|---|---|---|
|
|
tabby_tiger_cat.jpg |
Input image file path |
|
|
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:
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).Diameter: Controls the size of the pixel neighborhood considered for each output pixel. Larger values produce stronger smoothing but increase runtime.
Sigma Color: Higher values allow more dissimilar colors to be blended, reducing edge-preservation strength toward a plain Gaussian blur.
Sigma Space: Controls spatial falloff; behaves like the radius of a Gaussian blur and determines how far neighboring pixels contribute.
Border Mode:
cvcuda.Border.REFLECTmirrors 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:
Original Input Image |
Output: Edge-Preserving Bilateral Filter |
CV-CUDA Operators Used
Operator |
Purpose |
|---|---|
Apply edge-preserving bilateral smoothing to an image |
Common Utilities Used
read_image() - Load image as CV-CUDA tensor
write_image() - Save filtered image
See Also
Resize Operator - Resize images with GPU acceleration
Common Utilities - Helper functions