Note: This page has been translated by MathWorks. Please click here

To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materials 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)`

```
gpuarrayB = medfilt2(gpuarrayA,[m
n])
```

performs median filtering of the image `B`

= medfilt2(`A`

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

performs the median filtering operation on a GPU, where each output pixel
contains the median value in the `gpuarrayB`

= medfilt2(`gpuarrayA`

,```
[m
n]
```

)`m`

-by-`n`

neighborhood around the corresponding pixel in the input
`gpuArray`

. When working with `gpuArray`

s,
`medfilt2`

only supports square neighborhoods with
odd-length sides between 3 and 15. This syntax requires the Parallel
Computing Toolbox.

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.

Was this topic helpful?