Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
88 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Convolution
 Code
 Filters
 Edge Enhancement
 Edge Detection
 Tips and Tricks

 Printable version

 


Filters are not just made to enhance images, however. They also can be useful in edge enhancement or even edge detection. There are four edge enhancement algorithms:

  1. Shift and Difference Edge Enhancement Filter
  2. Laplacian Edge Enhancement Filter
  3. Gradient Directional Edge Enhancement Filter
  4. Directional Edge Enhancement Filter

There are two edge detection algorithms:

  1. Sobel's algorithm
  2. Prewitt's filters

I will walk through the uses of each.

Shift and Difference Edge Enhancement Filters are pretty simple. These filters subtract a spatially shifted copy of the image from the original image. If you don't understand what that means, just try the filters out and you'll see their intended effect.

In subtracting the images, the filter measures the slope of the brightness - where the image goes from black to white. In areas where there is little change (slope around 0), the resulting pixel will be black. However, in areas with great black to white change (larger slope), the resulting pixel will brighten. This works as edge enhancement for areas of great black to white change (larger slope) are most likely the edge of an object in the

Some Shift and Difference Edge Enhancement Filters

Horizontal

Vertical

Horizontal/Vertical
0 -1 0 0 0 0 -1 0 0
0 1 0 -1 1 0 0 1 0
0 0 0 0 0 0 0 0 0
1 90 1 100 1 110

image. Note that white to black transitions will make negative slopes - and with the code I've provided above, the pixel will be zeroed out. This can be easily remedied - just store the pixel as the absolute value of the negative number. Also note that in the above table, I've labeled it Some… Filters. Obviously, there are ways to extend the Horizontal/Vertical filter (put the -1 on any other corner), and the number does not need to be 1 altogether (try 2 or 20 for interesting results). Another thing to note is that the actual sum of all the coefficients is 0 - so the normalization constant is defaulted to 1. The above images may seem to be direct blue, but they are not - if you look carefully, the image "points" in the direction of the filter. However, much of the image's content is destroyed, so this filter is not often used for special effects.

The Laplacian Edge Enhancement Filter has the advantage of being omni-directional (it highlights edges from all directions). However, it has a great deal of math involved. If you don't understand the following, don't worry about it - just be glad it works.

Here's the math. The Laplacian of f(x,y) is defined as follows:

L[f(x,y)]=d2f/dx2+d2f/dy2

The larger variables are second partial derivatives with respect to x and y. We need to make this function discrete, so we need to approximate the second partial derivatives:

d2f/dx2=f(x+1)-2f(x)+f(x-1)
d2f/dy2=f(y+1)-2f(y)+f(y-1)

That's actually quite nice - for now we can approximate the entire Laplacian in this way:

L[f(x,y)]=-4f(x,y)+f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)

Look at that carefully. It looks an awful bit like a kernal. Don't see it? Well, this is how it translates:

0 1 0 à 0 -1 0
1 -4 1 -1 4 -1
0 1 0 0 -1 0
-1 1

The kernal on the left is the direct translation of the above calculation. Of course, we can't use a negative sum (it adds to zero, yet since the negative is in the center you should technically assume a negative 1), so we have to negate every coefficient, as shown in the kernal on the right.

Here is the accepted "actual" Laplacian kernals used in digital filtering:


LAPL1

LAPL2

LAPL3

Diagonal

Horizontal

Vertical
0 -1 0 -1 -1 -1 1 -2 1 -1 0 -1 0 -1 0 0 0 0
-1 4 -1 -1 8 -1 -2 4 -2 0 4 0 0 2 0 -1 2 -1
0 -1 0 -1 -1 -1 1 -2 1 -1 0 -1 0 -1 0 0 0 0
1 120 1 130 1 140 1 150 1 160 1 170

The Gradient Directional Edge Enhancement filter is used to highlight certain details in an image. A highlighted new pixel is made with positive slopes. The intensity of the new pixel is directly proportional to the pixel-by-pixel slope that the kernal experiences. Here they are:


East

South East

South

South West
-1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1
-1 -2 1 -1 -2 1 1 -2 1 1 -2 -1
-1 1 1 1 1 1 1 1 1 1 1 1
1 180 1 190 1 200 1 210

West

North West

North

North East
1 1 -1 1 1 1 1 1 1 1 1 1
1 -2 -1 1 -2 -1 1 -2 1 -1 -2 1
1 1 -1 1 -1 -1 -1 -1 -1 -1 -1 1
1 220 1 230 1 240 1 250

The Directional Edge Enhancement Filter for Embossing Effects, or "The Embossing Filter," is used to emboss an image and special effects:


East

South East

South

South West
-1 0 1 -1 -1 0 -1 -1 -1 0 -1 -1
-1 1 1 -1 1 1 0 1 0 1 1 -1
-1 0 1 0 1 1 1 1 1 1 1 0
1 260 1 270 1 280 1 290

West

North West

North

North East
1 0 -1 1 1 0 1 1 1 0 1 1
1 1 -1 1 1 -1 0 1 0 -1 1 1
1 0 -1 0 -1 -1 -1 -1 -1 -1 -1 0
1 300 1 310 1 320 1 330



Next : Edge Detection