numpy/python中的Floodlight分割图像 [英] Floodfill segmented image in numpy/python

查看:72
本文介绍了numpy/python中的Floodlight分割图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组,该数组表示图像中的分段二维矩阵.基本上,它是一个稀疏矩阵,具有一堆闭合形状,这些闭合形状是图像各部分的轮廓.我需要做的是用numpy中的不同颜色/标签为每个闭合形状中的空白像素着色.

I have a numpy array which represents a segmented 2-dimensional matrix from an image. Basically, it's a sparse matrix with a bunch of closed shapes that are the outlines of the segments of the image. What I need to do is colorize the empty pixels within each closed shape with a different color/label in numpy.

我知道我可以使用PIL中的Floodfill来做到这一点,但是我试图不必将矩阵从numpy到PIL来回转换.如果在函数中有一个像skimage或sklearn这样的函数可以用不同的标签为我自动标记"矩阵的所有不同封闭区域,那将是很好的(它可以是单调递增的整数或颜色.只要它代表其区域内相邻像素的正确分组即可.

I know I could do this with floodfill in PIL but I'm trying not to have to convert the matrix back and forth from numpy to PIL. It would be nice if there was a function in someting like skimage or sklearn that could "auto-label" all the different closed regions of my matrix with a different label for me (it could be a monotonically incrementing integer or a color. I don't care so long as it represents the correct grouping of adjacent pixels within its region).

我已经花了很多时间尝试实现自己的泛洪功能,这时我想做些可以为我开箱即用标记图像的事情.

I've already spent a lot of time trying to implement my own floodfill and at this point I'd just like somehting that could label the image out of the box for me.

推荐答案

我假设您的矩阵是二进制的,其中非零值表示提取的段,而零值则是您不关心的值. measure模块中的scikit-image label函数可能是您感兴趣的: http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.label

I'm assuming that your matrix is binary where non-zero values represent the extracted segments and zero values are values you don't care about. The scikit-image label function from the measure module may be of interest: http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.label

它实质上执行连接的组件分析,并标记所有单独闭合的组件以及整数.但是,关于如何指定连接性,您需要格外小心.有4个连接和8个连接,其中前者仅使用北,南,东和西方向找到连接区域,而8个连接使用所有8个方向(北,南,东,西,东北,东南,西北,西南) ).您可以使用connectivity选项,将1指定为4个连接,将2指定为8个连接.

It essentially performs a connected components analysis and labels all separately closed components together with an integer number. You need to be careful though with regards to how you specify connectivity. There is 4-connectedness and 8-connectedness where the former finds connected regions using only the North, South, East and West directions whereas 8-connectedness uses all 8 directions (North, South, East, West, Northeast, Southeast, Northwest, Southwest). You would use the connectivity option and specify 1 for the 4-connectedness and 2 for the 8-connectedness.

但是,默认连通性是完全连通性,因此对于2D情况,它将是2选项.我怀疑你会是这种方式.矩阵中的任何斑点为零都将被标记为零.事不宜迟,这是一个非常简单的可重现示例:

However, the default connectivity would be a full connectivity, so for the case of 2D it would be the 2 option. I suspect for you it would be this way. Any blobs in your matrix that are zero would be labelled as zero. Without further ado, here's a very simple reproducible example:

In [1]: from skimage.measure import label

In [2]: import numpy as np

In [3]: x = np.zeros((8,8))

In [4]: x[0:4,0:4] = 1

In [5]: x[6:8,6:8] = 1

In [6]: x
Out[6]:
array([[ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  1.]])

In [7]: label(x)
Out[7]:
array([[1, 1, 1, 1, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 2, 2],
       [0, 0, 0, 0, 0, 0, 2, 2]], dtype=int64)

我们可以看到我在左上角和右下角创建了两个单独的岛.一旦运行label函数,它将返回一个标签矩阵,该标签矩阵标识彼此属于像素的区域.具有相同ID的像素表示属于同一区域.

We can see that there are two separate islands that I created on the top left and bottom right corner. Once you run the label function, it returns a label matrix identifying regions of pixels that belong to each other. Pixels that have the same ID mean that the belong to the same region.

为了向您展示连通性是如何发挥作用的,这是另一个简单的示例:

To show you how the connectivity comes into play, here's another simple example:

In [1]: import numpy as np

In [2]: from skimage.measure import label

In [3]: y = np.array([[0,1,0,0],[1,1,1,0],[0,1,0,1]])

In [4]: y
Out[4]:
array([[0, 1, 0, 0],
       [1, 1, 1, 0],
       [0, 1, 0, 1]])

In [5]: label(y, connectivity=1)
Out[5]:
array([[0, 1, 0, 0],
       [1, 1, 1, 0],
       [0, 1, 0, 2]], dtype=int64)

In [6]: label(y)
Out[6]:
array([[0, 1, 0, 0],
       [1, 1, 1, 0],
       [0, 1, 0, 1]], dtype=int64)

输入在左上角有一个十字图案,在右下角有一个单独的非零值.如果使用4连通性,则右下角将被分类为不同的标签,但是如果使用默认连通性(完全),则每个像素将被分类为相同的标签.

The input has a cross pattern on the top left corner and a separate non-zero value at the bottom right corner. If we use 4-connectivity, the bottom right corner would be classified as a different label but if we use the default connectivity (full), every pixel would be classified as the same label.

这篇关于numpy/python中的Floodlight分割图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆