Is there a simple function in OpenCV that does exactly what bwlabel does in MATLAB?
Asked
Active
Viewed 9,604 times
1
-
2This doesn't seem to be a duplicate. As noted in sinelaw's answer (cv)findContours is not a direct alternative, and findContours is still the top answer for the so-called duplicate. If everything is in your hands you could re-write accordingly, but not if feeding an API out of your hands. Additionally it's natural for someone familiar with MATLAB to search for "bwlabel" rather than "connected components", and this comes up first on that search. Finally a better answer now exists for opencv 3, connectedComponets(), but because it is closed the answer can not be updated. – Ken Dec 13 '17 at 23:51
2 Answers
3
One possible alternative is to use findContours and/or drawContours, as explained in the docs.
This isn't exactly the same as bwlabel
, but may be close enough.
The docs contain this page demonstrating how to use these two functions together. The resulting image contains randomly colored contours. If you use an index (1,2,3...) instead of the random colors, and use single channel grayscale output, it will be similar to bwlabel
.

sinelaw
- 16,205
- 3
- 49
- 80
-
Thank you for your answer. Maybe you have an example how to use those functions like BWlable? – user2339198 Dec 03 '13 at 17:33
-
2
I think you can use cvBlob library. It has many features apart from bwlabel
. In OpenCV 3.0, connectedComponents
function can be used.

Autonomous
- 8,935
- 1
- 38
- 77