## Documentation Center |

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

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 `0`s if the class of `A` is `uint8`,
or `1`s 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.

Value | Description |
---|---|

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

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

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)

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

Was this topic helpful?