Documentation Center

  • Trials
  • Product Updates

medfilt2

2-D median filtering

    Note:   The syntax medfilt2(A,[M N],(Mb Nb],...) has been removed.

Syntax

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

Description

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.

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.

ValueDescription
'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 (unless the 'indexed' syntax is used, in which case A cannot be of class uint16). 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.

Examples

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

Tips

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, suppose you call medfilt2 using 2-by-2 neighborhoods, and the input image is a uint8 array that includes this neighborhood.

1 5
4 8

medfilt2 returns an output value of 4 for this neighborhood, although the true median is 4.5.

Algorithms

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

References

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

See Also

| | |

Was this topic helpful?