I am looking for an example of how to use OpenCV's ConnectedComponentsWithStats() function in python, note this is only available with OpenCV 3 or newer. The official documentation only shows the API for C++, even though the function exists when compiled for python. I could not find it anywhere online.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
The function works as follows:
Labels is a matrix the size of the input image where each element has a value equal to its label.
Stats is a matrix of the stats that the function calculates. It has a length equal to the number of labels and a width equal to the number of stats. It can be used with the OpenCV documentation for it:
Centroids is a matrix with the x and y locations of each centroid. The row in this matrix corresponds to the label number.
Adding to
Zack Knopp
answer, If you are using a grayscale image you can simply use:When I tried using
Zack Knopp
answer on a grayscale image it didn't work and this was my solution.I have come here a few times to remember how it works and each time I have to reduce the above code to :
Hopefully, it's useful for everyone :)