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.


2-D median filtering


B = medfilt2(A)
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.

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

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.


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

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

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

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

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

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

Padding option, specified as one of the following values:

'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.

Data Types: char

Output Arguments

collapse all

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

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


  • 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.

  • 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.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?