Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

imfilter

N-D filtering of multidimensional images

Syntax

Description

example

B = imfilter(A,h) filters the multidimensional array A with the multidimensional filter h. The array A can be logical or a nonsparse numeric array of any class and dimension. The result B has the same size and class as A.

imfilter computes each element of the output, B, using double-precision floating point. If A is an integer or logical array, imfilter truncates output elements that exceed the range of the given type, and rounds fractional values.

example

gpuarrayB = imfilter(gpuArrayA,h) performs the operation on a GPU. gpuArrayA is a gpuArray that contains a logical or a nonsparse numeric array of any class and dimension. When used with a gpuArray, H must be a vector or 2-D matrix. This syntax requires the Parallel Computing Toolbox™.

example

___= imfilter(___,options,...) performs multidimensional filtering according to the specified options.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Examples

collapse all

Read a color image into the workspace and display it.

originalRGB = imread('peppers.png');
imshow(originalRGB)

Create a motion-blur filter using the fspecial function.

h = fspecial('motion', 50, 45);

Apply the filter to the original image to create an image with motion blur. Note that imfilter is more memory efficient than some other filtering functions in that it outputs an array of the same data type as the input image array. In this example, the output is an array of uint8.

filteredRGB = imfilter(originalRGB, h);
figure, imshow(filteredRGB)

Filter the image again, this time specifying the replicate boundary option.

boundaryReplicateRGB = imfilter(originalRGB, h, 'replicate');
figure, imshow(boundaryReplicateRGB)

Read a color image into the workspace as a gpuArray and view it.

originalRGB = gpuArray(imread('peppers.png'));
imshow(originalRGB)

Original Image

Create a filter, h, that can be used to approximate linear camera motion.

h = fspecial('motion', 50, 45);

Apply the filter, using imfilter, to the image originalRGB to create a new image, filteredRGB. The image is returned as a gpuArray.

filteredRGB = imfilter(originalRGB, h);
figure, imshow(filteredRGB)

Filtered Image

Note that imfilter is more memory efficient than some other filtering operations in that it outputs an array of the same data type as the input image array. In this example, the output is an array of uint8.

whos
Name               Size               Bytes  Class       Attributes

  filteredRGB      384x512x3              108  gpuArray              
  h                 37x37               10952  double                
  originalRGB      384x512x3              108  gpuArray                    

Try the filtering operation again, this time specifying the replicate boundary option.

boundaryReplicateRGB = imfilter(originalRGB, h, 'replicate');
figure, imshow(boundaryReplicateRGB)

Image with Replicate Boundary

Input Arguments

collapse all

Image to be filtered, specified as a nonsparse, numeric array of any class and dimension

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

Multidimensional filter, specified as an N-D array of doubles.

Data Types: double

Image to be filtered, specified as a gpuArray object. When used with a gpuArray, imfilter computes gpuarrayB, using either single- or double-precision floating point, depending on the data type of gpuArrayA. When gpuArrayA contains double-precision or uint32 values, imfilter uses double-precision values. For all other data types, imfilter uses single-precision. If gpuarrayA is an integer or logical array, imfilter truncates output elements that exceed the range of the given type, and rounds off fractional values.

Options that control the filtering operation, specified as a character vector or numeric value. The following table lists all supported options.

Boundary Options

Option

Description

Boundary Options

X

Input array values outside the bounds of the array are implicitly assumed to have the value X. When no boundary option is specified, the default is 0.

'symmetric'

Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border.

'replicate'

Input array values outside the bounds of the array are assumed to equal the nearest array border value.

'circular'

Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic.

Output Size

'same'

The output array is the same size as the input array. This is the default behavior when no output size options are specified.

'full'

The output array is the full filtered result, and so is larger than the input array.

Correlation and Convolution Options

'corr'

imfilter performs multidimensional filtering using correlation, which is the same way that filter2 performs filtering. When no correlation or convolution option is specified, imfilter uses correlation.

'conv'

imfilter performs multidimensional filtering using convolution.

Output Arguments

collapse all

Filtered image, returned as an array the same size and class as the input image.

Filtered image, returned as a gpuArray, the same size and class as gpuarrayA

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. 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 Understanding Code Generation with Image Processing Toolbox.

When generating code, the input image, A, must be 2-D or 3-D. The value of the input argument, options, must be a compile-time constant.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

Tips

  • This function may take advantage of hardware optimization for data types uint8, uint16, int16, single, and double to run faster.

See Also

| | | |

Introduced before R2006a

Was this topic helpful?