# Boundary tracing

Boundary tracing (also known as contour tracing) of a binary digital region can be thought of as a segmentation technique that identifies the boundary pixels of the digital region. Boundary tracing is an important first step in the analysis of that region.

## Algorithms

Algorithms used for boundary tracing:[1]

• Square Tracing algorithm[2]
• Moore-Neighbor Tracing algorithm
• Theo Pavlidis’ algorithm [4]
• an algorithm created by Dr Kovalevsky using modulo division and simple pixel lookups
• A generic approach using vector algebra for tracing of a boundary can be found at.[5]
• An extension of boundary tracing for segmentation of traced boundary into open and closed sub-section is described at.[6]

## Square Tracing Algorithm

The square tracing algorithm is simple, yet effective. Its behavior is completely based on whether one is on a black, or a white cell (assuming white cells are part of the shape). First, scan from the upper left to right and row by row. Upon entering your first white cell, the core of the algorithm starts. It consists mainly of two rules:

• If you are in a white cell, go left.
• If you are in a black cell, go right.

Keep in mind that it matters how you entered the current cell, so that left and right can be defined.

```public void GetBoundary(byte[,] image){
for(int j = 0; j < image.GetLength(1); j++)
for(int i = 0; int i < image.GetLength(0); i++)
if(image[i,j] == 255)               //Found first white pixel
SquareTrace(new Point(i, j));
}

public void SquareTrace(Point start){
HashSet<Point> boundaryPoints = new HashSet<Point>();  //Use a HashSet to prevent double occurrences
//We found at least one pixel

//The first pixel you encounter is a white one by definition, so we go left.
//Our initial direction was going from left to right, hence (1, 0)
Point nextStep = GoLeft(new Point(1, 0));
Point next = start + nextStep;
while(next != start){
//We found a black cell, so we go right and don't add this cell to our HashSet
if(image[next.x, next.y] == 0){
nextStep = GoRight(nextStep);
next = next + nextStep;
}
//Alternatively we found a white cell, we do add this to our HashSet
else{
nextStep = GoLeft(nextStep);
next = next + nextStep;
}
}
}

private point GoLeft(Point p) => new Point(p.y, -p.x);
private point GoRight(Point p) => new Point(-p.y, p.x);
```

## Dr Kovalevsky Algorithm

With an abstract cell complex representation of a digital image, the boundary point coordinates may be extracted from that digital image by following an algorithm created by Dr Kovalevsky using modulo division and simple pixel lookups.

This algorithm:

• assumes a single connected region within the binary image
• begins with an exhaustive search to locate the first foreground pixel by iterating over the columns and rows of the image.
For this example digital region the boundary point depicted in red is the starting point for the boundary tracing algorithm since it is the point / 0-cell for the first foreground pixel located along a row major order search.
```// 2D Point data structure
struct Point
{
int x; // col
int y; // row

P(int a , int b)
{
x = a;
y = b;
};
};

// Example Image Data
const int width  = 6;
const int height = 10;
int[height][width] image = { { 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 1 , 1 , 1 , 1 } ,
{ 0 , 0 , 1 , 1 , 1 , 1 } ,
{ 0 , 1 , 1 , 1 , 1 , 1 } ,
{ 1 , 1 , 1 , 1 , 1 , 0 } ,
{ 1 , 1 , 1 , 1 , 1 , 0 } ,
{ 0 , 0 , 1 , 1 , 1 , 0 } ,
{ 0 , 0 , 1 , 1 , 1 , 0 } ,
{ 0 , 0 , 1 , 1 , 1 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 } };

// Exhaustive row-major order search for first foreground pixel
Point startPoint;
for ( int row = 0 ; row < height ; row++ )
{
for ( int col = 0 ; col < width ; col++ )
{
if ( image[row][col] == 1 )
{
startPoint.x = col;
startPoint.y = row;
return;
}
}
}
```
The direction assignments given to various crack orientations to create a means of coding a set of boundary points.

Once that pixel is located the algorithm may begin by tracing the cracks of the region in a counterclockwise fashion following one of four possible directions at each step. These directions are represented by a crack code sequence: 0 (East), 1 (South), 2 (West), 3 (North)