connected component labeling in python

The OpenCV 3.0 docs for connectedComponents() don’t mention Python but it actually is implemented. See for e.g. this SO question. On OpenCV 3.4.0 and above, the docs do include the Python signatures, as can be seen on the current master docs. The function call is simple: num_labels, labels_im = cv2.connectedComponents(img) and you can specify a … Read more

How to use OpenCV’s connectedComponentsWithStats in Python?

The function works as follows: # Import the cv2 library import cv2 # Read the image you want connected components of src = cv2.imread(‘/directorypath/image.bmp’) # Threshold it so it becomes binary ret, thresh = cv2.threshold(src,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # You need to choose 4 or 8 for connectivity type connectivity = 4 # Perform the operation output = … Read more