This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

vision.MedianFilter System object

Package: vision

2D median filtering


    Note:   The vision.MedianFilter System object will be removed in a future release. Use the medfilt2 function with equivalent functionality instead.

The MedianFilter object performs 2D median filtering.

    Note:   Starting in R2016b, instead of using the step method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform equivalent operations.


H = vision.MedianFilter returns a 2D median filtering object, H. This object performs two-dimensional median filtering of an input matrix.

H = vision.MedianFilter(Name,Value) returns a median filter object, H, with each property set to the specified value. You can specify additional name-value pair arguments in any order as (Name1, Value1,...,NameN,ValueN).

H = vision.MedianFilter(SIZE,Name,Value) returns a median filter object, H, with the NeighborhoodSize property set to SIZE and other properties set to the specified values.

Code Generation Support
Supports MATLAB® Function block: Yes
System Objects in MATLAB Code Generation.
Code Generation Support, Usage Notes, and Limitations.



Size of neighborhood to compute the median

Specify the size of the neighborhood over which the median filter computes the median. When you set this property to a positive integer, the integer represents the number of rows and columns in a square matrix. When you set this property to a two-element vector, the vector represents the number of rows and columns in a rectangular matrix. The median filter does not support fixed-point properties when both neighborhood dimensions are odd. The default is [3 3].


Output matrix size

Specify the output size as one of Same as input size | Valid. The default is Same as input size. When you set this property to Valid, the 2D median filter only computes the median where the neighborhood fits entirely within the input image and requires no padding. In this case, the dimensions of the output image are:

output rows = input rows - neighborhood rows + 1
output columns = input columns - neighborhood columns + 1

Otherwise, the object outputs the same dimensions as the input image.


Input matrix boundary padding method

Specifies how to pad the boundary of the input matrix as one of Constant | Replicate | Symmetric | Circular. When you set this property to Constant, the object pads the matrix with a constant value. When you set this property to Replicate, the object pads the input matrix by repeating its border values. When you set this property to Symmetric, the object pads the input matrix with its mirror image. When you set this property to Circular the object pads the input matrix using a circular repetition of its elements. This property applies only when you set the OutputSize property to Same as input size. The default is Constant.


Source of constant boundary value

Specifies how to define the constant boundary value as one of Property | Input port. This property applies only when you set the PaddingMethod property to Constant. The default is Property.


Constant padding value for input matrix

Specifies a constant value to pad the input matrix. The default is 0. This property applies only when you set the PaddingMethod property to Constant and the PaddingValueSource property to Property. This property is tunable.

 Fixed-Point Properties


cloneCreate 2D median filter with same property values
getNumInputsNumber of expected inputs to step method
getNumOutputsNumber of outputs from step method
isLockedLocked status for input attributes and non-tunable properties
release Allow property value and input characteristics changes
stepPerform median filtering on input image


expand all

Read the image and add noise.

I = imread('pout.tif');
I = imnoise(I, 'salt & pepper')

Create a median filter object.

medianFilter = vision.MedianFilter([5 5]);

Filter the image.

I_filtered = step(medianFilter, I);

Display the results with a title.

figure; imshowpair(I, I_filtered, 'montage'); 
title('Noisy image on the left and filtered image on the right');


This object implements the algorithm, inputs, and outputs described on the Median Filter block reference page. The object properties correspond to the block parameters.

Introduced in R2012a

Was this topic helpful?