Recent from talks
Contribute something
Nothing was collected or created yet.
Chain code
View on WikipediaA chain code is a lossless compression based image segmentation method for binary images based upon tracing image contours. The basic principle of chain coding, like other contour codings, is to separately encode each connected component, or "blob", in the image.
For each such region, a point on the boundary is selected and its coordinates are transmitted. The encoder then moves along the boundary of the region and, at each step, transmits a symbol representing the direction of this movement.
This continues until the encoder returns to the starting position, at which point the blob has been completely described, and encoding continues with the next blob in the image.
This encoding method is particularly effective for images consisting of a reasonably small number of large connected components.
Variations
[edit]Some popular chain codes include:
- the Freeman Chain Code of Eight Directions[1] (FCCE)
- Directional Freeman Chain Code of Eight Directions[2] (DFCCE)
- Vertex Chain Code[3] (VCC)
- Three OrThogonal symbol chain code[4] (3OT)
- Unsigned Manhattan Chain Code[5] (UMCC)
- Ant Colonies Chain Code[6] (ACCC)
- Predator-Prey System Chain Code[7][8] (PPSCC)
- Beaver Territories Chain Code[9] (BTCC)
- Biological Reproduction Chain Code[10] (BRCC)
- Agent-Based Modeling Chain Code[11] (ABMCC)
In particular, FCCE, VCC, 3OT and DFCCE can be transformed from one to another[12]

A related blob encoding method is crack code.[13] Algorithms exist to convert between chain code, crack code, and run-length encoding.
A new trend of chain codes involve the utilization of biological behaviors. This started by the work of Mouring et al.[6] who developed an algorithm that takes advantage of the pheromone of ants to track image information. An ant releases a pheromone when they find a piece of food. Other ants use the pheromone to track the food. In their algorithm, an image is transferred into a virtual environment that consists of food and paths according to the distribution of the pixels in the original image. Then, ants are distributed and their job is to move around while releasing pheromone when they encounter food items. This helps other ants identify information, and therefore, encode information.
In use
[edit]Recently, the combination of move-to-front transform and adaptive run-length encoding accomplished efficient compression of the popular chain codes.[14] Chain codes also can be used to obtain high levels of compression for image documents, outperforming standards such as DjVu and JBIG2.[11][10][9][8][7][6][15]
See also
[edit]References
[edit]- ^ Freeman, Herbert (June 1961). "On the Encoding of Arbitrary Geometric Configurations". IRE Transactions on Electronic Computers. EC-10 (2): 260–268. doi:10.1109/TEC.1961.5219197.
- ^ Liu, Yong Kui; Žalik, Borut (April 2005). "An efficient chain code with Huffman coding". Pattern Recognition. 38 (4): 553–557. Bibcode:2005PatRe..38..553K. doi:10.1016/j.patcog.2004.08.017.
- ^ Bribiesca, Ernesto (February 1999). "A new chain code". Pattern Recognition. 32 (2): 235–251. Bibcode:1999PatRe..32..235B. doi:10.1016/S0031-3203(98)00132-0.
- ^ Sanchez-Cruz, Hermilo; Rodríguez-Dagnino, Ramón M. (September 2005). "Compressing bilevel images by means of a three-bit chain code". Optical Engineering. 44 (9). 097004. Bibcode:2005OptEn..44i7004S. doi:10.1117/1.2052793.
- ^ Žalik, Borut; Mongus, Domen; Liu, Yong-Kui; Lukač, Niko (July 2016). "Unsigned Manhattan chain code". Journal of Visual Communication and Image Representation. 38: 186–194. doi:10.1016/j.jvcir.2016.03.001.
- ^ a b c Mouring, Matthew; Dhou, Khaldoon; Hadzikadic, Mirsad (2018). "A Novel Algorithm for Bi-Level Image Coding and Lossless Compression based on Virtual Ant Colonies". Proceedings of the 3rd International Conference on Complexity, Future Information Systems and Risk. COMPLEXIS 2018. Vol. 1. pp. 72–78. doi:10.5220/0006688400720078. Retrieved 2022-07-06.
- ^ a b Dhou, Khaldoon (January 2020). "A new chain coding mechanism for compression stimulated by a virtual environment of a predator–prey ecosystem". Future Generation Computer Systems. 102: 650–669. doi:10.1016/j.future.2019.08.021. S2CID 202783274.
- ^ a b Dhou, Khaldoon (2018). A Novel Agent-Based Modeling Approach for Image Coding and Lossless Compression Based on the Wolf-Sheep Predation Model. ICCS 2018. Computational Science – ICCS 2018. LNCS. Vol. 10861. pp. 117–128. doi:10.1007/978-3-319-93701-4_9.
- ^ a b Dhou, Khaldoon; Cruzen, Christopher (May 2021). "A highly efficient chain code for compression using an agent-based modeling simulation of territories in biological beavers". Future Generation Computer Systems. 118: 1–13. doi:10.1016/j.future.2020.12.016. S2CID 232023010.
- ^ a b Dhou, Khaldoon; Cruzen, Christopher (December 2019). "An Innovative Chain Coding Technique for Compression Based on the Concept of Biological Reproduction: An Agent-Based Modeling Approach". IEEE Internet of Things Journal. 6 (6): 9308–9315. doi:10.1109/JIOT.2019.2912984. S2CID 150025529.
- ^ a b Dhou, Khaldoon (June 2019). "An innovative design of a hybrid chain coding algorithm for bi-level image compression using an agent-based modeling approach". Applied Soft Computing. 79: 94–110. doi:10.1016/j.asoc.2019.03.024. S2CID 126831246.
- ^ Sanchez-Cruz, Hermilo; Lopez-Valdez, Hiram H. (January 2014). "Equivalence of chain codes". Journal of Electronic Imaging. 23 (1). 013031. Bibcode:2014JEI....23a3031S. doi:10.1117/1.JEI.23.1.013031. S2CID 41897871.
- ^ Rosenfeld, Azriel; Kak, Avinash C. (1982). "Chapter 11 - Representation". Digital Picture Processing. Vol. 2 (2nd ed.). Academic Press. p. 220. Bibcode:1982dpp..book.....R. doi:10.1016/B978-0-12-597302-1.50010-4. ISBN 0-12-597302-0. ISBN 0-12-597301-2, 0-12-597302-0
- ^ Žalik, Borut; Lukač, Niko (January 2014). "Chain code lossless compression using move-to-front transform and adaptive run-length encoding". Signal Processing: Image Communication. 29 (1): 96–106. doi:10.1016/j.image.2013.09.002.
- ^ Rodríguez-Díaz, Mario A.; Sánchez-Cruz, Hermilo (July 2014). "Refined fixed double pass binary object classification for document image compression". Digital Signal Processing. 30: 114–130. Bibcode:2014DSP....30..114R. doi:10.1016/j.dsp.2014.03.007.
Chain code
View on GrokipediaFundamentals
Definition
Chain code is a lossless compression and boundary representation technique used in image processing to encode the contours of connected components, often referred to as blobs, in binary images. It achieves this by tracing the perimeter of an object pixel by pixel, converting the boundary into a compact sequence of directional moves rather than storing the full pixel coordinates. This method was originally proposed by Herbert Freeman in 1961 as a way to encode arbitrary geometric configurations efficiently.[5] The core principle of chain code involves representing a shape's boundary as a sequence of unit-length steps along specified directions, typically employing either 4-connectivity (horizontal and vertical moves only) or 8-connectivity (including diagonals). In the 8-connectivity system, directions are encoded as integers from 0 to 7, where 0 denotes a move to the right (east), 1 to the upper-right (northeast), 2 upward (north), and so on, progressing counterclockwise in 45-degree increments. This encoding allows for a succinct description of the boundary path, preserving all topological and geometric details without loss of information.[6][2] To generate a chain code, an arbitrary boundary pixel is selected as the starting point, with its coordinates (x, y) explicitly transmitted to enable reconstruction. The tracing proceeds along the contour until it returns to this starting pixel, forming a closed loop that fully delineates the connected component. This approach is particularly suitable for binary images featuring a small number of large connected components, as it minimizes redundancy and storage requirements for prominent object boundaries while being less efficient for scenes with numerous tiny elements.[2]Boundary Representation
Chain codes represent the boundary of an object in a binary image by encoding the sequence of directional moves along its perimeter, effectively capturing the geometric structure through a compact string of integers corresponding to unit steps in predefined directions. This method, introduced by Herbert Freeman, traces the contour starting from a designated point and follows the boundary in a consistent direction, such as clockwise, to ensure a closed loop representation without gaps or overlaps.[7] The boundary tracing process typically employs algorithms like Moore's neighborhood algorithm, which begins at the leftmost pixel of the topmost row of the object and examines neighboring pixels in a clockwise manner to identify and follow the edge pixels. This ensures all boundary points are visited exactly once before returning to the starting point, forming a complete contour description. For connectivity, chain codes support two primary models: 4-connected, which uses four cardinal directions (0 for east/right, 1 for south/down, 2 for west/left, 3 for north/up), restricting moves to horizontal and vertical steps; and 8-connected, which expands to eight directions (0-7, starting from east and proceeding clockwise to include diagonals), allowing for more fluid approximations of curved boundaries. The choice of connectivity affects the smoothness and length of the code, with 8-connected often yielding shorter sequences for diagonal-heavy contours.[8][9] A representative example in 4-connectivity is a simple square boundary traversed clockwise, starting from the top-left corner: the sequence "000111222333" encodes three right moves (0), three down moves (1), three left moves (2), and three up moves (3) for a 4x4 pixel square, precisely outlining the perimeter. In 8-connectivity, the same square would use only cardinal directions but could incorporate diagonals (e.g., 1 for southeast) if the boundary includes slanted edges in more complex shapes.[8] One key advantage of chain codes is their compact storage of contour information, requiring minimal space—often just one byte per direction—while preserving all boundary pixels losslessly, enabling exact reconstruction of the original shape by interpreting the sequence as a series of vector steps from the starting point. For instance, decoding "000111222333" repositions a cursor successively right three times, down three times, left three times, and up three times, yielding the square's exact pixel outline without interpolation errors. However, chain codes have limitations, including sensitivity to the choice of starting point, which can alter the sequence despite representing the same shape, and vulnerability to rotation, as shifting the orientation changes the directional codes; they are also not inherently scale-invariant, requiring additional processing for resized images.[7][9]Historical Development
Origins in Image Processing
Chain codes originated in the field of computer vision during the early era of digital image processing, where researchers sought efficient methods to represent and analyze visual data from scanned or digitized sources. Herbert Freeman introduced the concept in his seminal 1961 paper, proposing a technique for encoding arbitrary geometric configurations to enable compact storage, transmission, and machine recognition of shapes.[5] This approach addressed the challenges of handling large volumes of image data on early computers, focusing on boundary descriptions to support pattern recognition tasks.[10] Freeman's prototype, known as the Freeman Chain Code of Eight Directions (FCCE), utilized an 8-connected path that quantized boundary directions into eight discrete orientations spaced at 45-degree increments, allowing for a concise sequence of directional symbols to trace object contours.[5] The method formalized the encoding process by following contours in a counterclockwise manner, preserving essential shape information while reducing data redundancy compared to pixel-by-pixel representations.[10] This innovation built upon prior informal ideas in contour-following algorithms but provided a structured, numerical framework for digital curve representation.[10] During the 1960s and 1970s, chain codes saw early adoption in research on topological properties of digital images and the development of shape descriptors, enabling applications in automated image analysis and recognition systems. Freeman's work laid the groundwork for subsequent variations that extended the basic encoding scheme to handle more complex boundary features.[10]Key Advancements
In 1965, Freeman and Jerry Feder co-developed correlation techniques for chain codes, improving shape matching efficiency by analyzing directional correlations in boundary sequences.[2] In the 1970s, Seymour Papert simplified encoding to binary turn codes, reducing complexity for basic direction changes.[2] During the 1970s and 1980s, differential chain codes (DCC) emerged as a significant improvement over early Freeman codes, encoding relative direction changes between successive boundary segments to mitigate sensitivity to rotations and translations.[11] This approach, pioneered by researchers at Delft University, reduced storage requirements by exploiting correlations in boundary directions, achieving higher compression efficiency for line drawings compared to absolute direction encodings. Building on Freeman's foundational work from 1961, the Vertex Chain Code (VCC) was developed in 1999 by Ernesto Bribiesca, emphasizing representations based on corner points or vertices rather than every pixel along the boundary, which enhanced compactness and invariance properties.[12] VCC uses a minimal set of symbols to describe vertex connections, allowing for more efficient storage of complex shapes while maintaining topological accuracy.[12] During the 1990s and 2000s, chain codes saw integrations with complementary encodings such as crack codes, which trace boundaries along pixel cracks, and run-length encoding (RLE), enabling hybrid representations for improved compression in binary images.[13] In 2005, Huffman-coded chain codes were proposed as a compressed variant to further optimize storage. Around 2006, histogram-based extensions like chain code histograms (CCH) and coherence vectors (CCCV) were developed, facilitating statistical shape analysis across diverse grid structures. The 3-Orthogonal Chain Code (3OT), introduced in 2005, further advanced this by using only three orthogonal symbols to represent boundaries, facilitating simpler computations for properties like the Euler characteristic and outperforming prior codes in storage efficiency for rasterized shapes.[14] The 2010s introduced biologically inspired variants, drawing from natural systems for more robust boundary tracing. The Ant Colonies Chain Code (ACCC) of 2018 employs swarm optimization principles, simulating ant pheromone trails to achieve resilient contour extraction in noisy environments.[15] Similarly, the Predator-Prey System Chain Code (PPSCC), from 2019, models dynamic interactions akin to ecological predation for adaptive boundary detection, enhancing performance in evolving or irregular shapes.[16] In the 2020s, chain codes have been increasingly integrated into AI-driven image segmentation pipelines, particularly for post-processing contours extracted via deep learning models, as explored in agent-based simulations for bi-level image compression.[17] Works like Dhou's 2019 analysis highlight future applications in hybrid systems combining chain codes with neural networks for precise shape representation in medical and computational imaging.[18] As of 2025, advancements include noise-robust techniques such as direct noise injection into Freeman chain codes to model imperfections in real-world data.[4] Algorithms for interconvertibility among major chain code formats, including Freeman Chain Code Extended (FCCE), VCC, 3OT, and Differential Freeman Chain Code Extended (DFCCE), were established to enable seamless transformations and unified processing across variants.[19]Types and Variations
Freeman Chain Code
The Freeman Chain Code (FCC), introduced by Herbert Freeman in 1961, represents the boundary of a digital object as a sequence of integer codes corresponding to eight possible move directions in an 8-connected grid.[5] This original formulation, often denoted as the Freeman Chain Code of Eight Directions (FCCE), numbers the directions from 0 to 7, with each code indicating a unit move of 45 degrees: 0 for east, 1 for northeast, 2 for north, 3 for northwest, 4 for west, 5 for southwest, 6 for south, and 7 for southeast, measured counter-clockwise from the positive x-axis. The angle for direction is given by where . In the first-order FCCE, the sequence records absolute directions of each boundary step, starting from an arbitrary point and tracing the contour, typically clockwise or counter-clockwise.[2] A second-order variant computes differences between consecutive absolute directions, reducing sensitivity to starting orientation but requiring additional normalization for full rotation invariance, such as cyclically shifting the sequence to its lexicographically smallest form.[2] [20] For example, a closed boundary approximating a circle might yield a sequence like "012345670", forming an octagonal path that closes back to the starting direction.[2] The FCCE's advantages include its simplicity, requiring only 3 bits per code for storage efficiency, and its ability to accurately capture diagonal moves inherent to 8-connectivity, enabling compact representation of irregular boundaries without loss of topological connectivity. [2] Introduced in Freeman's seminal work, it remains the baseline for evaluating subsequent chain code variants due to its foundational role and widespread adoption in shape encoding tasks.[5]Differential and Vertex Variants
Modifications to the foundational Freeman chain code have introduced variants that mitigate limitations such as sensitivity to rotation and representational redundancy, particularly for smooth or polygonal boundaries. These include the differential chain code, which emphasizes relative direction changes, and vertex-based approaches that focus on corner points rather than every boundary step. The differential chain code (DCC) encodes the difference in direction between consecutive steps along the boundary, defined as , where represents the absolute direction code at step . This relative encoding produces compact sequences for smooth contours, as straight lines yield repeated 0s, while gentle curves feature mostly 0s interspersed with small values like ±1 or ±2. For instance, a horizontal line segment results in an all-zeros sequence, significantly reducing storage compared to absolute codes. DCC was analyzed for its efficiency in line drawing representation, demonstrating reduced data volume without loss of information.[21] The vertex chain code (VCC), introduced by Bribiesca, represents boundaries by traversing the vertices of regular cells composing the shape, recording the number of cell vertices in contact with the contour at each step—typically using codes 1, 2, or 3 (with 4 indicating closure). This approach only updates the code at significant vertices or corners, making it suitable for polygonal approximations where straight segments are encoded implicitly through vertex positions. VCC achieves invariance to translation and rotation, and it can be normalized for starting point independence, enhancing its utility for shape recognition. By focusing on vertices, it offers efficiency gains in boundaries with long straight segments, avoiding redundant codes for collinear points.[22] The 3-orthogonal chain code (3OT) restricts movements to three orthogonal directions (forward, left, right relative to the current orientation), using a minimal set of three symbols for encoding. This simplification facilitates hardware implementation, as it reduces the complexity of direction tracking while maintaining lossless boundary description. Proposed for applications like corner detection and Euler characteristic computation, 3OT proves effective for binary images with orthogonal features, outperforming four-direction codes in compression for certain contours.[23] These variants provide targeted advantages: DCC excels in compactness for smooth, continuous curves by leveraging directional stability; VCC enhances efficiency for polygonal or corner-dominated shapes through selective vertex recording; and 3OT prioritizes implementation simplicity in resource-constrained environments. While the foundational Freeman code serves as a basis for these developments, the variants address specific representational challenges without altering core boundary-tracing principles.[24]Algorithms and Implementation
Encoding Process
The encoding process for generating a chain code from a binary image boundary involves systematically tracing the contour of a connected component while recording directional changes. This procedure typically employs 8-connectivity, as introduced in Freeman's framework for representing object boundaries. The process begins with identifying a starting pixel on the boundary and proceeds through a sequential traversal until the boundary is fully enclosed, ensuring a lossless representation of the shape. To initiate encoding, the starting pixel is selected as the leftmost (or uppermost leftmost) boundary point in the image, often the first foreground pixel encountered in a raster scan from the top-left corner; its coordinates (x, y) are output as the initial position.[5][8] Next, the initial direction is determined by examining the eight possible neighbors of the starting pixel in a predefined order, such as clockwise from an assumed entry direction (e.g., from the west neighbor, treated as background), to identify the first adjacent boundary pixel.[8][1] The traversal follows a consistent search pattern using the Moore neighborhood, which checks the eight surrounding pixels in a clockwise manner starting from the previous entry direction to locate the next boundary pixel. At each step, the direction from the current pixel to the next is encoded as a value from 0 to 7, corresponding to the eight possible unit moves (0 for east, 1 for northeast, up to 7 for southeast). This code is appended to the sequence, and the process repeats, updating the current position and entry direction for the next check.[5][8] The traversal continues until the starting pixel is revisited and the subsequent pixel matches the initial next pixel, confirming closure of the boundary.[8] The following pseudocode outlines the core algorithm for 8-connected boundary tracing and chain code generation:Initialize: Find starting boundary [pixel](/page/Pixel) b0 = (x0, y0); set entry direction c0 (e.g., west neighbor as background).
Set current [pixel](/page/Pixel) b = b0; set previous entry c = c0; initialize empty chain code sequence S.
While true:
Examine [Moore neighborhood](/page/Moore_neighborhood) of b clockwise starting from c: check [pixels](/page/Pixel) n1 to n8.
Find first foreground (boundary) [pixel](/page/Pixel) nk among n1 to n8.
If nk == b0 and b == starting next [pixel](/page/Pixel) b1, break (closure detected).
Append direction code d (0-7) from b to nk to S.
Set c = the [pixel](/page/Pixel) preceding nk in the neighborhood check (new entry for next step).
Set b = nk.
Output: Coordinates (x0, y0) and sequence S.
Initialize: Find starting boundary [pixel](/page/Pixel) b0 = (x0, y0); set entry direction c0 (e.g., west neighbor as background).
Set current [pixel](/page/Pixel) b = b0; set previous entry c = c0; initialize empty chain code sequence S.
While true:
Examine [Moore neighborhood](/page/Moore_neighborhood) of b clockwise starting from c: check [pixels](/page/Pixel) n1 to n8.
Find first foreground (boundary) [pixel](/page/Pixel) nk among n1 to n8.
If nk == b0 and b == starting next [pixel](/page/Pixel) b1, break (closure detected).
Append direction code d (0-7) from b to nk to S.
Set c = the [pixel](/page/Pixel) preceding nk in the neighborhood check (new entry for next step).
Set b = nk.
Output: Coordinates (x0, y0) and sequence S.
Normalization Techniques
Chain code normalization techniques render the directional sequence invariant to translation, rotation, and starting point, enabling reliable comparisons in shape analysis.[1] Translation invariance arises naturally from the focus on relative directions in the sequence, with absolute starting coordinates managed independently during encoding.[25] Starting point normalization treats the chain code as a circular sequence and applies a cyclic shift to begin with the lowest code, such as the first 0 in Freeman chain code equivalents, yielding the lexicographically smallest representation.[9] Rotation invariance for first-order codes is obtained by subtracting an offset equal to the minimum direction value from all codes modulo 8, aligning the sequence to start from 0; differential chain codes, based on direction changes, are inherently robust as uniform rotations preserve differences.[25] The standard algorithm proceeds as follows: compute the initial chain code sequence, identify the minimum code position, perform a cyclic shift to relocate it to the start, and apply the rotation adjustment by subtracting the minimum direction modulo 8 for absolute codes. For instance, an original sequence "0642" is cyclically redefined to "20642", then transformed via first differences to "6666", resulting in the normalized shape number "66666".[20] Scale invariance, when required, involves resampling the boundary contour to a uniform length prior to chain code generation, though this extension is uncommon for traditional binary chain codes.[26] These methods underpin effective shape matching in recognition tasks by ensuring canonical forms across transformations.[1]Applications
Image Compression
Chain codes facilitate the compression of binary image contours by representing the boundary path as a compact sequence of directional instructions, rather than storing the coordinates of each boundary pixel individually. In Freeman's chain code, for instance, an 8-connectivity variant encodes each step along the contour using one of eight possible directions, requiring only 3 bits per step (log₂(8)), which contrasts sharply with the bit cost of listing all N boundary pixels explicitly. This mechanism achieves substantial data reduction for smooth or structured boundaries, as the sequence length remains proportional to the number of steps, typically much shorter than exhaustive pixel lists.[27] To further enhance compression ratios, chain codes are often combined with preprocessing transforms and entropy coding techniques. The move-to-front (MTF) transform reorganizes the sequence to prioritize frequent directions, improving subsequent encoding efficiency, while adaptive arithmetic coding exploits statistical redundancies in the transformed data for near-optimal bit allocation. Additionally, run-length encoding (RLE) can be applied to handle repeated directions, such as long straight segments in contours, yielding further savings in repetitive patterns. These enhancements form a pipeline, as demonstrated in methods integrating Burrows-Wheeler transform (BWT) with MTF and arithmetic coding, which achieve average code lengths below 2 bits per symbol on benchmark datasets. Performance evaluations highlight chain code-based methods' superiority for contour-heavy images, such as document scans, where they outperform standards like DjVu and JBIG2. A 2014 study using 3-orthogonal-tilt (3OT) chain codes with pattern matching reported 27% better compression than JBIG2 at 200 dpi and 65% at 600 dpi, alongside 6-35% gains over DjVu's JB2 layer, particularly on binary objects with clear edges. Similarly, 2019 surveys of lossless compression techniques, including those combining chain codes with LZ77, highlight improved efficiency for scanned documents and line art.[28][29] Despite these advantages, chain codes exhibit limitations in handling noisy or highly irregular boundaries, where extraneous direction changes inflate the sequence length and degrade compression ratios, as the method assumes clean, connected contours traceable via boundary following algorithms.Shape Analysis and Recognition
Chain codes facilitate feature extraction from object boundaries in binary images by encoding contours as sequences of directional symbols, enabling the computation of geometric and statistical properties. The perimeter of a shape is directly derived from the chain code as the length of the sequence, adjusted for directionality: for 4-directional codes, equals the number of codes, while for 8-directional codes, it is the count of even directions plus twice the odd directions to account for diagonal steps.[30] Compactness, a measure of shape circularity, is calculated as , where is the enclosed area, providing insight into elongation or irregularity.[30] Direction histograms, generated by tallying the frequency of each directional code in the sequence, capture angular distribution and orientation trends along the contour. In shape recognition, normalized chain codes serve as compact representations for matching against templates or databases. String alignment techniques, such as modified Needleman-Wunsch algorithms, compare code sequences by allowing gaps and substitutions to quantify similarity, accommodating variations in contour length or noise.[31] Fourier descriptors can be derived from chain codes by treating the sequence as a parametric curve and applying discrete Fourier transforms to its coordinates, yielding rotation- and scale-invariant coefficients for classification.[32][33] Chain codes have been applied in handwritten character recognition since the 1970s, where boundary encoding distinguishes scripts by contour patterns, as in early optical character recognition systems using direction histograms from chain codes.[34] In medical imaging, they trace cell contours for segmentation and analysis, such as in edge detection algorithms that vectorize boundaries from microscopic images.[35] For geographic information systems, chain codes represent polygon boundaries, supporting simplification by reducing code length while preserving essential topology.[36] For example, in optical character recognition, chain codes differentiate an 'O'—characterized by a smooth, uniform sequence of directions—from a 'D', which features longer straight segments interrupted by curves.[34] Since the 2010s, chain codes have integrated with deep learning as preprocessing steps, where embeddings of code sequences feed into convolutional neural networks for enhanced shape classification, such as in numeral recognition systems achieving high accuracy through combined Freeman codes and neural classifiers.[37] Chain codes also enable topological analysis, computing properties like genus via the Euler characteristic from multiple contours, as in recent methods using orthogonal chain symbols to evaluate connectivity in binary images.[38]Related Techniques
Comparisons with Other Codes
Chain codes differ from crack codes in their approach to boundary tracing in binary images. Chain codes, such as the Freeman variant, trace the boundary by connecting the centers of pixels inside the object, using 8-connectivity to encode directions between adjacent pixels.[39] In contrast, crack codes trace the cracks or boundaries between object and background pixels, employing 4-connectivity for horizontal and vertical moves.[39] This interior-focused path in chain codes provides better preservation of object connectivity but underestimates enclosed area and lacks sub-pixel precision, whereas crack codes offer more accurate area estimation and sub-pixel boundary resolution at the cost of longer code lengths and overestimated perimeters along diagonals.[39] Compared to run-length encoding (RLE), which compresses filled regions by encoding consecutive runs of identical pixels across scanlines, chain codes are specialized for contour boundaries only and do not efficiently represent interior pixels.[40] RLE excels in compacting uniform areas within binary images, making it suitable for entire raster data, while chain codes focus on shape outlines, often leading to hybrid approaches where RLE handles region filling and chain codes describe edges.[41] In relation to polygonal approximations, such as those generated by the Douglas-Peucker algorithm, chain codes provide pixel-precise, lossless representations of digital curves but result in verbose sequences for complex boundaries.[42] Polygonal methods simplify these chains into fewer straight-line segments that approximate smooth shapes, achieving compactness for storage and rendering while introducing minor approximation errors.[42] Chain codes serve as an intermediate step for deriving such polygons, preserving exact raster details that approximations may discard.[42] Regarding performance, chain codes are simpler and more straightforward for representing boundaries in binary images compared to vector formats like SVG, which support scalable, parametric curves but require more computational overhead for raster-to-vector conversion.[43] However, chain codes are less efficient for complex, high-resolution images where vector representations reduce file sizes through geometric primitives.[43] Overall, chain codes offer advantages in lossless, discrete boundary encoding that reduces storage needs relative to full coordinate lists, enabling efficient shape feature extraction.[43] Their primary disadvantages include sensitivity to noise, which can propagate errors along the boundary, and lack of inherent scale invariance, necessitating extensions like resampling for multi-resolution analysis.[44][20]Interconversion Methods
Interconversion between chain code variants and other boundary representations is essential for integrating chain codes into diverse processing pipelines, such as combining compression with rendering or analysis in hybrid systems. These methods typically involve mapping directional sequences to alternative encodings while preserving the original contour topology, often achieving linear time complexity O(N), where N is the length of the chain code sequence. Seminal works have established procedural algorithms for these translations, leveraging local transitions or boundary adjustments to ensure lossless fidelity. One key aspect of interconversion involves adapting chain codes to vertex-based representations like the Vertex Chain Code (VCC), introduced by Bribiesca in 1999. VCC encodes the boundary of shapes composed of regular cells (e.g., pixels) using symbols that represent the number of cell vertices on the contour for each boundary cell, providing invariance to translation, rotation, starting point, and scaling. While direct mapping from Freeman Chain Code of Eight Directions (FCCE) to VCC requires reconstructing the boundary pixel sequence to count vertices per cell, algorithms exist to derive VCC from pixel boundaries traced by chain codes.[22][45] Converting a standard chain code to a crack code addresses the representational shift from pixel-centered boundaries (interior to pixels) to inter-pixel cracks, enabling compatibility with crack-based analyses like area estimation. The algorithm adjusts directions by rotating them 45 degrees clockwise or counterclockwise and reverses the traversal order to align the path with crack edges; for an FCCE direction d, the corresponding crack direction is (d + 1) mod 8 or equivalent, followed by path inversion. This mapping ensures the crack code traces the dual boundary without loss, as detailed in analyses of contour code properties. Reverse conversion from crack to chain code applies the inverse rotation and forward traversal, restoring the pixel-based sequence. To translate a chain code to run-length encoding (RLE), the algorithm simulates boundary traversal to populate row-wise run lengths, effectively rasterizing the contour into horizontal segments for image reconstruction or compression. Starting from the initial position, each directional step in the chain code updates the current row and column, appending or merging run lengths for boundary pixels encountered in that row; straight horizontal moves contribute directly to runs, while vertical or diagonal steps trigger row changes and partial run updates. This yields an RLE sequence representing the filled boundary outline. The reverse process, from RLE to chain code, employs boundary extraction algorithms that scan rows to identify edge transitions using neighborhood rules, tracing the perimeter to generate directional codes in a single pass. These extractions can operate on the RLE structure to achieve efficient contour recovery. A straightforward example of interconversion within chain code variants is between FCCE and the Differential Freeman Chain Code of Eight Directions (DFCCE), where DFCCE encodes relative turns rather than absolute directions. The forward conversion computes the differential as the modular difference between consecutive FCCE directions.function fcc_to_dfcc(fcc_sequence):
dfcc = []
for i in 1 to length(fcc_sequence) - 1:
diff = (fcc_sequence[i+1] - fcc_sequence[i]) mod 8
dfcc.append(diff)
return dfcc // Starting direction from fcc_sequence[0]
function fcc_to_dfcc(fcc_sequence):
dfcc = []
for i in 1 to length(fcc_sequence) - 1:
diff = (fcc_sequence[i+1] - fcc_sequence[i]) mod 8
dfcc.append(diff)
return dfcc // Starting direction from fcc_sequence[0]
