Is connectedcomponents working right?

I’m working on segmentation at the moment and I want to proof my results with openCV.
When I’m working with different connectivity type the result is the same.
Is this a bug in openCV?

My minimal example with connectivity=4:

test = np.zeros((5, 5)).astype(np.uint8)
test[1:3, 1:3] = 255
test[3:5, 3:5] = 255
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(test, 4, cv2.CV_32S)
test, labels

is:

(array([[  0,   0,   0,   0,   0],
        [  0, 255, 255,   0,   0],
        [  0, 255, 255,   0,   0],
        [  0,   0,   0, 255, 255],
        [  0,   0,   0, 255, 255]], dtype=uint8),
 array([[0, 0, 0, 0, 0],
        [0, 1, 1, 0, 0],
        [0, 1, 1, 0, 0],
        [0, 0, 0, 1, 1],
        [0, 0, 0, 1, 1]], dtype=int32))

and thats wrong, because a 4-way-connectivity only connects elements when

[[0, 1, 0],
[1, 1, 1],
[0, 1, 0]]

is true.

Or am I wrong?
Thanks for your responds.
Regards Marc

use python’s features, name the parameters.

connectedComponentsWithStats(..., connectivity=4, ...)

basically, you passed the 4 in the wrong position. check the signature of the call. using this python feature, you can avoid such errors.

the signature is
cv.connectedComponentsWithStats( image[, labels[, stats[, centroids[, connectivity[, ltype]]]]] ) -> retval, labels, stats, centroids

maybe you looked at connectedComponentsWithStatsWithAlgorithm?

1 Like

Thanks Christoph for your quick reply.
You’re right naming the parameters is the proper way and solve the problem.