Documentation

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.

This function supports code generation (see Tips).

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.

Examples

expand all

Create a Filter and Apply It

Read a color image into the workspace and view it.

originalRGB = 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.

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.

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

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

Image with Replicate Boundary

Create a Filter and Apply it on a GPU

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

expand all

A — Image to be filterednonsparse, numeric array of any class and dimension

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

h — Multidimensional filterN-D array of doubles

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

Data Types: double

gpuArrayA — Image to be filteredgpuArray object

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 — Options that control the filtering operationcharacter string | numeric value

Options that control the filtering operation, specified as a character string 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

expand all

B — Filtered imagenumeric array the same size and class as input image

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

gpuarrayB — Filtered imagegpuArray

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

More About

expand all

Tips

  • 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 Code Generation Using a Shared Library.

    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.

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

See Also

| | | |

Was this topic helpful?