<div dir="ltr">The other day I found myself finding trailing edges in binary images doing something like this:<div><br></div><div>arr = np.random.randint(2, size=1000).astype(np.int8)</div><div style>pattern = np.array([1, 1, 1, 1, 0, 0])</div>
<div style>arr_match = 2*arr - 1</div><div style>pat_match = 2*pattern - 1</div><div>from numpy.lib.stride_tricks import as_strided</div><div>arr_win = as_strided(arr_match, shape=arr.shape[:-1] + (arr.shape[-1]-len(pattern)+1, len(pattern)), strides=arr.strides+arr.strides[-1:])</div>
<div>matches = np.einsum('...i, i', arr_win, pat_match) == len(pattern)<br clear="all"><div><br></div><div style>While this works fine, this led me to thinking that all this functions (inner, dot, einsum, tensordot...) could be generalized to any other ufuncs apart from a pointwise np.multiply followed by an np.add reduction.</div>
<div style><br></div><div style>It would be great if there was a np.gen_inner that allowed something like:</div><div style><br></div><div style>np.gen_inner(arr_win, pattern, pointwise=np.equal, reduce=np.logical_and)</div>
<div style><br></div><div style>I would like to think that such a generalization would be useful in other settings (although I can't think of any right now), and that it could find it's place in numpy, rather than in scipy.ndimage or the like. Does this make any sense? Is there any already existing way of doing this that I'm overlooking?</div>
<div style><br></div><div style>Jaime</div><div style><br></div>-- <br>(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
</div></div>