|Image Processing Toolbox|
Filtering Using imfilter
Filtering of images, either by correlation or convolution, can be performed using the toolbox function
imfilter. This example filters the image in the file
blood1.tif with a 5-by-5 filter containing equal weights. Such a filter is often called an averaging filter.
imfilter function handles data types similar to the way the image arithmetic functions do, as described in Image Arithmetic Truncation Rules. The output image has the same data type, or numeric class, as the input image. The
imfilter function computes the value of each output pixel using double-precision, floating-point arithmetic. If the result exceeds the range of the data type, the
imfilter function truncates the result to that data type's allowed range. If it is an integer data type,
imfilter rounds fractional values.
Because of the truncation behavior, you may sometimes want to consider converting your image to a different data type before calling
imfilter. In this example, the output of
imfilter has negative values when the input is of class
Notice that the result has negative values. Now suppose
A was of class
uint8, instead of
Since the input to
imfilter is of class
uint8, the output also is of class
uint8, and so the negative values are truncated to
0. In such cases, it may be appropriate to convert the image to another type, such as a signed integer type,
double, before calling
Correlation and Convolution Options
imfilter function can perform filtering using either correlation or convolution. It uses correlation by default, because the filter design functions, described in Filter Design, and the
fspecial function, described in Using Predefined Filter Types, produce correlation kernels.
However, if you want to perform filtering using convolution instead, you can pass the string
'conv' as optional input argument to
imfilter. For example,
Boundary Padding Options
When computing an output pixel at the boundary of an image, a portion of the convolution or correlation kernel is usually off the edge of the image, as illustrated in the figure below.
Figure 7-3: When the Values of the Kernel Fall Outside the Image
imfilter function normally fills in these "off-the-edge" image pixels by assuming that they are
0. This is called zero-padding and is illustrated in the figure below.
Figure 7-4: Zero-Padding of Outside Pixels
When filtering an image, zero-padding can result in a dark band around the edge of the image, as shown in this example.
To eliminate the zero-padding artifacts around the edge of the image,
imfilter offers an alternative boundary padding method called border replication. In border replication, the value of any pixel outside the image is determined by replicating the value from the nearest border pixel. This is illustrated in the figure below.
Figure 7-5: Replicated Boundary Pixels
To filter using border replication, pass the additional optional argument
imfilter function supports other boundary padding options, such as
'symmetric'. See the reference page for
imfilter for details.
imfilter function can handle both multidimensional images and multidimensional filters. A convenient property of filtering is that filtering a three-dimensional image with a two-dimensional filter is equivalent to filtering each plane of the three-dimensional image individually with the same two-dimensional filter. This property makes it easy, for example, to filter each color plane of a truecolor image with the same filter.
Relationship to Other Filtering Functions
MATLAB has several two-dimensional and multidimensional filtering functions. The function
filter2 performs two-dimensional correlation;
conv2 performs two-dimensional convolution; and
convn performs multidimensional convolution. Each of these other filtering functions always converts the input to
double, and the output is always
double. Also, each of these other filtering functions always assumes the input is zero-padded, and they do not support other padding options.
In contrast, the
imfilter function does not convert input images to
imfilter function also offers a flexible set of boundary padding options, as described in Boundary Padding Options.
|Correlation||Using Predefined Filter Types|