We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What Is a Chain Code?

By Eugene P.
Updated: May 16, 2024

In computer graphics and image processing, a chain code is an algorithm used to encode the contours of an object in a black and white, or monochrome, image. The resulting sequence of codes can either describe how to draw the outline of the object relative to the image in which it is located, or it can be a collection of directions relative 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 for isolating objects in a computer vision program or image segmentation in image processing, though it more commonly may be used in optical character recognition (OCR) programs.

Although there are several established algorithms for a chain code, the basic concept is the same in each. First, the edge of an object is located, usually by moving pixel by pixel through a raster image. Once located, the position is recorded and the surrounding edges are detected. Depending on whether the detection algorithm will move clockwise or counterclockwise, the current location is moved in one direction or another along the edge until it returns to the original position.

Each time the current position moves, a number is recorded in the chain code. This number generally indicates the direction that was moved along the edge of the object. For example, if a chain code algorithm is following a straight edge from right to left, then each time the edge is traced to the left, the numerical code for left is added to the end of the code. The resulting code is a string of numbers in which, if the sequence is followed from an arbitrary starting point and a pixel placed at each step, the outline of the object would be redrawn.

Once the string of numbers that make up the code is completed, several different algorithms can be applied to it to help make comparisons against other chain code sequences. First, the number is normalized by rotating the starting number until the lowest integer value is determined. In this way, two objects that have the same outline can be compared, regardless of where on each object the encoding started.

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

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
https://www.easytechjunkie.com/what-is-a-chain-code.htm
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.