Finding the overlap of numpy images

numpy, python

Solution

If, as in your example, the values in the arrays are either 0 or 1, you can use the bitwise "and" operator `&`:

In [3]: a
Out[3]: 
array([[0, 1, 1],
       [0, 1, 0],
       [0, 0, 0]])

In [4]: b
Out[4]: 
array([[0, 1, 1],
       [0, 0, 1],
       [0, 0, 0]])

In [5]: c = a & b

In [6]: c
Out[6]: 
array([[0, 1, 1],
       [0, 0, 0],
       [0, 0, 0]])

Problem

If I have two numpy arrays: ``` a = np.array[[0,1,1], [0,1,0], [0,0,0]] b = np.array[[0,1,1], [0,0,1], [0,0,0]] ``` How could I find the 'overlap' between them, so the output is: ``` c = [[0,1,1], [0,0,0], [0,0,0]] ``` I have this, but is there a way which could be quicker as my arrays are large? ``` c = a + b - 1 c[c<0] = 0 ```

Original source

Related problems