将Python数组划分为由单个单元或更少单元连接的唯一区域?

时间:2015-03-25 00:52:48

标签: python arrays image-processing numpy scipy

我有一个numpy数组,我希望将其划分为具有唯一ID的离散区域,如下所示:

Example array

通常对于这样的事情,我会使用 scipy.ndimage.label 为离散blob生成唯一的id,但是在这种情况下我有几个非常大的连续区域,我也希望将它们分割成较小的独特区域,理想情况是当它们仅通过1个单元格的连接连接到它们的邻居时。为了说明,这是一个示例数组,我在运行 scipy.ndimage.label 时获得的结果,以及我想要的结果:

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

# Sample study area array
example_array = np.array([[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1],
                          [0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1],
                          [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
                          [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                          [1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1],
                          [1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1],
                          [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
                          [1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0],
                          [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0],
                          [1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

# Identify discrete regions and assign unique IDs
current_output, num_ids = ndimage.label(example_array, structure=np.ones((3,3)))

# Plot outputs
plt.imshow(example_array, cmap="spectral", interpolation='nearest')
plt.imshow(current_output, cmap="spectral", interpolation='nearest')

Outputs and expected outputs

细胞的具体编号和分裂并不重要;任何类似上面最后一个情节的东西都可以。到目前为止,我最好的尝试是使用形态学图像处理来首先侵蚀我的阵列,运行 scipy.ndimage.label 然后扩张,但这有消除所有单细胞区域或薄的不幸副作用线性特征(其中有很多)。

非常感谢任何想法!

1 个答案:

答案 0 :(得分:4)

最简单的方法是在SciPy标签之前应用以下预先存储的内核(2,2作为原点,“?”可以是0或1):

IF |? 1 ?| OR |? 0 ?| THEN origin(x,y) == 0
   |0 1 0|    |1 1 1|
   |? 1 ?|    |? 0 ?|

实现:

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

# Sample study area array
example_array = np.array([[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1],
                          [0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1],
                          [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
                          [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                          [1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1],
                          [1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1],
                          [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
                          [1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0],
                          [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0],
                          [1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

# Get array shape
row_count, column_count = example_array.shape

# Iterate through kernel origins
for (x,y), value in np.ndenumerate(example_array):

    # Skip first and last rows and columns because kernel would be out of bounds
    if not (x == 0 
         or x == row_count-1 
         or y == 0 
         or y == column_count-1):

        # Extract kernel
        kernel = example_array[x-1:x+2, y-1:y+2]

        # Apply IF |? 1 ?| OR |? 0 ?| THEN origin(x,y) == 0
        #          |0 1 0|    |1 1 1|
        #          |? 1 ?|    |? 0 ?|
        if ((kernel[1,0:3]).all() == 1 and kernel[0,1] == 0 and kernel[2,1] == 0 
            or (kernel[0:3,1]).all() == 1 and kernel[1,0] == 0 and kernel[1,2] == 0):
            example_array[x,y] = 0

# Identify discrete regions and assign unique IDs
current_output, num_ids = ndimage.label(example_array, structure=np.ones((3,3)))

# Plot outputs
plt.imshow(example_array, cmap="spectral", interpolation='nearest')
plt.imshow(current_output, cmap="spectral", interpolation='nearest')

结果: enter image description here