ImageDev

MedianFilter2d

Nonlinear filter smoothing a two-dimensional image with the median morphological operator.

Access to parameter description

For an introduction to image filters: section Images Filtering.

This algorithm uses morphological operators to set the pixel value to the median for the defined neighborhood. This filter is particularly effective for removing speckle noise and salt and pepper noise (impulsive noise) without affecting edges.
The median filter usually works well with images containing non-Gaussian noise and/or very small artifacts. It does not cause blurring to the same extent as the Box Filter, but takes longer to execute.

The gray levels of all pixels of the considered neighborhood are sorted from the smallest value to the largest one. The central pixel in the sort is then the median value (the value for which there are as many lower gray levels as higher ones). The process can be iterated.

Considering the array: $$ \begin{bmatrix} 12 & 17 & 15 \\ 20 & 14 & 16 \\ 18 & 19 & 14 \end{bmatrix} $$ The sorted gray level values are: [12 14 14 15 16 17 18 19 20]. The median value is 16, and it is assigned to the output pixel.

See also
See related examples

Function Syntax

This function returns the outputImage output parameter.
// Function prototype.
std::shared_ptr< iolink::ImageView >
medianFilter2d( std::shared_ptr< iolink::ImageView > inputImage,
                uint32_t kernelRadius,
                MedianFilter2d::KernelMode kernelMode,
                MedianFilter2d::SearchMode searchMode,
                std::shared_ptr< iolink::ImageView > outputImage = NULL );
This function returns the outputImage output parameter.
// Function prototype.
median_filter_2d( input_image,
                  kernel_radius = 3,
                  kernel_mode = MedianFilter2d.KernelMode.SQUARE,
                  search_mode = MedianFilter2d.SearchMode.AUTOMATIC,
                  output_image = None )
This function returns the outputImage output parameter.
// Function prototype.
public static IOLink.ImageView
MedianFilter2d( IOLink.ImageView inputImage,
                UInt32 kernelRadius = 3,
                MedianFilter2d.KernelMode kernelMode = ImageDev.MedianFilter2d.KernelMode.SQUARE,
                MedianFilter2d.SearchMode searchMode = ImageDev.MedianFilter2d.SearchMode.AUTOMATIC,
                IOLink.ImageView outputImage = null );

Class Syntax

Parameters

Class Name MedianFilter2d

Parameter Name Description Type Supported Values Default Value
input
inputImage
The input image. Image Binary, Label, Grayscale or Multispectral nullptr
input
kernelRadius
The half size of the structuring element. A square structuring element always has an odd side length (3x3, 5x5, etc.) which is defined by twice the kernel radius + 1. UInt32 >=1 3
input
kernelMode
The structuring element shape and application mode.
SQUARE This mode considers a square structuring element of side length 2 x kernelRadius + 1 pixels.
DISK This mode considers a disk structuring element of diameter 2 x kernelRadius + 1 pixels.
Enumeration SQUARE
input
searchMode
Handles all types of method for finding the median.
AUTOMATIC The algorithm automatically chooses the method to find the median value.
HISTOGRAM The algorithm uses a histogram to find the median value.
SELECTION The algorithm uses a shoting by selection to find the median value.
Enumeration AUTOMATIC
output
outputImage
The output image. Its dimensions and type are forced to the same values as the input. Image nullptr

Object Examples

std::shared_ptr< iolink::ImageView > polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

MedianFilter2d medianFilter2dAlgo;
medianFilter2dAlgo.setInputImage( polystyrene );
medianFilter2dAlgo.setKernelRadius( 3 );
medianFilter2dAlgo.setKernelMode( MedianFilter2d::KernelMode::SQUARE );
medianFilter2dAlgo.setSearchMode( MedianFilter2d::SearchMode::AUTOMATIC );
medianFilter2dAlgo.execute();

std::cout << "outputImage:" << medianFilter2dAlgo.outputImage()->toString();
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

median_filter_2d_algo = imagedev.MedianFilter2d()
median_filter_2d_algo.input_image = polystyrene
median_filter_2d_algo.kernel_radius = 3
median_filter_2d_algo.kernel_mode = imagedev.MedianFilter2d.SQUARE
median_filter_2d_algo.search_mode = imagedev.MedianFilter2d.AUTOMATIC
median_filter_2d_algo.execute()

print( "output_image:", str( median_filter_2d_algo.output_image ) )
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

MedianFilter2d medianFilter2dAlgo = new MedianFilter2d
{
    inputImage = polystyrene,
    kernelRadius = 3,
    kernelMode = MedianFilter2d.KernelMode.SQUARE,
    searchMode = MedianFilter2d.SearchMode.AUTOMATIC
};
medianFilter2dAlgo.Execute();

Console.WriteLine( "outputImage:" + medianFilter2dAlgo.outputImage.ToString() );

Function Examples

std::shared_ptr< iolink::ImageView > polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );

auto result = medianFilter2d( polystyrene, 3, MedianFilter2d::KernelMode::SQUARE, MedianFilter2d::SearchMode::AUTOMATIC );

std::cout << "outputImage:" << result->toString();
polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))

result = imagedev.median_filter_2d( polystyrene, 3, imagedev.MedianFilter2d.SQUARE, imagedev.MedianFilter2d.AUTOMATIC )

print( "output_image:", str( result ) )
ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );

IOLink.ImageView result = Processing.MedianFilter2d( polystyrene, 3, MedianFilter2d.KernelMode.SQUARE, MedianFilter2d.SearchMode.AUTOMATIC );

Console.WriteLine( "outputImage:" + result.ToString() );