**Take Control**– Choose how to license your stuff and who can edit it, track every change Join Siafoo Now or Learn More

# Expand Mask (True Area of Boolean Numpy Array ) 0

In Brief | Expands a mask of Trues by one cell in both the horizontal and vertical directions, a given number of times. Useful for including everything in an unevenly-shaped region, plus a little bit around the edges.... more |

Language | Python |

# 's

` 1# Copyright 2007 Regents of the University of California`

2# Written by David Isaacson at the University of California, Davis

3# BSD License

4

5def ExpandMask(input, iters):

6 """

7 Expands the True area in an array 'input'.

8

9 Expansion occurs in the horizontal and vertical directions by one

10 cell, and is repeated 'iters' times.

11 """

12 yLen,xLen = input.shape

13 output = input.copy()

14 for iter in xrange(iters):

15 for y in xrange(yLen):

16 for x in xrange(xLen):

17 if (y > 0 and input[y-1,x]) or \

18 (y < yLen - 1 and input[y+1,x]) or \

19 (x > 0 and input[y,x-1]) or \

20 (x < xLen - 1 and input[y,x+1]): output[y,x] = True

21 input = output.copy()

22 return output

Expands a mask of Trues by one cell in both the horizontal and vertical directions, a given number of times. Useful for including everything in an unevenly-shaped region, plus a little bit around the edges.

Requires numpy.

Add a Comment