CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-itk

Insight Toolkit for N-dimensional image processing, segmentation, and registration in medical and scientific applications

Pending
Overview
Eval results
Files

smoothing.mddocs/reference/filtering/

Smoothing Filters

Noise reduction and image smoothing operations including median, mean, Gaussian, and edge-preserving filters.

Capabilities

Median Filtering

Non-linear filter that replaces each pixel with the median of neighboring pixels, excellent for impulse noise removal while preserving edges.

def median_image_filter(input, radius=1):
    """
    Apply median filtering to reduce noise.
    
    Parameters:
    - input: itk.Image - Input image
    - radius: int or itk.Size - Neighborhood radius (scalar or per-dimension)
    
    Returns:
    itk.Image - Median filtered image
    """

class MedianImageFilter:
    """
    Median filter for noise reduction.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetRadius(self, radius):
        """
        Set the neighborhood radius.
        
        Parameters:
        - radius: int or itk.Size - Radius in each dimension
        """
    
    def GetRadius(self):
        """Get the current radius."""

Mean Filtering

Linear smoothing filter that replaces each pixel with the mean of neighboring pixels.

def mean_image_filter(input, radius=1):
    """
    Apply mean filtering for smoothing.
    
    Parameters:
    - input: itk.Image - Input image
    - radius: int or itk.Size - Neighborhood radius
    
    Returns:
    itk.Image - Mean filtered image
    """

class MeanImageFilter:
    """
    Mean filter for image smoothing.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetRadius(self, radius):
        """Set the neighborhood radius."""

Gaussian Smoothing

Linear smoothing using Gaussian kernel, provides isotropic blurring.

def discrete_gaussian_image_filter(input, variance=1.0, maximum_error=0.01, maximum_kernel_width=32):
    """
    Apply discrete Gaussian smoothing.
    
    Parameters:
    - input: itk.Image - Input image
    - variance: float or list - Gaussian variance (can be per-dimension)
    - maximum_error: float - Maximum approximation error
    - maximum_kernel_width: int - Maximum kernel size
    
    Returns:
    itk.Image - Smoothed image
    """

class DiscreteGaussianImageFilter:
    """
    Discrete Gaussian smoothing filter.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetVariance(self, variance):
        """
        Set the variance of the Gaussian.
        
        Parameters:
        - variance: float or array - Variance in each dimension
        """
    
    def SetMaximumError(self, error):
        """
        Set the maximum approximation error.
        
        Parameters:
        - error: float - Maximum error (default 0.01)
        """
    
    def SetMaximumKernelWidth(self, width):
        """
        Set the maximum kernel width.
        
        Parameters:
        - width: int - Maximum kernel size
        """
    
    def SetUseImageSpacing(self, use_spacing):
        """
        Enable/disable using image spacing in calculations.
        
        Parameters:
        - use_spacing: bool
        """

def smoothing_recursive_gaussian_image_filter(input, sigma=1.0):
    """
    Apply recursive Gaussian smoothing (efficient for large sigma).
    
    Parameters:
    - input: itk.Image - Input image
    - sigma: float or list - Gaussian sigma (standard deviation)
    
    Returns:
    itk.Image - Smoothed image
    """

class SmoothingRecursiveGaussianImageFilter:
    """
    Recursive implementation of Gaussian smoothing, efficient for large kernels.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type (typically float)
    """
    
    def SetSigma(self, sigma):
        """
        Set the sigma (standard deviation) of the Gaussian.
        
        Parameters:
        - sigma: float or array - Sigma in each dimension
        """
    
    def SetNormalizeAcrossScale(self, normalize):
        """
        Enable normalization across scale.
        
        Parameters:
        - normalize: bool
        """

class RecursiveGaussianImageFilter:
    """
    Base recursive Gaussian filter for single direction.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetDirection(self, direction):
        """
        Set the direction for 1D Gaussian.
        
        Parameters:
        - direction: int - Axis direction (0, 1, 2, etc.)
        """
    
    def SetSigma(self, sigma):
        """Set the Gaussian sigma."""
    
    def SetOrder(self, order):
        """
        Set the order of the derivative.
        
        Parameters:
        - order: int - 0 (smoothing), 1 (first derivative), 2 (second derivative)
        """

Bilateral Filtering

Edge-preserving smoothing that considers both spatial proximity and intensity similarity.

def bilateral_image_filter(input, domain_sigma=4.0, range_sigma=50.0):
    """
    Edge-preserving bilateral filtering.
    
    Parameters:
    - input: itk.Image - Input image
    - domain_sigma: float - Spatial standard deviation
    - range_sigma: float - Intensity standard deviation
    
    Returns:
    itk.Image - Bilaterally filtered image
    """

class BilateralImageFilter:
    """
    Bilateral filter for edge-preserving smoothing.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetDomainSigma(self, sigma):
        """
        Set the spatial standard deviation.
        
        Parameters:
        - sigma: float - Controls the spatial extent of filtering
        """
    
    def SetRangeSigma(self, sigma):
        """
        Set the intensity standard deviation.
        
        Parameters:
        - sigma: float - Controls sensitivity to intensity differences
        """
    
    def SetNumberOfRangeGaussianSamples(self, samples):
        """
        Set the number of samples in the intensity domain.
        
        Parameters:
        - samples: int - Number of Gaussian samples
        """

Binomial Blur

Simple smoothing using binomial coefficients.

def binomial_blur_image_filter(input, repetitions=1):
    """
    Apply binomial blur smoothing.
    
    Parameters:
    - input: itk.Image - Input image
    - repetitions: int - Number of times to apply the filter
    
    Returns:
    itk.Image - Smoothed image
    """

class BinomialBlurImageFilter:
    """
    Binomial blur smoothing filter.
    
    Template parameters:
    - input_image_type: Input image type
    - output_image_type: Output image type
    """
    
    def SetRepetitions(self, repetitions):
        """
        Set the number of repetitions.
        
        Parameters:
        - repetitions: int - Number of smoothing iterations
        """

Patch-Based Denoising

Advanced denoising using non-local means and patch-based approaches.

class PatchBasedDenoisingImageFilter:
    """
    Patch-based denoising filter using non-local means approach.
    
    Template parameters:
    - image_type: Image type
    """
    
    def SetPatchRadius(self, radius):
        """
        Set the patch radius.
        
        Parameters:
        - radius: int - Radius of patches to compare
        """
    
    def SetNoiseModel(self, model):
        """
        Set the noise model.
        
        Parameters:
        - model: NoiseModelEnum - NOMODEL, GAUSSIAN, RICIAN, POISSON
        """
    
    def SetNumberOfIterations(self, iterations):
        """
        Set the number of denoising iterations.
        
        Parameters:
        - iterations: int
        """
    
    def SetSmoothingWeight(self, weight):
        """
        Set the smoothing weight.
        
        Parameters:
        - weight: float - Controls amount of smoothing
        """

Usage Examples

Basic Smoothing

import itk

# Load noisy image
image = itk.imread('noisy.png', itk.F)

# Median filtering (good for salt-and-pepper noise)
smoothed_median = itk.median_image_filter(image, radius=2)
itk.imwrite(smoothed_median, 'median.png')

# Mean filtering
smoothed_mean = itk.mean_image_filter(image, radius=2)
itk.imwrite(smoothed_mean, 'mean.png')

# Gaussian filtering
smoothed_gaussian = itk.discrete_gaussian_image_filter(image, variance=2.0)
itk.imwrite(smoothed_gaussian, 'gaussian.png')

Anisotropic Smoothing with Different Radii

import itk

image = itk.imread('input.png', itk.F)

# Create radius with different values per dimension
ImageType = itk.Image[itk.F, 2]
filter = itk.MedianImageFilter[ImageType, ImageType].New()
filter.SetInput(image)

radius = itk.Size[2]()
radius[0] = 5  # Smooth more in x direction
radius[1] = 2  # Smooth less in y direction
filter.SetRadius(radius)

filter.Update()
result = filter.GetOutput()
itk.imwrite(result, 'anisotropic_median.png')

Edge-Preserving Bilateral Filtering

import itk

image = itk.imread('input.png', itk.F)

# Bilateral filter preserves edges while smoothing
# domain_sigma: spatial extent
# range_sigma: intensity similarity
smoothed = itk.bilateral_image_filter(
    image,
    domain_sigma=4.0,  # Smooth over 4-pixel neighborhood
    range_sigma=50.0   # Consider pixels within intensity range of 50
)

itk.imwrite(smoothed, 'bilateral.png')

Multi-Scale Gaussian Smoothing

import itk

image = itk.imread('input.png', itk.F)

# Apply Gaussian smoothing at multiple scales
sigmas = [1.0, 2.0, 4.0, 8.0]

for sigma in sigmas:
    smoothed = itk.smoothing_recursive_gaussian_image_filter(image, sigma=sigma)
    itk.imwrite(smoothed, f'gaussian_sigma_{sigma}.png')

Recursive Gaussian for Large Kernels

import itk

image = itk.imread('large_image.nii.gz', itk.F)

# For large sigma values, recursive Gaussian is much faster
# than discrete Gaussian
smoothed = itk.smoothing_recursive_gaussian_image_filter(
    image,
    sigma=10.0  # Large smoothing
)

itk.imwrite(smoothed, 'large_smooth.nii.gz')

Custom Gaussian Parameters

import itk

image = itk.imread('input.png', itk.F)

ImageType = itk.Image[itk.F, 2]
gaussian = itk.DiscreteGaussianImageFilter[ImageType, ImageType].New()
gaussian.SetInput(image)

# Set variance (sigma^2)
gaussian.SetVariance(4.0)

# Control approximation accuracy
gaussian.SetMaximumError(0.01)

# Limit kernel size
gaussian.SetMaximumKernelWidth(64)

# Use image spacing in calculations
gaussian.SetUseImageSpacing(True)

gaussian.Update()
result = gaussian.GetOutput()

Patch-Based Denoising

import itk

image = itk.imread('noisy.png', itk.F)

ImageType = itk.Image[itk.F, 2]
denoiser = itk.PatchBasedDenoisingImageFilter[ImageType, ImageType].New()
denoiser.SetInput(image)

# Set patch radius
denoiser.SetPatchRadius(4)

# Set noise model
denoiser.SetNoiseModel(itk.PatchBasedDenoisingImageFilter.NoiseModelEnum_GAUSSIAN)

# Set number of iterations
denoiser.SetNumberOfIterations(10)

# Set smoothing parameters
denoiser.SetSmoothingWeight(0.8)

denoiser.Update()
denoised = denoiser.GetOutput()
itk.imwrite(denoised, 'denoised.png')

Comparison of Smoothing Methods

import itk
import numpy as np

# Load noisy image
image = itk.imread('noisy.png', itk.F)

# Apply different smoothing methods
methods = {
    'median': lambda img: itk.median_image_filter(img, radius=2),
    'mean': lambda img: itk.mean_image_filter(img, radius=2),
    'gaussian': lambda img: itk.discrete_gaussian_image_filter(img, variance=2.0),
    'bilateral': lambda img: itk.bilateral_image_filter(img, domain_sigma=4.0, range_sigma=50.0),
}

for name, method in methods.items():
    smoothed = method(image)
    itk.imwrite(smoothed, f'{name}_result.png')
    print(f"{name} smoothing complete")

Install with Tessl CLI

npx tessl i tessl/pypi-itk

docs

index.md

tile.json