MedianBlur

Targets:
image
volume
Image Types:uint8, float32

Apply median blur to the input image.

This transform uses a median filter to blur the input image. Median filtering is particularly effective at removing salt-and-pepper noise while preserving edges, making it a popular choice for noise reduction in image processing.

Arguments
blur_limit
tuple[int, int] | int
[3,7]

Maximum aperture linear size for blurring the input image. Must be odd and in the range [3, inf).

  • If a single int is provided, the kernel size will be randomly chosen between 3 and that value.
  • If a tuple of two ints is provided, it defines the inclusive range of possible kernel sizes. Default: (3, 7)
p
float
0.5

Probability of applying the transform. Default: 0.5

Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Create a sample image for demonstration
>>> image = np.zeros((300, 300, 3), dtype=np.uint8)
>>> # Add some shapes to visualize blur effects
>>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1)  # Red square
>>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1)  # Green circle
>>> cv2.putText(image, "Sample Text", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
>>>
>>> # Add salt and pepper noise to demonstrate median blur's noise removal capability
>>> noise = np.zeros((300, 300, 3), dtype=np.uint8)
>>> noise_points = np.random.random((300, 300)) > 0.95  # 5% of pixels as noise
>>> image[noise_points] = 255  # White noise (salt)
>>> noise_points = np.random.random((300, 300)) > 0.95  # Another 5% of pixels
>>> image[noise_points] = 0    # Black noise (pepper)
>>>
>>> # Example 1: Minimal median blur (3x3 kernel)
>>> minimal_blur = A.Compose([
...     A.MedianBlur(
...         blur_limit=3,  # Fixed 3x3 kernel
...         p=1.0          # Always apply
...     )
... ])
>>>
>>> minimal_result = minimal_blur(image=image)
>>> minimal_blurred = minimal_result["image"]
>>> # The image will have minimal median blur, removing most salt and pepper noise
>>> # while preserving edges and details
>>>
>>> # Example 2: Medium median blur
>>> medium_blur = A.Compose([
...     A.MedianBlur(
...         blur_limit=5,  # Fixed 5x5 kernel
...         p=1.0
...     )
... ])
>>>
>>> medium_result = medium_blur(image=image)
>>> medium_blurred = medium_result["image"]
>>> # The image will have a medium median blur, removing noise and small details
>>> # while still preserving major edges
>>>
>>> # Example 3: Strong median blur
>>> strong_blur = A.Compose([
...     A.MedianBlur(
...         blur_limit=9,  # Fixed 9x9 kernel
...         p=1.0
...     )
... ])
>>>
>>> strong_result = strong_blur(image=image)
>>> strong_blurred = strong_result["image"]
>>> # The image will have a strong median blur, potentially removing smaller
>>> # features while still preserving major edges better than other blur types
>>>
>>> # Example 4: Random kernel size range
>>> random_kernel = A.Compose([
...     A.MedianBlur(
...         blur_limit=(3, 9),  # Kernel size between 3x3 and 9x9
...         p=1.0
...     )
... ])
>>>
>>> random_result = random_kernel(image=image)
>>> random_blurred = random_result["image"]
>>> # The image will have a random median blur strength
>>>
>>> # Example 5: In a pipeline for noise reduction
>>> pipeline = A.Compose([
...     A.GaussNoise(var_limit=(10, 50), p=0.5),        # Possibly add some noise
...     A.MedianBlur(blur_limit=(3, 5), p=0.7),         # 70% chance of applying median blur
...     A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.3)
... ])
>>>
>>> pipeline_result = pipeline(image=image)
>>> processed_image = pipeline_result["image"]
>>> # The image may have been denoised with the median blur (70% probability)
Notes
  • The kernel size (aperture linear size) must always be odd and greater than 1.
  • Unlike mean blur or Gaussian blur, median blur uses the median of all pixels under the kernel area, making it more robust to outliers.
  • This transform is particularly useful for:
    • Removing salt-and-pepper noise
    • Preserving edges while smoothing images
    • Pre-processing images for edge detection algorithms
  • For color images, the median is calculated independently for each channel.
  • Larger kernel sizes result in stronger blurring effects but may also remove fine details from the image.