# How to apply a filter kernel

A filter that can be used for digital signals like audio, video or image processing can be defined using a matrix ("kernel") that weights the surrounding area (this is a description I read in lecture notes from someone else).

The kernel $$\left(\begin{array}{ccc} 1 & 1 & 1 \\ 1 & 1 & 1\\ 1 & 1 & 1\end{array}\right)$$ defines an erode filter.

Could you please tell me how this kernel is applied to e.g. an image (and therefore a 2D field of pixels)? Thank you in advance!

## migrated from math.stackexchange.comSep 22 '11 at 1:04

This question came from our site for people studying math at any level and professionals in related fields.

• The term erode "filter" is a little bit of a misnomer here as it's a highly non-linear operation. Filter in the more stringent sense refers to a linear time invariant operation. Convolution with a kernel is a filter, applying the erode process isn't – Hilmar Sep 22 '11 at 12:57
• IMO, the answer is one line: convolution. Research convolution and the math equation and implementation of convolution and that is done. (in this case you would do 2d convolution). – Trevor Boyd Smith Sep 22 '11 at 14:47
• I think the real question should be "how do you filter something with an FIR filter?" or "how does convolution work?" or "how do you implement convolution?". – Trevor Boyd Smith Sep 22 '11 at 14:48
• This is not a kernel, but a structuring element. Hence, it is applied completely differently. See @kolentebert's answer below. Simply speaking, it is a shape that you overlay on an image to extract local maxima/minima. – sansuiso May 1 '13 at 7:40

## 2 Answers

Your erode filter is incorrect. The link you gave says it is supposed to find the maximum of the adjacent pixels, and your array does not do this. Instead, it gives the sum of the adjacent pixels. If you normalize the result (since all these additions will increase the brightness), then you will find that your filter simply evenly averages the adjacent pixels.

What you would generally do is take your image and convolve 3x3 blocks of it (or however big the filter is) with your filter. You can also do these in parallel, for speed.

In image processing, to apply such a filter you would iterate over all pixels of the input image and in each step place the filter mask over the image so that its center is located at your current pixel. You then "evaluate" the pixels in the neighborhood that are covered by the mask in some way and write the result back to the current pixel.

For a normal convolution, you multiply each element of the filter with the value of the corresponding pixel, add up the results and write the sum to the current pixel.

Erosion is a morphological operation, and you would implement it (on a binary image) by checking whether all "1" values of your mask lie over "1" pixels in the image. If so, you write a "1" to the current pixel (otherwise a "0").

In both cases, make sure to always read your input pixels from an umodified version of the image (rather than modifying the image in-place).