How it works...
There are two functions in OpenCV that can be used to find connected components: cv2.connectedComponents and cv2.connectedComponentsWithStats. Both take the same arguments: the binary image whose components are to be found, the connectivity type, and the depth of the output image, with labels for components. Return values will vary.
cv2.connectedComponents is simpler, and returns a tuple of component numbers and an image with labels for components (labelmap). In addition to the previous function's outputs, cv2.connectedComponentsWithStats also returns statistics about each component and the components' centroid locations.
The labelmap has the same size as the input image, and each of its pixels has a value in the range [0, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access