• AlbumentationsAlbumentations
All TransformsGet LicenseDocumentationNews & Insights
Report IssueJoin Discord...

MedianBlur

Targets:
image
Image Types:uint8, float32

Replace each pixel with median in a square window. Removes salt-and-pepper noise; edges sharper than box or Gaussian. Kernel size from blur_range.

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_range
tuple[int, int]
[3,7]

Inclusive range of the median filter aperture linear size. Both ends must be odd and >= 3. 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_range=(3, 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_range=(5, 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_range=(9, 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_range=(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(std_range=(0.04, 0.2), p=0.5),     # Possibly add some noise
...     A.MedianBlur(blur_range=(3, 5), p=0.7),         # 70% chance of applying median blur
...     A.RandomBrightnessContrast(brightness_range=(-0.1, 0.1), contrast_range=(-0.1, 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.
References
  • Median filterhttps://en.wikipedia.org/wiki/Median_filter
  • OpenCV medianBlurhttps://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9