Bilateral Filter

# Overview

The Bilateral Filter is a non-linear, edge-preserving smoothing filter that is commonly used in Computer Vision as a simple noise-reduction stage in a pipeline. It calculates the intensity of each output pixel as a weighted average of intensity values from nearby pixels in the input image. Crucially, the weights depend not only on the Euclidean distance between current and neighbor pixels, but also on the radiometric differences (e.g., color intensity differences) between them. The outcome is that edges are preserved while regions with similar intensities are smoothed out.

Input Parameters Output

\begin{align*} \mathit{kernelSize} &= 7x7 \\ \sigma_s &= 1.7 \\ \sigma_r &= 50 \end{align*}

# Implementation

The bilateral filter is defined as:

$I'(p) = \frac{1}{W_p} \sum_{q\in\Omega}I(p)k_r(\|I(q) - I(p)\|)k_s(\|p-q\|)$

and the normalization term, W, is defined as:

$W_p = \sum_{q\in\Omega}k_r(\|I(p)-I(q)\|)k_s(\|p-q\|)$

where

• $$I$$ and $$I'$$ are the input and output images, respectively.
• $$k_r$$ and $$k_s$$ are the range and space kernels, respectively, defined as the following non-normalized Gaussian functions:

\begin{align*} k_r(p) &= e^{-\frac{\|p\|^2}{2\sigma_r^2}} \\ k_s(p) &= e^{-\frac{\|p\|^2}{2\sigma_s^2}} \end{align*}

• $$\sigma_r$$ controls the intensity range that is smoothed out. Higher values will lead to larger regions being smoothed out. The $$\sigma_r$$ value should be selected with the dynamic range of the image pixel values in mind.
• $$\sigma_s$$ controls smoothing factor. Higher values will lead to more smoothing.

# C API functions

For list of limitations, constraints and backends that implements the algorithm, consult reference documentation of the following functions:

Function Description
vpiSubmitBilateralFilter Runs a 2D bilateral filter over an image.

# Usage

Language:
1. Import VPI module
import vpi
2. Run bilateral filter on input image using the CUDA backend. It's using a 7x7 kernel with $$\sigma_r=50$$ and $$\sigma_s=1.7$$, and ZERO boundary condition. Input and output are VPI images.
with vpi.Backend.CUDA:
output = input.bilateral_filter(5, 7, 50, border=vpi.Border.ZERO)
1. Initialization phase
1. Include the header that defines the Bilateral filter function.
Declares functions that implement the Bilateral Filter algorithm.
2. Define the input image.
VPIImage input = /*...*/;
struct VPIImageImpl * VPIImage
A handle to an image.
Definition: Types.h:256
3. Create the output image. It gets its dimensions and format from the input image.
int32_t w, h;
vpiImageGetSize(input, &w, &h);
vpiImageGetFormat(input, &type);
VPIImage output;
vpiImageCreate(w, h, type, 0, &output);
uint64_t VPIImageFormat
Pre-defined image formats.
Definition: ImageFormat.h:94
VPIStatus vpiImageGetFormat(VPIImage img, VPIImageFormat *format)
Get the image format.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageGetSize(VPIImage img, int32_t *width, int32_t *height)
Get the image dimensions in pixels.
4. Create the stream where the algorithm will be submitted for execution.
VPIStream stream;
vpiStreamCreate(0, &stream);
struct VPIStreamImpl * VPIStream
A handle to a stream.
Definition: Types.h:250
VPIStatus vpiStreamCreate(uint64_t flags, VPIStream *stream)
Create a stream instance.
2. Processing phase
1. Submit the algorithm to the stream using the CUDA backend, along with all parameters. Here we're using a 7x7 kernel with $$\sigma_r=50$$ and $$\sigma_s=1.7$$.
vpiSubmitBilateralFilter(stream, VPI_BACKEND_CUDA, input, output, 7, 50, 1.7, VPI_BORDER_ZERO);
VPIStatus vpiSubmitBilateralFilter(VPIStream stream, uint64_t backend, VPIImage input, VPIImage output, int32_t kernelSize, float sigmaRange, float sigmaSpace, VPIBorderExtension border)
Runs a 2D bilateral filter over an image.
@ VPI_BACKEND_CUDA
CUDA backend.
Definition: Types.h:93
@ VPI_BORDER_ZERO
All pixels outside the image are considered to be zero.
Definition: Types.h:278
2. Optionally, wait until the processing is done.
vpiStreamSync(stream);
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
3. Cleanup phase
1. Free resources held by the stream and the input and output images.
vpiImageDestroy(output);
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.

For more information, see Bilateral Filter in the "C API Reference" section of VPI - Vision Programming Interface.

# Performance

For information on how to use the performance table below, see Algorithm Performance Tables.
Before comparing measurements, consult Comparing Algorithm Elapsed Times.
For further information on how performance was benchmarked, see Performance Benchmark.

-