真夜中を過ぎて、誰かが私の問題に取り組む方法を考えているかもしれません。隣接するセルの数 (配列値の近くにあるゼロなど、他の値を持つ配列フィールドの数を意味します) を有効な各値の合計としてカウントしたい! .
例:
import numpy, scipy
s = ndimage.generate_binary_structure(2,2) # Structure can vary
a = numpy.zeros((6,6), dtype=numpy.int) # Example array
a[2:4, 2:4] = 1;a[2,4] = 1 # with example value structure
print a
>[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 1 1 1 0]
[0 0 1 1 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
# The value at position [2,4] is surrounded by 6 zeros, while the one at
# position [2,2] has 5 zeros in the vicinity if 's' is the assumed binary structure.
# Total sum of surrounding zeroes is therefore sum(5+4+6+4+5) == 24
値の構造が異なる場合、どのようにゼロの数を数えることができますか? 値構造を拡大できる SciPy のbinary_dilation関数を使用する必要があると私は信じていますが、重複を単純に数えるだけでは正しい合計を導き出すことができませんか?
print ndimage.binary_dilation(a,s).astype(a.dtype)
[[0 0 0 0 0 0]
[0 1 1 1 1 1]
[0 1 1 1 1 1]
[0 1 1 1 1 1]
[0 1 1 1 1 0]
[0 0 0 0 0 0]]