Repository navigation
Description
This is an RFC for the implementation of a multithreaded version of the connected component labeling algorithm.
Such algorithms have appeared in research papers since a few years ago. A common theme in these approaches is to divide the image into blocks (tiles or strips), and then:
- Perform labeling within each block, in parallel;
- Let the main (caller) thread process the equivalence labeling of "seams" between blocks;
- Update the labeling in each block to the final labeling, in parallel.
The second step is single-threaded. As a result there is a synchronization barrier before and after the second step.
I ask this question here instead of OpenCV Answers because it is not a beginner question, and I believe this is a feature that will eventually be implemented in (contributed to) OpenCV. A second reason I ask this is because I have implemented a proprietary version of it for my employer. I have secured the permissions to make a separate implementation for OpenCV provided that the implementation details in the contributed version can be fully traced to research work that has already been published.
I am aware of the existing connected component labeling implementation in OpenCV and has studied the algorithm. However, it is too early for me to say whether I will be able to keep the best of both implementations.
One minor detail is that my current (proprietary) implementation uses a more flexible definition of connectivity, one which is traditionally termed "flat-zones CCL". This flexible definition allows caller to specify two predicates:
- isForeground ( point ) -> bool
- isConnected ( pointOne, pointTwo ) -> bool
The foreground predicate is given the pixel value, and returns whether it will be assigned a label. All non-foreground pixels will be given a nondescript pseudo-label.
If two pixels are adjacent (according 4 or 8 neighborhood), and both pixels are foreground, then the connectivity predicate will be invoked. The two pixels will be connected if and only if all these conditions are satisfied.
The current OpenCV implementation corresponds to the imposed restrictions that:
- Image must be CV_8UC1 or CV_8SC1
- isForeground is currently hard-coded as Image(row, col) != 0
- isConnected must return true, i.e. two pixels which are both foreground and adjacent, must be connected by definition.
(Note: in the current OpenCV implementation, Wu's implementation defines foreground as Image(row, col) != 0, whereas Grana's implementation defines foreground as Image(row, col) > 0. This difference may be pose problem for CV_8SC1 input image type.)
The significance of this flexibility is that it allows for the possibility that two pixels which are both foreground to be disconnected, by a suitable choice of connectivity predicate. This flexibility is required in the implementation of some image processing algorithms.
On the other hand, this predicate flexibility is incompatible with some CCL optimization techniques, such as Grana et al. For this reason, users who provide isConnected predicate should indicate whether it is a trivial implementation (one that always return true), so that all optimization techniques can be applied.
The predicate will need to be supplied as a functor to a class template.
I haven't started working on this second implementation yet; so I would like to solicit some feedback about the desirability of this feature before I start. Please feel free to ask questions.
I understand that performance benchmarks will help make an informed decision. Please feel free to suggest performance benchmark scenarios that I should test for.
Activity
PkLab commentedon Sep 12, 2016
Maybe you already know about available test framework, in case see :
catree commentedon Sep 13, 2016
There is also YACCLAB: Yet Another Connected Components Labeling Benchmark repository by one of the author of the Grana method.
nosleduc commentedon Sep 29, 2016
Did you compare your implementation with "Optimized Block-based Connected Components Labeling with Decision Trees" by Grana. It is not multithreaded but it is quite fast.
This algorithm was added one month ago in OpenCV 3
CostantinoGrana commentedon Nov 4, 2016
Just for the records, we are nearly ready to make a pull request with a multithreaded version of both my algorithm and SAUF (which can cope with 4 connection also). We are just missing the statistics computation, but everything else works. You will be able to test it, but the speedup on a quad core is about 2.
The image is simply cut in horizontal stripes (to ease cache coherency), each stripe independently labeled, boundaries are sequentially merged, and finally the second scan is again performed in stripes (no data dependency there).
Stay tuned!
kinchungwong commentedon Nov 5, 2016
@CostantinoGrana The reason I remained silent is that, during my own testing, I also noticed that scaling is much worse than linear). I saw that quad-core performance is slightly better than 2, but less than three. The final assignment of labels used too much time. I can share my test source code, but I guess I have to retract my proposal for inclusion into OpenCV.
sturkmen72 commentedon Aug 15, 2021
@kinchungwong #8789 is merged. can we close the issue?