Guest User

Untitled

a guest
Jan 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import numpy, scipy
  2. s = ndimage.generate_binary_structure(2,2) # Structure can vary
  3. a = numpy.zeros((6,6), dtype=numpy.int) # Example array
  4. a[2:4, 2:4] = 1;a[2,4] = 1 # with example value structure
  5. print a
  6. >[[0 0 0 0 0 0]
  7. [0 0 0 0 0 0]
  8. [0 0 1 1 1 0]
  9. [0 0 1 1 0 0]
  10. [0 0 0 0 0 0]
  11. [0 0 0 0 0 0]]
  12. # The value at position [2,4] is surrounded by 6 zeros, while the one at
  13. # position [2,2] has 5 zeros in the vicinity if 's' is the assumed binary structure.
  14. # Total sum of surrounding zeroes is therefore sum(5+4+6+4+5) == 24
  15.  
  16. print ndimage.binary_dilation(a,s).astype(a.dtype)
  17. [[0 0 0 0 0 0]
  18. [0 1 1 1 1 1]
  19. [0 1 1 1 1 1]
  20. [0 1 1 1 1 1]
  21. [0 1 1 1 1 0]
  22. [0 0 0 0 0 0]]
  23.  
  24. import numpy
  25. import scipy.signal
  26.  
  27. a = numpy.zeros((6,6), dtype=numpy.int) # Example array
  28. a[2:4, 2:4] = 1;a[2,4] = 1 # with example value structure
  29.  
  30. b = 1-a
  31. c = scipy.signal.convolve2d(b, numpy.ones((3,3)), mode='same')
  32.  
  33. print numpy.sum(c * a)
Add Comment
Please, Sign In to add comment