[英]How to get all pixels in connected components by opencv?
I use the new opencv function connectedComponentsWithStats
(version 3.0). 我使用新的opencv函数
connectedComponentsWithStats
(3.0版)。 How do I get all pixels in connected components? 如何获取连接组件中的所有像素?
The result 结果
The third argument of connectedComponentsWithStats
, stats
, provides you information that will allow you to draw bounding boxes around the labeled regions. connectedComponentsWithStats
, stats
的第三个参数为您提供了允许您在标记区域周围绘制边界框的信息。
The second argument, labels
should contain an image with zeros (black pixels) for non labeled pixels and coloured pixels for the labeled groups of pixels, one colour per label. 第二个参数,
labels
应包含一个图像,其中非标记像素为零(黑色像素),标记像素组为彩色像素,每个标签一种颜色。 Bare in mind, the non-zero values will be unique, but quite small (eg for 7 labels, starting at 0 (background), the label values will go up to 6). 请记住,非零值将是唯一的,但非常小(例如,对于7个标签,从0(背景)开始,标签值将达到6)。
Here's an example 9x9 binary image: 这是一个9x9二进制图像示例:
[
[255,255, 0, 0, 0, 0, 0,255,255],
[255, 0, 0,255,255,255, 0, 0,255],
[ 0, 0,255, 0, 0, 0,255, 0, 0],
[ 0,255, 0, 0,255, 0, 0,255, 0],
[ 0,255, 0,255,255,255, 0,255, 0],
[ 0,255, 0, 0,255, 0, 0,255, 0],
[ 0, 0,255, 0, 0, 0,255, 0, 0],
[255, 0, 0,255,255,255, 0, 0,255],
[255,255, 0, 0, 0, 0, 0,255,255]
]
the connected components labels are: 连接的组件标签是:
[
[1 1 0 0 0 0 0 3 3]
[1 0 0 2 2 2 0 0 3]
[0 0 2 0 0 0 2 0 0]
[0 2 0 0 4 0 0 2 0]
[0 2 0 4 4 4 0 2 0]
[0 2 0 0 4 0 0 2 0]
[0 0 2 0 0 0 2 0 0]
[5 0 0 2 2 2 0 0 6]
[5 5 0 0 0 0 0 6 6]
]
to visualise them via imshow
you might want to scale those values up. 通过
imshow
可视化它们你可能想要扩展这些值。 (could be a look-up table of colours you choose or computed colours as long as they are different enough to visually make sense). (可以是您选择的颜色查找表或计算颜色,只要它们足够不同,在视觉上有意义)。
Here's an example of scaling the labels above by 42 (255 max value / 6 foreground labels): 以下是将上面的标签缩放42(最大值255个/前景标签6个)的示例:
[
[ 42 42 0 0 0 0 0 126 126]
[ 42 0 0 84 84 84 0 0 126]
[ 0 0 84 0 0 0 84 0 0]
[ 0 84 0 0 168 0 0 84 0]
[ 0 84 0 168 168 168 0 84 0]
[ 0 84 0 0 168 0 0 84 0]
[ 0 0 84 0 0 0 84 0 0]
[210 0 0 84 84 84 0 0 252]
[210 210 0 0 0 0 0 252 252]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.