Contents

Overview

In five seconds

Detection of connected objects in an image, mainly used in image analysis and OCR.

In five minutes

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.

Pay special attention to the recursive function GetRoot(), that's how we reach the parent of any label.

Remember this part? This is what the function Join(Label root) does. Now let's say we have
three labels, 1, 2, and 3, and we picked 1 to be our current label; all we have to do is loop the other labels, if their roots don't match,
set their root to that of the label we just picked.

Conclusion

Hope I delivered a clear explanation, feel free to comment or ask .
Drawings by zwibbler

Excellent article! However, it might be some bugs in the code as I tested with one image and the label images are not exactly as I would expect. I can send you the test image if you can take a look. Thanks.

there may be more than two different roots in one region, so we must set Root of these roots to one root in UnifyNeighboringLabelsParents() .
if some black points are on the border, GetDistinctNeighboringLabels() is not correct.
and thanks so much.

Can u plz do give an explanation and demonstration on using CC-labeling for text detection in images? or at least forward me to an example, I especially want to know how to filter out non-text regions in an image. thanks

Detection of objects is only the first step, how do you know that "2" is 2?
its through pattern recognition[^], teaching the computer how to interpret objects and discard noise using neural networks[^].

It's a wholesome topic, there's a lot of good articles here on the CodeProject, but are you trying to create your own OCR engine ? chances are you are better off taking up where others left off, using OpenCV[^] is a good open source project in C++, I don't know if there's one in .net (not just a wrapper).