2-D median filtering


  • B = medfilt2(A) example
  • B = medfilt2(A, [m n])
  • B = medfilt2(___,padopt)
  • gpuarrayB = medfilt2(gpuarrayA,___)



B = medfilt2(A) performs median filtering of the matrix A in two dimensions. Each output pixel contains the median value in a 3-by-3 neighborhood around the corresponding pixel in the input image. medfilt2 pads the image with 0's on the edges, so the median values for points within one-half the width of the neighborhood ([m n]/2) of the edges might appear distorted.

This function supports code generation (see Tips).

B = medfilt2(A, [m n]) performs median filtering, where each output pixel contains the median value in the m-by-n neighborhood around the corresponding pixel in the input image.

B = medfilt2(___,padopt) controls how medfilt2 pads the matrix boundaries.

gpuarrayB = medfilt2(gpuarrayA,___) performs the median filtering operation on a GPU. The input image and the output image are gpuArrays. When working with gpuArrays, medfilt2 only supports square neighborhoods with odd-length sides between 3 and 15. This syntax requires the Parallel Computing Toolbox™.


collapse all

Remove Salt and Pepper Noise from Image

Read image into workspace and display it.

I = imread('eight.tif');
figure, imshow(I)

Add salt and pepper noise.

J = imnoise(I,'salt & pepper',0.02);

Use a median filter to filter out the noise.

K = medfilt2(J);

Display results, side-by-side.


Remove Salt and Pepper Noise from Image Using a GPU

Read the image into a gpuArray.

I = gpuArray(imread('eight.tif'));

Read the image into a gpuArray.

J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
figure, imshow(J), figure, imshow(K)

Input Arguments

collapse all

A — Input matrix2-D, real, nonsparse, numeric or logical matrix

Input matrix, specified as a 2-D, real, nonsparse, numeric or logical matrix.

Example: I = imread('eight.tif'); J = medfilt2(I);

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

[m n] — Neighborhood size3-by-3 (default) | two-element numeric vector

Neighborhood size, specified as a two-element numeric vector, [m n], of real positive integers.

Example: J = medfilt2(gpuarrayI,[5 5]);

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

gpuarrayA — Input matrix when run on a GPUgpuArray

Input matrix when run on a GPU, specified as a gpuArray.

Example: gpuarrayI = gpuArray(imread('your_image_file.tif')); J = medfilt2(gpuarrayI);

padopt — Padding option'zeros' (default) | 'symmetric' | 'indexed'

Padding option, specified as pne of the following strings.

'zeros'Padded with 0s. This is the default.
'symmetric'Symmetrically extended at the boundaries.
'indexed'Padded with 1s, if the class of A is double; otherwise, padded with 0s.

Example: K = medfilt2(J,'symmetric');

Data Types: char

Output Arguments

collapse all

B — Output image2-D array

Output image, returned as a 2-D array of the same class as the input image A.

gpuarrayB — Output image when run on a GPUgpuArray

Output image when run on a GPU, returned as a gpuArray.

More About

collapse all


  • Median filtering is a nonlinear operation often used in image processing to reduce "salt and pepper" noise. A median filter is more effective than convolution when the goal is to simultaneously reduce noise and preserve edges. For information about performance considerations, see ordfilt2.

  • This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

    When generating code, the padopt argument must be a compile-time constant.

  • If the input image A is of an integer class, all the output values are returned as integers. If the number of pixels in the neighborhood (i.e., m*n) is even, some of the median values might not be integers. In these cases, the fractional parts are discarded. Logical input is treated similarly. For example, the true median for the following 2-by-2 neighborhood in a uint8 array is 4.5, but medfilt2 discards the fractional part and returns 4.

    1 5
    4 8


On the CPU, medfilt2 uses ordfilt2 to perform the filtering.


[1] Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 469-476.

Was this topic helpful?