???????
4 邻域就是上下左右找有没有连着的 1
8 邻域就是上下左右 + 4 个斜线找有没有连着的 1?
# label函数返回两个参数
# labeled_array:标记后的数组
# num_features:连通区域的数量
from scipy.ndimage import label
import numpy as np
a = np.array([[0,0,1,1,0,0],
[0,0,0,1,0,0],
[1,1,0,0,1,0],
[0,0,0,1,0,0]])
labeled_array, num_features = label(a)
默认以 4 邻域划分区域
from scipy.ndimage import label
import numpy as np
a = np.array([[0,0,1,1,0,0],
[0,0,0,1,0,0],
[1,1,0,0,1,0],
[0,0,0,1,0,0]])
labeled_array, num_features = label(a)
print(labeled_array)
'''
[[0 0 1 1 0 0]
[0 0 0 1 0 0]
[2 2 0 0 3 0]
[0 0 0 4 0 0]]
'''
print(num_features) # 4
# 默认是4邻域,即 stru=np.ones([2,2])
stru = np.ones([3,3]) # 修改为8邻域
labeled_array, num_features = label(a, stru)
print(labeled_array)
‘'‘
[[0 0 1 1 0 0]
[0 0 0 1 0 0]
[2 2 0 0 1 0]
[0 0 0 1 0 0]]
'‘'