Finding Edges

Finding edges is an interesting and occasionally useful problem in image processing. It can be done effectively with surprisingly simple programs.

We can treat an edge as an area of an image where there is a lot of variation in the colors in nearby pixels.

For instance, you could measure the standard deviation of the color values among a pixel and its neighbors, and then set the pixels value based on it; a high standard deviation would indicate an edge, so you could set the pixel to be bright.

The example below uses a simpler scheme. In it, the maximum and minimum red, green, and blue color values for each pixel and its eight neighbors is found. Then the red, green and blue values for the pixel is set to be the difference of the maximum and minimum values. The result is a very simple and reasonably fast program that does a decent job.

The left image is the original, and the right is the result of processing it with the program listed below.

rem
rem This is an example of image manipulation software written in Basic
rem using the freeware Macintosh METAL basic interpreter.
rem 
rem This program produces an interpretation of a given image
rem by "finding edges".  For a given pixel, the max and min red,
rem green, and blue values of all adjacent nine (including the pixel
rem itself) pixels are found.  The color that pixel is then
rem assigned is the difference of the max and min values for each
rem of red, green, and blue.  The result is that uniform areas
rem of color are replaces by black (or otherwise dark colors), and
rem pixels at points of great variation (e.g. edges) are rendered
rem brightly.
rem
rem Metal seems to have a problem with very large images, so try this on
rem relatively small ones, say 600 x 500 or smaller.
rem
rem Matthew M. Conroy, 2001.  
rem Do whatever you want to with this code, especially improve it.
rem

rem user selects an image to be loaded and processed
file$ = open preview dialog$
get quicktime pict size file$,picW,picH

resize console 20,50,picW+20,picH+50
 load quicktime pict file$

virtu = init screen (0,0,picW+0,picH+0)
set screen to virtu
cls
copyrect 0,0,picW,picH,0,0,picW,picH,0,0,virtu

set screen to console
cls

rem loop through the entire image save the edge strips of pixels
for x=1 to picW-1
for y=1 to picH-1

rem initialize the max and min values
maxred=0: maxgreen=0: maxblue=0
minred=70000: mingreen=70000: minblue=70000

rem loop over the 9 pixels centered on (x,y)
for j = -1 to 1
for k = -1 to 1

set screen to virtu
get pixel x+j,y+k,red,green,blue

if (red > maxred) then maxred=red
if (blue > maxblue) then maxblue=blue
if (green > maxgreen) then maxgreen=green

if (red < minred) then minred=red
if (blue < minblue) then minblue=blue
if (green < mingreen) then mingreen=green

next k
next j

rem set the red, green and blue values for (x,y)
red = (maxred-minred)
blue = (maxblue-minblue)
green = (maxgreen-mingreen)

set screen to console
forecolor red,green,blue

plot x,y

next y
next x


back to image manipulation