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 |
|---|---|---|---|
|
|
tabby_tiger_cat.jpg |
Input image file path |
|
|
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:
Kernel size:
ksizeis a two-element list[kW, kH]where both values must be odd positive integers. Larger kernels produce stronger smoothing.Noise removal: Median blur is especially effective for removing impulse (salt-and-pepper) noise because the median statistic is robust to outliers.
Edge preservation: Unlike mean blur, median blur preserves edges well since the median value is always drawn from actual pixel values in the neighborhood.
Supported types: The operator supports
uint8,uint16, andfloat32data types in HWC or NHWC layout.
Expected Output
The output shows the image with impulse noise suppressed and edges intact:
Original Input Image |
Output: Median Blur (7×7 kernel) |
CV-CUDA Operators Used
Operator |
Purpose |
|---|---|
Apply median blur filter to remove noise while preserving edges |
Common Utilities Used
read_image() - Load image as CV-CUDA tensor
write_image() - Save blurred image
See Also
Resize Operator - Resize images with interpolation
Common Utilities - Helper functions