algorithm to trace border in 2D array
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Tracing the border in a 2D array is a fundamental operation in image processing, computer graphics, and computational geometry. The goal is to identify and delineate the edge or perimeter of a structure within a matrix, where entries typically represent pixel intensity values, object presence, or other domain-specific data points.
Core Algorithmic Approach
There are numerous algorithms to trace borders in 2D arrays, with applicability depending on the specific requirements, such as precision, speed, and simplicity. A common algorithm widely used for edge tracing in binary images is the Contour Tracing Algorithm, specifically the Moore-Neighbor Tracing Algorithm or the Square Tracing Algorithm.
Conceptual Overview
- Segmentation: Begin with a segmented 2D array, where the border of the object is represented by distinct values (usually binary,
0for background and1for the object). - Starting Point: Choose a starting pixel on the object boundary. This can be identified using a scanning approach starting from a corner of the matrix.
- Orientation and Navigation:
- The algorithm typically moves in a clockwise or counterclockwise manner around the object.
- For each current pixel, examine its neighboring pixels using a defined pattern (e.g., 8-connected or 4-connected neighbors).
- Termination: The process stops when the initial starting point is reached for the second time after tracing the entire boundary.
Moore-Neighbor Tracing Algorithm
This is one of the simplest forms of contour tracing designed for 8-connected components.
- Initialization:
- Input a binary image, where
1represents the object. - Identify the starting boundary pixel.
- Traversal:
- From the current pixel, check neighboring pixels in a clockwise direction until a boundary pixel is found.
- Move to the boundary pixel.
- Continue checking the next set of neighbors.
- Output:
- The ordered list of border pixels or the contour.
Example in Pseudocode
- 8-connected vs. 4-connected: The choice between 8 and 4 connectivity affects the precision and can lead to different tracing outcomes. Use 8-connected for smoother borders.
- Complexity: The time complexity depends on the number of border pixels and the efficiency of neighbor checking, generally where is the number of pixels in the array.
- Limitations: The algorithm assumes a single connected component; additional logic might be needed for multiple objects.
Related reading
- Algorithmic question Best angle to view trees from fixed camera
- Algorithms for finding a look alike face?
- Apple Vision framework – Text extraction from image
- Are modern CNN convolutional neural network as DetectNet rotate invariant?
- Algorithm to transfer water from a set of bottles to another one metaphorically speaking
- Algorithm to transform a workflow DAG into parallel resource allocation?
- Algorithm to transform one word to another through valid words
- Algorithm/approximation for combined independent set/hamming distance

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.