2-D median filtering


B = medfilt2(A, [m n])
B = medfilt2(A)
gpuarrayB = medfilt2(gpuarrayA,___)
B = medfilt2(A,'indexed',___)
B = medfilt2(..., padopt)


B = medfilt2(A, [m n]) performs median filtering of the matrix A in two dimensions. Each output pixel contains the median value in the m-by-n neighborhood around the corresponding pixel in the input image. medfilt2 pads the image with 0s 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.

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.

This function supports code generation (see Tips).

B = medfilt2(A) performs median filtering of the matrix A using the default 3-by-3 neighborhood.

gpuarrayB = medfilt2(gpuarrayA,___) performs the 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™.

B = medfilt2(A,'indexed',___) performs median filtering of the indexed image A, padding with 0s if the class of A is uint8, or 1s if the class of A is double.

B = medfilt2(..., padopt) controls how the matrix boundaries are padded. padopt may be one of the text strings in the following table.

'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

Class Support

The input image A can be of class logical or numeric. The output image B is of the same class as A.

The input gpuArray image gpuarrayA can be of class logical or numeric. The output gpuArray image gpuarrayB is of the same class as gpuarrayA.

    Note   For information about performance considerations, see ordfilt2.


Remove salt and pepper noise from an image.

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

Remove salt and pepper noise from an image on a GPU.

I = gpuArray(imread('eight.tif'));
J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
figure, imshow(J), figure, imshow(K)

More About

expand all


  • This function supports the generation of C code using MATLAB® Coder™. The code generated for this function 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 Code Generation Using a Shared Library.

    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?