Edge-Preserving Filters Demonstration#

This notebook demonstrates different edge-preserving filters on the blobs.tif sample image. We will compare the following filters:

  • Median filter

  • Bilateral filter

  • Kuwahara filter

Edge-preserving filters are useful for noise reduction while maintaining important structural features like edges and boundaries in images.

import numpy as np
import stackview
from skimage import filters, io
import ipywidgets as widgets
from IPython.display import display
from gpu_image_pro import bilateral, kuwahara
from pyclesperanto_prototype import median_box

Load Sample Image#

We load the blobs.tif sample image which contains circular blob-like structures that are ideal for demonstrating edge-preserving filtering.

# Load the blobs sample image
original_image = io.imread("../../data/blobs.tif")

stackview.insight(original_image)
shape(254, 256)
dtypeuint8
size63.5 kB
min8
max248

Apply Median Filter#

The median filter replaces each pixel with the median value of its neighborhood. It’s particularly effective at removing salt-and-pepper noise while preserving edges.

median_filtered_5 = median_box(original_image, radius_x=5, radius_y=5)

stackview.insight(median_filtered_5)
shape(254, 256)
dtypefloat32
size254.0 kB
min24.0
max248.0

Apply Bilateral Filter#

The bilateral filter reduces noise while preserving edges by considering both spatial distance and intensity difference between pixels.

bilateral(original_image, sigma_color=0.2, sigma_spatial=2)
shape(254, 256)
dtypeuint8
size63.5 kB
min7
max248

Apply Kuwahara Filter#

The Kuwahara filter creates an artistic painterly effect while preserving edges by selecting the region with minimum variance in the neighborhood.

kuwahara(original_image, kernel_size=5)
shape(254, 256)
dtypeuint8
size63.5 kB
min8
max248

Interactive User Interface#

import warnings
def median_filter(image, radius):
    return median_box(image, radius_x=radius, radius_y=radius)
    
stackview.interact(median_filter, original_image)
stackview.interact(bilateral, original_image)
stackview.interact(kuwahara, original_image)

Exercise#

Play with the percentile filter, e.g. using paramerter percentile=50. Is it an edge-preserving filter, too?

from scipy.ndimage import percentile_filter