This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

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.


General sliding-neighborhood operations


B = nlfilter(A,[m n],fun)
B = nlfilter(A, 'indexed',___)



B = nlfilter(A,[m n],fun) applies the function fun to each m-by-n sliding block of the grayscale image A. fun is a function that accepts an m-by-n matrix as input and returns a scalar result.

B = nlfilter(A, 'indexed',___) processes A as an indexed image, padding with 1's if A is of class single or double and 0's if A is of class logical, uint8, or uint16.

nlfilter can take a long time to process large images. In some cases, the colfilt function can perform the same operation much faster.


collapse all

This example shows how apply a median filter to an image using nlfilter . 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)

Input Arguments

collapse all

Image to be filtered, specified as a numeric array of any class supported by 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.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Block to be processed, specified as a two-element vector of the form, [m n], where is m the number of rows and n is the number of columns.

Example: B = nlfilter(A,[3 3],fun);

Data Types: single | double | logical

Function to be executed, specified as a function handle. The function must accept an m-by-n matrix as input and returns a scalar result.

c = fun(x)

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.

Data Types: function_handle

Output Arguments

collapse all

Filtered image, returned as numeric array. The class of B depends on the class of the output from fun.

Introduced before R2006a

Was this topic helpful?