Linear Filtering of Images in MATLAB (Image Processing)

The Image Processing Toolbox includes a function imfilter that can be used to filter images. The first argument to this function is the image matrix, and the second argument is a matrix containing the filter coefficients. The command

tmp6-156_thumb

passes the image I through a 3×3 averaging filter. The ones function returns a constant matrix of ones. There is another similar MATLAB function zeros that returns the zero matrix – both of these functions are useful for preallocating space for matrices and initialization of matrix values. The . / operator specifies element-wise division, so the second argument above passes into imfilter a 3×3 matrix where each element is 1/9. The imfilter command also allows one to specify how to handle edge pixels with an optional third argument, and a variety of other options are available (refer to the online help for more information).

The Image Processing Toolbox function fspecial can be used to generate 2D kernels for use with imfilter. This function is especially helpful, as it knows about a slew of common filters, all of which can be tuned according to kernel size and additional filter-specific parameters. The first argument to fspecial is a string indicating which class of filter to generate (refer to the online help for a full list of supported filter types), the second argument is the size of the kernel, and the rest are optional arguments for tuning the filter4, the exact specifications of which depend on the type of filter. For example, the command


tmp6-157_thumb

returns the low-pass filter used in Figure 4-6 to implement the unsharp mask image sharpening procedure. The image filtering C programs that follow in sections 4.3.1-4.3.5 and 4.4.1-4.4.2 for the most part utilize two-dimensional arrays to store filter coefficients. To experiment with different filters, all that is required is to generate the coefficients via fspecial and copy the coefficients into the source code. For fixed-point programs, care must be taken to use coefficients in Q15 format. Conversion from floating-point to Q15 format is quite easy in MATLAB – the following snippet of code shows how to generate the fixed-point filter coefficients for a 5×5 Gausssian low-pass filter:

tmp6-158_thumb

If the Image Processing Toolbox is not available, conv2 can be used as a substitute for imfilter to perform 2D linear filtering of images. This function has a smaller set of arguments compared to imfilter and differs in some subtle ways. The first and second arguments are the image matrix and filter kernel. The third optional argument is a string indicating how much of the output to return5. The conv2 function always assumes zero padding of edge pixels, and by passing in the string ‘valid’, conv2 returns just the portion of the convolution that did not require zero-padding. The other difference to be aware of is the class of the returned type. If imfilter receives a matrix of type uint8, it will return back a uint8 matrix, in contrast to conv2 which always returns a double matrix. Thus, displaying the result of conv2 using image or imshow typically results in a completely white display if the image is not first converted back to the uint8 type, as shown below:

tmp6-159_thumb

In the above statement, the addition of 0.5 following the integer truncation via uint8 aids in the correct rounding. The matrix J can now be displayed in the appropriate scale using either of image or imshow. Alternatively, since both of the aforementioned image display functions expect image matrices of the double type to be scaled between [0,1.0], J could be divided by the maximum allowable pixel value for this data type (255 for uint8) prior to display:

tmp6-160_thumb

Next post:

Previous post: