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 the points within [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.
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(..., 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|
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)
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.
On the CPU, medfilt2 uses ordfilt2 to perform the filtering.
 Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 469-476.