Connected-component labeling (alternatively connected-component analysis, blob extraction, region labeling, blob discovery, or region extraction) is an algorithmic application of graph theory, where subsets of connected components are uniquely labeled based on a given heuristic. Connected-component labeling is not to be confused with segmentation.
Connected-component labeling is used in computer vision to detect connected regions in binary digital images, although color images and data with higher dimensionality can also be processed. When integrated into an image recognition system or human-computer interaction interface, connected component labeling can operate on a variety of information. Blob extraction is generally performed on the resulting binary image from a thresholding step, but it can be applicable to gray-scale and color images as well. Blobs may be counted, filtered, and tracked.
Blob extraction is related to but distinct from blob detection.
- 1 Overview
- 2 Definition 
- 3 Algorithms
- 4 Graphical example of two-pass algorithm
- 5 Others
- 6 Performance evaluation
- 7 Hardware architectures
- 8 See also
- 9 References
- 10 External links
A graph, containing vertices and connecting edges, is constructed from relevant input data. The vertices contain information required by the comparison heuristic, while the edges indicate connected 'neighbors'. An algorithm traverses the graph, labeling the vertices based on the connectivity and relative values of their neighbors. Connectivity is determined by the medium; image graphs, for example, can be 4-connected neighborhood or 8-connected neighborhood.
Following the labeling stage, the graph may be partitioned into subsets, after which the original information can be recovered and processed .
The usage of the term connected components labeling (CCL) and its definition is quite consistent in the academic literature, whereas connected components analysis (CCA) varies in terms of both terminology and problem definition.
Rosenfeld et al. define connected components labeling as the “[c]reation of a labeled image in which the positions associated with the same connected component of the binary input image have a unique label.” Shapiro et al. define CCL as an operator whose “input is a binary image and [...] output is a symbolic image in which the label assigned to each pixel is an integer uniquely identifying the connected component to which that pixel belongs.”
There is no consensus on the definition of connected components analysis in the academic literature. It is often used interchangeably with CCL. A more extensive definition is given by Shapiro et al.: “Connected component analysis consists of connected component labeling of the black pixels followed by property measurement of the component regions and decision making.” The definition for connected components analysis presented here is more general, taking the thoughts expressed in  into account.
“Connected components analysis derives one feature vector of each connected component in a binary 2-D input image. A feature vector of a connected component is an n-tuple composed of functions of the component’s pattern.”
The algorithms discussed can be generalized to arbitrary dimensions, albeit with increased time and space complexity.
One component at a time
This is a fast and very simple method to implement and understand. It is based on graph traversal methods in graph theory. In short, once the first pixel of a connected component is found, all the connected pixels of that connected component are labelled before going onto the next pixel in the image. This algorithm is part of Vincent and Soille's watershed segmentation algorithm, other implementations also exist.
In order to do that a linked list is formed that will keep the indexes of the pixels that are connected to each other, steps (2) and (3) below. The method of defining the linked list specifies the use of a depth or a breadth first search. For this particular application, there is no difference which strategy to use. The simplest kind of a last in first out queue implemented as a singly linked list will result in a depth first search strategy.
It is assumed that the input image is a binary image, with pixels being either background or foreground and that the connected components in the foreground pixels are desired. The algorithm steps can be written as:
- Start from the first pixel in the image. Set current label to 1. Go to (2).
- If this pixel is a foreground pixel and it is not already labelled, give it the current label and add it as the first element in a queue, then go to (3). If it is a background pixel or it was already labelled, then repeat (2) for the next pixel in the image.
- Pop out an element from the queue, and look at its neighbours (based on any type of connectivity). If a neighbour is a foreground pixel and is not already labelled, give it the current label and add it to the queue. Repeat (3) until there are no more elements in the queue.
- Go to (2) for the next pixel in the image and increment current label by 1.
Note that the pixels are labelled before being put into the queue. The queue will only keep a pixel to check its neighbours and add them to the queue if necessary. This algorithm only needs to check the neighbours of each foreground pixel once and doesn't check the neighbours of background pixels.
Relatively simple to implement and understand, the two-pass algorithm, (also known as the Hoshen–Kopelman algorithm) iterates through 2-dimensional binary data. The algorithm makes two passes over the image. The first pass to assign temporary labels and record equivalences and the second pass to replace each temporary label by the smallest label of its equivalence class.
Connectivity checks are carried out by checking neighbor pixels' labels (neighbor elements whose labels are not assigned yet are ignored), or say, the North-East, the North, the North-West and the West of the current pixel (assuming 8-connectivity). 4-connectivity uses only North and West neighbors of the current pixel. The following conditions are checked to determine the value of the label to be assigned to the current pixel (4-connectivity is assumed)
Conditions to check:
- Does the pixel to the left (West) have the same value as the current pixel?
- Yes – We are in the same region. Assign the same label to the current pixel
- No – Check next condition
- Do both pixels to the North and West of the current pixel have the same value as the current pixel but not the same label?
- Yes – We know that the North and West pixels belong to the same region and must be merged. Assign the current pixel the minimum of the North and West labels, and record their equivalence relationship
- No – Check next condition
- Does the pixel to the left (West) have a different value and the one to the North the same value as the current pixel?
- Yes – Assign the label of the North pixel to the current pixel
- No – Check next condition
- Do the pixel's North and West neighbors have different pixel values than current pixel?
- Yes – Create a new label id and assign it to the current pixel
The algorithm continues this way, and creates new region labels whenever necessary. The key to a fast algorithm, however, is how this merging is done. This algorithm uses the union-find data structure which provides excellent performance for keeping track of equivalence relationships. Union-find essentially stores labels which correspond to the same blob in a disjoint-set data structure, making it easy to remember the equivalence of two labels by the use of an interface method E.g.: findSet(l). findSet(l) returns the minimum label value that is equivalent to the function argument 'l'.
Once the initial labeling and equivalence recording is completed, the second pass merely replaces each pixel label with its equivalent disjoint-set representative element.
A faster-scanning algorithm for connected-region extraction is presented below.
On the first pass:
- Iterate through each element of the data by column, then by row (Raster Scanning)
- If the element is not the background
- Get the neighboring elements of the current element
- If there are no neighbors, uniquely label the current element and continue
- Otherwise, find the neighbor with the smallest label and assign it to the current element
- Store the equivalence between neighboring labels
On the second pass:
- Iterate through each element of the data by column, then by row
- If the element is not the background
- Relabel the element with the lowest equivalent label
Here, the background is a classification, specific to the data, used to distinguish salient elements from the foreground. If the background variable is omitted, then the two-pass algorithm will treat the background as another region.
Graphical example of two-pass algorithm
This section does not cite any sources. (June 2013) (Learn how and when to remove this template message)
1. The array from which connected regions are to be extracted is given below (8-connectivity based).
We first assign different binary values to elements in the graph. It is to be noted that the values "0~1" at the center of each of the elements in the following graph are the elements' values, whereas the "1,2,...,7" values in the next two graphs are the elements' labels. The two concepts should not be confused.
2. After the first pass, the following labels are generated:
A total of 7 labels are generated in accordance with the conditions highlighted above.
The label equivalence relationships generated are,
|Set ID||Equivalent Labels|
3. Array generated after the merging of labels is carried out. Here, the label value that was the smallest for a given region "floods" throughout the connected region and gives two distinct labels, and hence two distinct labels.
4. Final result in color to clearly see two different regions that have been found in the array.
The pseudocode is as follows:
algorithm TwoPass(data) linked =  labels = structure with dimensions of data, initialized with the value of Background First pass for row in data: for column in row: if data[row][column] is not Background neighbors = connected elements with the current element's value if neighbors is empty linked[NextLabel] = set containing NextLabel labels[row][column] = NextLabel NextLabel += 1 else Find the smallest label L = neighbors labels labels[row][column] = min(L) for label in L linked[label] = union(linked[label], L) Second pass for row in data for column in row if data[row][column] is not Background labels[row][column] = find(labels[row][column]) return labels
The find and union algorithms are implemented as described in union find.
Create a region counter
Scan the image (in the following example, it is assumed that scanning is done from left to right and from top to bottom):
- For every pixel check the north and west pixel (when considering 4-connectivity) or the northeast, north, northwest, and west pixel for 8-connectivity for a given region criterion (i.e. intensity value of 1 in binary image, or similar intensity to connected pixels in gray-scale image).
- If none of the neighbors fit the criterion then assign to region value of the region counter. Increment region counter.
- If only one neighbor fits the criterion assign pixel to that region.
- If multiple neighbors match and are all members of the same region, assign pixel to their region.
- If multiple neighbors match and are members of different regions, assign pixel to one of the regions (it doesn't matter which one). Indicate that all of these regions are equivalent.
- Scan image again, assigning all equivalent regions the same region value.
Some of the steps present in the two-pass algorithm can be merged for efficiency, allowing for a single sweep through the image. Multi-pass algorithms also exist, some of which run in linear time relative to the number of image pixels.
The interest to the algorithm arises again with an extensive use of CUDA.
Matlab code for the one-component-at-a-time algorithm
This section does not cite any sources. (June 2014) (Learn how and when to remove this template message)
- Connected-component matrix is initialized to size of image matrix.
- A mark is initialized and incremented for every detected object in the image.
- A counter is initialized to count the number of objects.
- A row-major scan is started for the entire image.
- If an object pixel is detected, then following steps are repeated while (Index !=0)
- Set the corresponding pixel to 0 in Image.
- A vector (Index) is updated with all the neighboring pixels of the currently set pixels.
- Unique pixels are retained and repeated pixels are removed.
- Set the pixels indicated by Index to mark in the connected-component matrix.
- Increment the marker for another object in the image.
One-Component-at-a-Time(Image) [M, N]=size(Image); Connected = zeros(M,N); Mark = Value; Difference = Increment; Offsets = [-1; M; 1; -M]; Index = ; No_of_Objects = 0; for i: 1:M : for j: 1:N: if(Image(i,j)==1) No_of_Objects = No_of_Objects +1; Index = [((j-1)*M + i)]; Connected(Index)=Mark; while ~isempty(Index) Image(Index)=0; Neighbors = bsxfun(@plus, Index, Offsets'); Neighbors = unique(Neighbors(:)); Index = Neighbors(find(Image(Neighbors))); Connected(Index)=Mark; end Mark = Mark + Difference; end end end
The run time of the algorithm depends on the size of the image and the amount of foreground. The time complexity is comparable to the two pass algorithm if the foreground covers a significant part of the image. Otherwise the time complexity is lower. However, memory access is less structured than for the two-pass algorithm, which tends to increase the run time in practice.
In the last two decades many novel approaches on connected-component labeling have been proposed and almost none of them was compared on the same data. YACCLAB  (acronym for Yet Another Connected Components Labeling Benchmark) is an example of C++ open source framework which collects, runs, and tests connected-component labeling algorithms.
The emergence of FPGAs with enough capacity to perform complex image processing tasks also led to high-performance architectures for connected component labeling. Most of these architectures utilize the single pass variant of this algorithm, because of the limited memory resources available on an FPGA. These types of connected component labeling architectures are able to process several image pixels in parallel, thereby enabling a high throughput at low processing latency to be achieved.
- Image analysis
- Computer vision
- Feature extraction
- Connected component (graph theory)
- Graph traversal
- Union find
- Flood fill
- Blob detection
- Samet, H.; Tamminen, M. (1988). "Efficient Component Labeling of Images of Arbitrary Dimension Represented by Linear Bintrees". IEEE Transactions on Pattern Analysis and Machine Intelligence. IEEE. 10 (4): 579. doi:10.1109/34.3918.
- Michael B. Dillencourt; Hannan Samet; Markku Tamminen (1992). "A general approach to connected-component labeling for arbitrary image representations". Journal of the ACM. J. ACM. 39 (2): 253. doi:10.1145/128749.128750.
- Weijie Chen; Maryellen L. Giger; Ulrich Bick (2006). "A Fuzzy C-Means (FCM)-Based Approach for Computerized Segmentation of Breast Lesions in Dynamic Contrast-Enhanced MR Images". Academic Radiology. 13 (1): 63–72. doi:10.1016/j.acra.2005.08.035. PMID 16399033.
- Kesheng Wu; Wendy Koegler; Jacqueline Chen; Arie Shoshani (2003). "Using Bitmap Index for Interactive Exploration of Large part Datasets". SSDBM.
- R. Fisher; S. Perkins; A. Walker; E. Wolfart (2003). "Connected Component Labeling".
- "Definition of Connected Components Analysis and Connected Component Labeling".
- Klaiber, Michael J. (2016). A Parallel and Resource-Efficient Single Lookup Connected Components Analysis Architecture for Reconfigurable Hardware. University of Stuttgart.
- Rosenfeld, Azriel; Pfaltz, John L. (October 1966). "Sequential Operations in Digital Picture Processing". J. ACM. 13 (4): 471–494. doi:10.1145/321356.321357. ISSN 0004-5411.
- Shapiro, Linda G. Connected Component Labeling and Adjacency Graph Construction. pp. 1–30. doi:10.1016/s0923-0459(96)80011-5.
- Fu, Y.; Chen, X.; Gao, H. (December 2009). "A New Connected Component Analysis Algorithm Based on Max-Tree". 2009 Eighth IEEE International Conference on Dependable, Autonomic and Secure Computing: 843–844. doi:10.1109/DASC.2009.150.
- Grana, C.; Borghesani, D.; Santinelli, P.; Cucchiara, R. (August 2010). "High Performance Connected Components Labeling on FPGA". 2010 Workshops on Database and Expert Systems Applications: 221–225. doi:10.1109/DEXA.2010.57.
- Vincent, Luc; Soille, Pierre (June 1991). "Watersheds in digital spaces: an efficient algorithm based on immersion simulations". IEEE Transactions on Pattern Analysis and Machine Intelligence. 13 (6): 583. doi:10.1109/34.87344.
- Abubaker, A; Qahwaji, R; Ipson, S; Saleh, M (2007). "One Scan Connected Component Labeling Technique". Signal Processing and Communications, 2007. ICSPC 2007. IEEE International Conference: 1283. doi:10.1109/ICSPC.2007.4728561. ISBN 978-1-4244-1235-8.
- Shapiro, L.; Stockman, G. (2002). Computer Vision (PDF). Prentice Hall. pp. 69–73.
- Introduction to Algorithms, , pp498
- Lifeng He; Yuyan Chao,; Suzuki, K. (1 May 2008). "A Run-Based Two-Scan Labeling Algorithm". IEEE Transactions on Image Processing. 17 (5): 749–756. Bibcode:2008ITIP...17..749H. doi:10.1109/TIP.2008.919369. PMID 18390379.
- Kenji Suzuki; Isao Horiba; Noboru Sugie (2003). "Linear-time connected-component labeling based on sequential local operations". Computer Vision and Image Understanding. 89: 1. doi:10.1016/S1077-3142(02)00030-9.
- Yujie Han; Robert A. Wagner (1990). "An efficient and fast parallel-connected component algorithm". Journal of the ACM. 37 (3): 626. doi:10.1145/79147.214077.
- Grana, C.; Bolelli, F.; Baraldi, L.; Vezzani, R. (2016). "YACCLAB - Yet Another Connected Components Labeling Benchmark" (PDF). 23rd International Conference on Pattern Recognition. Cancún.
- Bailey, D. G.; Johnston, C. T.; Ma, Ni (September 2008). "Connected components analysis of streamed images". 2008 International Conference on Field Programmable Logic and Applications: 679–682. doi:10.1109/FPL.2008.4630038.
- M. J Klaiber; D. G Bailey; Y. Baroud; S. Simon (2015). "A Resource-Efficient Hardware Architecture for Connected Components Analysis". IEEE Transactions on Circuits and Systems for Video Technology. IEEE Transactions on Circuits and Systems for Video Technology: 1. doi:10.1109/TCSVT.2015.2450371.