Main Content


2-D median filtering


visionhdl.MedianFilter performs 2-D median filtering on a pixel stream. The object replaces each pixel value with the median value of the adjacent pixels.

This object uses a streaming pixel interface with a structure for frame control signals. This interface enables the object to operate independently of image size and format, and to connect with other Vision HDL Toolbox™ objects. The object accepts and returns a scalar pixel value and control signals as a structure containing five signals. The control signals indicate the validity of each pixel and its location in the frame. To convert a pixel matrix into a pixel stream and control signals, use the visionhdl.FrameToPixels object. For a full description of the interface, see Streaming Pixel Interface.


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.


MF = visionhdl.MedianFilter returns a System object, MF, that performs two-dimensional median filtering of serial pixel data.

MF = visionhdl.MedianFilter(Name,Value) returns a median filter System object, MF, with additional options specified by one or more Name,Value pair arguments. Name is a property name and Value is the corresponding value. Name must appear inside single quotes (''). You can specify several name-value pair arguments in any order as Name1,Value1,...,NameN,ValueN. Properties not specified retain their default values.

MF = visionhdl.MedianFilter(size,Name,Value) returns a median filter System object, MF, with the NeighborhoodSize property set to size and additional options specified by one or more Name,Value pair arguments.

Input Arguments


Size in pixels of the image region used to compute the median. This argument sets the NeighborhoodSize property value.

Output Arguments


visionhdl.MedianFilter System object.



Neighborhood size, in pixels.

  • '3×3' (default)

  • '5×5'

  • '7×7'


Specify a power of two that accommodates the number of active pixels in a single horizontal line.

Choose a power of 2 that accommodates the number of active pixels in a horizontal line. If you specify a value that is not a power of two, the object uses the next largest power of two. The object allocates N - 1-by-LineBufferSize memory locations to store the pixels used to compute the median value. N is the number of lines in the square region specified in Neighborhood size.

Default: 2048


Select one of these methods for padding the boundary of the input image.

  • 'Constant' — Interpret pixels outside the image frame as having a constant value.

  • 'Replicate' — Repeat the value of pixels at the edge of the image.

  • 'Symmetric' — Set the value of the padding pixels to mirror the edge of the image.

  • 'None' — Exclude padding logic. The object does not set the pixels outside the image frame to any particular value. This option reduces the hardware resources used by the object and the blanking required between frames but affects the accuracy of the output pixels at the edges of the frame. To maintain pixel stream timing, the output frame is the same size as the input frame. However, to avoid using pixels calculated from undefined padding values, mask off the n/2 pixels around the edge of the frame for downstream operations, where n is the size of the operation kernel. For details, see Increase Throughput with Padding None.

For more information about these methods, see Edge Padding.

Default: 'Symmetric'


Constant value used to pad the boundary of the input image. This property applies when you set PaddingMethod to 'Constant'. The object casts this value to the same data type as the input pixel.

Default: 0


stepMedian pixel value of neighborhood
Common to All System Objects

Allow System object property value changes


collapse all

This example implements a 5×5 median filter on a thumbnail image.

Load the source image from a file. Select a portion of the image matching the desired test size.

frmOrig = imread('rice.png');
frmActiveLines = 48;
frmActivePixels = 64;
frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels);
title 'Input Image'

Figure contains an axes. The axes with title Input Image contains an object of type image.

Create a serializer object and specify the size of the inactive pixel regions.

frm2pix = visionhdl.FrameToPixels(...

Create a filter object and specify a neighbourhood.

 medianfilt = visionhdl.MedianFilter(...

Serialize the test image by calling the serializer object. pixIn is a vector of intensity values. ctrlIn is a vector of control signal structures.

Note: This object syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent step syntax. For example, replace myObject(x) with step(myObject,x).

[pixIn,ctrlIn] = frm2pix(frmInput);

Prepare to process pixels by preallocating output vectors.

[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
pixOut = zeros(numPixelsPerFrame,1,'uint8');
ctrlOut  = repmat(pixelcontrolstruct,numPixelsPerFrame,1);

For each pixel in the padded frame, compute the local median. Monitor control signals to determine latency of the object. The latency of a filter configuration depends on:

  • The number of active pixels in a line.

  • The size of the neighbourhood.

foundValIn = false;
foundValOut = false;
for p = 1:numPixelsPerFrame  
    if (ctrlIn(p).valid && foundValIn==0)
        foundValIn = p;
    [pixOut(p),ctrlOut(p)] = medianfilt(pixIn(p),ctrlIn(p));
    if (ctrlOut(p).valid && foundValOut==0)
        foundValOut = p;
sprintf('object latency is %d cycles',foundValOut-foundValIn)
ans = 
'object latency is 177 cycles'

Create a deserializer object with a format matching that of the serializer. Convert the pixel stream to an image frame by calling the deserializer object. Display the resulting image.

pix2frm = visionhdl.PixelsToFrame(...
[frmOutput,frmValid] = pix2frm(pixOut,ctrlOut);
if frmValid
    imshow(frmOutput, 'InitialMagnification',300)
    title 'Output Image'

Figure contains an axes. The axes with title Output Image contains an object of type image.


This object implements the algorithms described on the Median Filter block reference page.

See Also

| | (Image Processing Toolbox)

Introduced in R2015a