Essentials of Mechatronics  ISBN: 0-471-72341-X- ©John Billingsley 2006 - published John Wiley & Sons, Inc

12/3/7.htm  Edge stiching

The previous examples sharpened the image, but did not find an edge in a logical sense.  It would be necessary to compare the sharpened image against a level to obtain a binary set
of edge points, and further searching would be needed to arrange these as a set of coordinates forming a locus.

The following program 'feels out' the edge with a sort of
"blind man's cane".
 
From some starting pixel, the routine moves one step forwards to 'feel' whether the brightness level has crossed a threshold.
If the threshold is crossed, a boundary point is noted while the program steps back and to the side to test the next point.

If the program steps forward without finding a level shift, the direction 'forward' is rotated 45 degrees.  Similarly if it steps backwards without crossing the threshold the direction
'forwards' is rotated 45 degrees in the opposite sense.
The result is that the program 'stitches' its way around the edge of a contrasting region, turning to follow any changes in edge direction.

From this fundamental principle, a number of additions are needed to make the routine work.

If the starting point is not near an edge, the routine would just go round in small circles.  The first modification is to count the number of steps since the last change of level and to advance an increasing distance before each 45 degree turn.  Now the search will make an expanding spiral.

When an edge is found, the 'turn distance' drops to 1 again.
 
The second modification enables the program to adapt the threshold to find subtle shades.  Two variables hold the lightest and the darkest values found so far; the threshold level is set midway between these levels.
 
To adapt to local changes, the 'lightest' value is reduced by a small amount at each step while the 'darkest' is increased.
They will ramp until they hit the values being found locally.

The example uses a higher resolution than the previous programs but searches very quickly. As each transition is found, it is tagged on the image by setting a point.  If the search arrives at a point already tagged then it is ended.

Run the simpler version at stitch1.bas or stitch1.htm

A second version is at stitch2.bas or stitch2.htm.