I am on Ubuntu, python 2.7. Working with OpenCV.
I was trying to understand exactly what the function cv2.connectedComponents is doing. This is the image:
This is because cv2.connectedComponents()
considers only the white portion as a component. Hence you are getting a single component.
You have to invert your image. You can do so by using cv2.bitwise_not()
function.
CODE:
import cv2
import numpy as np
img = cv2.imread('cc.png', 0)
ret, thresh = cv2.threshold(img, 127, 255, 0)
#---- Inverting the image here ----
img = cv2.bitwise_not(thresh)
_, markers = cv2.connectedComponents(img)
print np.amax(markers)
RESULT:
19