0

I am trying to access individual labels of objects with OpenCV's connectedComponentsWithStats in Python. However, when I run the connectedComponentsWithStats function, a labelled array is returned that has each object with different pixel values. How do I efficiently access each labelled object as a separate array? I am using very large images here with about 12000 x 10000 pixel dimensions.

I have an image here that has been labelled with cv.connectedComponentsWithStats: enter image description here

The colormap used starts with purple(1) and ends with yellow (last label). How do I reference each labelled object independently as a separate array?

Sibh
  • 393
  • 1
  • 4
  • 12
  • Please show your code and perhaps a small example image. – fmw42 Dec 20 '19 at 19:31
  • If you want to categorize all pixels of each object, do a loop on all the image pixels (rows*cols) and push all the pixels with equal color into a common array. If you get any new color make a new array and so on. At the end you have an array of arrays, each representing an object. – MeiH Dec 21 '19 at 07:33

1 Answers1

1
source = <some_image>
labels = <connected components result>

for label in np.unique(labels):
    m = (labels == label)  # boolean array/mask of pixels with this label
    obj = source[m]  # orignal pixel values for the labeled object

This will give back a flat result, it's unclear from your question whether this is acceptable

berardig
  • 652
  • 4
  • 10