Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- 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]]
- import numpy
- import scipy.signal
- a = numpy.zeros((6,6), dtype=numpy.int) # Example array
- a[2:4, 2:4] = 1;a[2,4] = 1 # with example value structure
- b = 1-a
- c = scipy.signal.convolve2d(b, numpy.ones((3,3)), mode='same')
- print numpy.sum(c * a)
Add Comment
Please, Sign In to add comment