Main Content


2-D and 3-D mode filtering

Since R2020a



B = modefilt(A) performs mode filtering on the 2-D image or 3-D volume A. Each output pixel in B contains the mode (most frequently occurring value) in the neighborhood around the corresponding pixel in A. modefilt pads A by mirroring border elements.

Mode filtering can be useful for processing categorical data, where other types of filtering, such as median filtering, are not available.

B = modefilt(A,filtSize) also specifies the size of the filter neighborhood.

B = modefilt(___,padopt) also specifies how modefilt pads array boundaries.


collapse all

Load an image (img) and the corresponding categorical labeled version of the image (label) into the workspace.

load buildingPixelLabeled;

View the original image, img.


View the categorical labeled image, label. The categorical image labels four separate categories: sky, grass, building, and sidewalk. For viewing, convert these categories to colors using the label2rgb function.


Perform mode filtering on the categorical labeled image, label, using the default filter size and padding method.

 b = modefilt(label);

View the filtered categorical labeled image, b. In the filtered image, the edges between labeled regions are more distinct.


Read a grayscale image of a brain MRI and the corresponding labels. The image loads in the workspace variable vol and the labels load in the workspace variable label.

dataDir = fullfile(toolboxdir("images"),"imdata","BrainMRILabeled");

Display the labeled volume.

viewer1 = viewer3d(BackgroundColor="white",BackgroundGradient="off",CameraZoom=1.5);
vol1 = volshow(vol,OverlayData=label,Parent=viewer1, ...
    RenderingStyle="GradientOpacity",GradientOpacityValue=0.8, ...

Perform mode filtering on the labels, specifying the size of the filter.

labelFiltered = modefilt(label,[5 5 5]);

Display the filtered labeled volume.

viewer2 = viewer3d(BackgroundColor="white",BackgroundGradient="off",CameraZoom=1.5);
vol2 = volshow(vol,OverlayData=labelFiltered,Parent=viewer2, ...
    RenderingStyle="GradientOpacity",GradientOpacityValue=0.8, ...

Input Arguments

collapse all

2-D image or 3-D volume, specified as a categorical, logical, or numeric array.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical | categorical

Filter size, specified as a vector of positive odd integers. For 2-D images, specify a vector of the form [height width]. The default for 2-D images is [3 3]. For 3-D volumes, specify a vector of the form [height width depth]. The default for 3-D volumes is [3 3 3].

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Padding method, specified as one of the following values.

"symmetric"Pad array with a mirror reflection of itself.
"replicate"Pad array by repeating border elements.
"zeros"Pad array with 0s for numeric data or with <undefined>s for categorical data.

Data Types: char | string

Output Arguments

collapse all

Filtered image or volume, returned as a numeric array of the same size and class as the input image A.


  • When the neighborhood has more than one pixel in a tie for the mode value, the function uses the following tie-breaking algorithm:

    • If the center pixel is one of the mode values in the tie, the function uses this value.

    • If the center pixel is not one of the mode values in the tie, the function uses the mode with the smallest numeric value.

    • For categorical input, the function chooses the first category (among the categories tied for mode) that appears in the list returned by categories(A).

  • modefilt treats RGB images as 3-D volumes. To do channel-wise filtering of an RGB image, specify filtSize as [3 3 1], as in this code: b = modefilt(a,[3 3 1]); .

Extended Capabilities

Version History

Introduced in R2020a

expand all