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 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
performs the median filtering operation on a GPU, where each output pixel
contains the median value in the
gpuarrayB = medfilt2(
neighborhood around the corresponding pixel in the input
gpuArray. When working with
medfilt2 only supports square neighborhoods with
odd-length sides between 3 and 15. This syntax requires the Parallel
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
I = gpuArray(imread('eight.tif'));
Add noise to the image, then perform median filtering and display the result.
J = imnoise(I,'salt & pepper',0.02); K = medfilt2(J); figure, imshow(J), figure, imshow(K)
A— Input image
Input image, specified as a 2-D, real, nonsparse, numeric or logical matrix.
[m n]— Neighborhood size
Neighborhood size, specified as a two-element numeric vector,
n], of real positive integers.
When working with
gpuArrays, the neighborhood must be
square with odd-length sides between 3 and 15.
gpuarrayA— Input image when run on a GPU
Input image when run on a GPU, specified as a
padopt— Padding option
Padding option, specified as one of the following values:
|Padded with 0s. This is the default.|
|Symmetrically extended at the boundaries.|
|Padded with 1s, if the class of |
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
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
uint8 array is 4.5, but
the fractional part and returns 4.
1 5 4 8
On the CPU,
perform the filtering.
 Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 469-476.
Usage notes and limitations:
This function supports the generation of C code using MATLAB®
Note that if you choose the generic
MATLAB Host Computer target
platform, the function generates code that uses a precompiled, platform-specific
shared library. Use of a shared library preserves performance optimizations
but limits the target platforms for which code can be generated. For
more information, see Understand Code Generation with Image Processing Toolbox.
When generating code, the
must be a compile-time constant.