What are convolution filters?

Reducing or eliminating unwanted moiré patterns in scanned images can sometimes be accomplished through use of a discrete convolution operation (also called digital filtering).
A convolution filter determines a new value for each pixel in an image by computing some function of that pixel and its neighbors. Here is how it is done.

Imagine creating a template of, say, 5x5 pixels. Each of these 25 pixels has a specific brightness. (For simplification, this explanation refers only to gray scale, not color images. The same effects can be achieved in color by making one pass of the filter for each primary color.) Now, imagine passing this template across an image of 512x512 pixels, making sure the template is centered once over each and every one of the pixels in the image. While this template is stopped over each pixel, the brightness value of each pixel in the template is multiplied with the value of the underlying pixel in the image, thus creating 25 products. Now, add up these products and finally, store this result as a pixel in a new image at the same pixel coordinate as the old image where the template is centered.

After you finish performing this process for each pixel of the old image, you've performed a convolution of an image (the old image) with a kernel(the 5x5 template) to generate another image (the new image). That's the definition of a convolution operation.

This straightforward technique produces myriad results depending on what values you use for the kernel weights, as well as what size kernel you use. (3x3,5x5,7x7 and so forth). NOTE: The dimensions of the matrix must be odd, so there will be a central cell to represent the weight of the original value of the pixel for which we are computing a new value.


More information about specific convolution filters: Image Blurring, Sharpening an Image , Edge Detection, Embossing , Mean filter, Gausian Blur

Image Blurring


Blurring of a scanned image can be achieved with a pixel-spreading convolution kernel. For every new pixel this kernel creates, it spreads out pixel values by soliciting contributions from the surrounding pixels, while de-emphasizing the contribution of the center pixel.


pixel spreading convolution kernel
0.05 0.15 0.05
0.15 0.20 0.15
0.05 0.15 0.05


The critical features of this kernel are:
1. N = M = odd number.
2. Weight values are symmetrical around the center pixel.
3. All weights are positive values.
4. Weights remain constant or decrease in value as they move further from the center.


You can increase the degree of blurring by increasing the size of the kernel (N by M). The sum of all the weights in the kernel should equal 1.0 if you don't want to increase or decrease the brightness of the image.



Sharpening an Image


To sharpen an image, you want to perform pixel emphasis instead of pixel spreading. Think of pixel emphasis as a competition between each pixel of the original image and its neighboring pixels. Each pixel tries to emphasize itself while at the same time reducing the values of its neighbors.

The critical features of the pixel emphasis kernel are:
1. N = M = odd number.
2. Weight values are symmetrical around the central pixel.
3. Center weights are positive values..
4. Weights surrounding the center weights are negative.
5. Sum of weights is greater than zero.


pixel emphasis kernel
0.0 -1.0 0.0
-1.0 5.0 -1.0
0.0 -1.0 0.0

You can increase the degree of sharpening by decreasing the number of weights in the central, positive region of the kernel and by reducing the size of the kernel (N and M). (Note that the kernel above is already at its smallest limit.) As with blurring, the sum of all the weights in the kernel should equal 1.0 if you don't want to increase or decrease the brightness of the image.



Edge Detection


Edge detection is similar to image sharpening with the adrenaline turned on. The idea is basically the same except now you change the kernel so that the sum of the weights equals zero. By setting up the kernel appropriately, you can perform edge detection for vertical edges only, horizontal edges only, or both. The trick lies in setting up the kernel so that:

1. N = M = odd number.
2. Weight values are symmetrical around the center pixel (for general edge detection).
3. Center weights are positive values.
4. Weights surrounding the center weights are negative. 5. The sum of the weights equals zero.

Performing edge detection in one orientation only is a matter of removing the symmetry from the kernel, as illustrated below:

Edge Detection Kernels
General Vertical Horizontal
 0.0 -1.0  0.0   0.0   0.0   0.0   0.0 -1.0   0.0
-1.0   4.0 -1.0 -1.0   2.0 -1.0  0.0   2.0   0.0
  0.0 -1.0   0.0   0.0   0.0   0.0   0.0 -1.0   0.0


Embossing


Embossing an image is a two-part process: part one is a convolution and part two is the simple addition of a constant value to the convolution's result. The convolution does a specialized edge detection which will draw a bright line or a dark line depending on the direction of the brightness change (i.e. from bright to dark or dark to bright) while crossing perpendicular to the edge. Thus for every part of the image which is wrapped with an edge, the embossed image will have a bright edge on one side and a dark edge on the opposite side, giving the illusion of an elevated plateau. The kernel for this convolution is shown below. Its critical features are: 1. N = M = odd number.
2. Asymmetrical weight values about the center pixel (positive/negative canceling pairs).
3. Center weights are zero.
4. The sum of the weights is equal to zero.

embossing kernel (directional edge detection)
-1.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 -1.0

Mean filter

The mean filter is different from the other convolution filters listed here. Instead of weighting a pixel value by multiplying by constant values stored in a kernel (or template), the mean filter slides an empty window across the image. Each pixel is replaced by the average of all the values which fall within the window. The window is usually square but can be any shape. An example of mean filtering a single 3x3 window of values is shown below.

Mean Filtering Convolution
unfiltered pixel values new pixel value
5 3 6 5+3+6+2+1+9+8+4+7 = 45 * * *
2 1 9 45 / 9 = 5 * 5 *
8 4 7 * * *

Center value (previously 1) is replaced by the mean of all nine values (5).


Gausian Blur


A Gaussian Blur filter blurs a selection by an adjustable amount. Gaussian refers to the bell-shaped curve that is generated when the filter applies a weighted average to the pixel values. This filter adds low-frequency detail and can produce a hazy effect.


Back to top