Median Blur

Overview

The Median Blur sample demonstrates GPU-accelerated median filtering using CV-CUDA’s median_blur operator. Median blur replaces each pixel with the median value of its neighborhood, making it highly effective for removing salt-and-pepper noise while preserving edges better than a simple averaging blur.

Usage

Basic Usage

Apply a 7×7 median blur to an image (default):

python3 median_blur.py -i input.jpg

Custom Output Path

Specify a custom output path:

python3 median_blur.py -i input.jpg -o blurred.jpg

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

--output

-o

cvcuda/.cache/cat_median_blur.jpg

Output image file path

Implementation

Median Blur Operation

# Apply median blur with a 7x7 kernel.
# ksize must be a list of two odd positive integers [kW, kH].
# Larger kernels produce stronger smoothing and better noise removal
# at the cost of more detail loss.
output_image: cvcuda.Tensor = cvcuda.median_blur(input_image, [7, 7])
write_image(output_image, args.output)

Key points:

  1. Kernel size: ksize is a two-element list [kW, kH] where both values must be odd positive integers. Larger kernels produce stronger smoothing.

  2. Noise removal: Median blur is especially effective for removing impulse (salt-and-pepper) noise because the median statistic is robust to outliers.

  3. Edge preservation: Unlike mean blur, median blur preserves edges well since the median value is always drawn from actual pixel values in the neighborhood.

  4. Supported types: The operator supports uint8, uint16, and float32 data types in HWC or NHWC layout.

Expected Output

The output shows the image with impulse noise suppressed and edges intact:

../../_images/tabby_tiger_cat.jpg

Original Input Image

../../_images/cat_median_blur.jpg

Output: Median Blur (7×7 kernel)

CV-CUDA Operators Used

Operator

Purpose

cvcuda.median_blur()

Apply median blur filter to remove noise while preserving edges

Common Utilities Used

See Also