简体   繁体   English

如何访问 OpenCV 连接组件标签中的单个标签

[英]How to access individual labels in OpenCV connected component labelling

I am trying to access individual labels of objects with OpenCV's connectedComponentsWithStats in Python.我试图在 Python 中使用 OpenCV 的 connectedComponentsWithStats 访问对象的各个标签。 However, when I run the connectedComponentsWithStats function, a labelled array is returned that has each object with different pixel values.但是,当我运行 connectedComponentsWithStats 函数时,会返回一个带标签的数组,其中每个对象具有不同的像素值。 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.我在这里使用了大约 12000 x 10000 像素尺寸的非常大的图像。

I have an image here that has been labelled with cv.connectedComponentsWithStats:我这里有一张用 cv.connectedComponentsWithStats 标记的图像: 在此处输入图片说明

The colormap used starts with purple(1) and ends with yellow (last label).使用的颜色图以紫色 (1) 开头,以黄色(最后一个标签)结尾。 How do I reference each labelled object independently as a separate array?如何将每个标记对象作为单独的数组独立引用?

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这将返回一个平坦的结果,从您的问题中不清楚这是否可以接受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM