General sliding-neighborhood operations
B = nlfilter(A, [m n], fun)
B = nlfilter(A, 'indexed',...)
c = fun(x)
Parameterizing Functions, in the MATLAB Mathematics documentation, explains how to provide additional parameters to the function fun.
c is the output value for the center pixel in the m-by-n block x. nlfilter calls fun for each pixel in A. nlfilter zero-pads the m-by-n block at the edges, if necessary.
The input image A can be of any class supported by fun. The class of B depends on the class of the output from fun. When A is grayscale, it can be any numeric type or logical. When A is indexed, it can be logical, uint8, uint16, single, or double.
This example produces the same result as calling medfilt2 with a 3-by-3 neighborhood.
A = imread('cameraman.tif'); A = im2double(A); fun = @(x) median(x(:)); B = nlfilter(A,[3 3],fun); imshow(A), figure, imshow(B)