本人经常把四连通与八连通的作用搞混,所以在此用demo做个笔记。
from scipy.ndimage.measurements import label
from scipy.ndimage.morphology import generate_binary_structure
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(num_features)
print(labeled_array)
s = generate_binary_structure(2,2)
print(s)
labeled_array, num_features = label(a, structure=s)#八连通
print(num_features)
print(labeled_array)
output:
4
[[0 0 1 1 0 0]
[0 0 0 1 0 0]
[2 2 0 0 3 0]
[0 0 0 4 0 0]]
[[ True True True]
[ True True True]
[ True True True]]
2
[[0 0 1 1 0 0]
[0 0 0 1 0 0]
[2 2 0 0 1 0]
[0 0 0 1 0 0]]