What’s a Chain Code?

Print anything with Printful



Chain codes are algorithms used to encode object outlines into a black and white image. The resulting code sequence can be used to redraw the object and compare it to other objects. Chain codes can be used in computer vision, image processing, and OCR programs. The algorithm involves finding the edge of an object and recording the location and direction of movement along the edge. The resulting code can be normalized and compared to other codes. There are more complex versions of the algorithm, including vector-based coding and RLE compression.

In computer graphics and image processing, a chain code is an algorithm used to encode the outlines of an object into a black and white or monochrome image. The resulting code sequence can describe how to draw the outline of the object relative to the image it is in, or it can be a collection of indications relating to the location on the outline where the algorithm started, essentially providing steps that can be followed to redraw the object. These codes can be normalized according to a formula and then compared to another chain code to determine if two objects are identical. A chain code can be used to isolate objects in a computer vision program or image segmentation in image processing, although more commonly it can be used in optical character recognition (OCR) programs.

While there are several established algorithms for a chain cipher, the basic concept is the same in each. First, the edge of an object is found, usually by moving pixel by pixel across a raster image. Once located, the location is recorded and surrounding edges are detected. Depending on whether the tracking algorithm moves clockwise or counterclockwise, the current position is moved in one direction or the other along the edge until it returns to its original position.

Each time the current position moves, a number is recorded in the chain code. This number usually indicates the direction that has been moved along the edge of the object. For example, if a chain code algorithm is following a straight edge from right to left, each time the edge is traced to the left, the numeric code for the left is added to the end of the code. The resulting code is a string of numbers where, if you followed the sequence from an arbitrary starting point and positioned a pixel at each pass, the outline of the object would be redrawed.

Once the string of numbers that make up the code is complete, various algorithms can be applied to it to help make comparisons with other chain code sequences. First, the number is normalized by rotating the starting number to determine the lowest integer value. This makes it possible to compare two objects that have the same outline, regardless of where on each object the coding started.

There are other, more complex versions of the chain cipher algorithm. These include vector-based coding where the outline of an object is described by a sequence of coordinates connected by lines, although this method can be lossy when used on finely detailed outlines. There is also a version of the algorithm that uses run-length encoding (RLE) to further compress the codes for exceptionally large or complex objects so they can be stored more efficiently.




Protect your devices with Threat Protection by NordVPN


Skip to content