I have 2D numpy array which is a mask from an image. Each cell has 0
or 1
value. So I would like to find top:left,right, bottom:left,right in an array
Use transpose
and nonzero
from numpy
, like:
im=np.array([[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,0,0],
[0,0,0,0,0]])
print(np.transpose(np.nonzero(im)))
yields:
array([[1, 1],
[1, 2],
[1, 3],
[2, 1],
[2, 2]])
Update:
Still not perfect, but as long as the mask is continuous within its rows, you could evaluate np.diff()
to get an idea where the 0->1
and 1->0
transitions are:
leftedge=np.transpose(np.nonzero(np.diff(im,prepend=0)==1))
rightedge=np.transpose(np.nonzero(np.diff(im,append=0)==-1))
top_left = leftedge[0]
bottom_left = leftedge[-1]
bottom_right = rightedge[-1]
top_right = rightedge[0]
pts=[list(x) for x in [top_left,top_right,bottom_left,bottom_right]]
yields: [[1, 1], [1, 3], [2, 1], [2, 2]]
I'd suggest to use Chris' answer instead.
xy=np.array([[0,0,0,0,0],[0,1,1,1,0],[0,1,1,0,0],[0,0,0,0,0]])
x,y=np.where(xy==1)
tl_i=np.argmin(x)
tl=[x[tl_i],y[tl_i]]
tr_i=np.argmax(y)
tr=[x[tr_i],y[tr_i]]
bl_i=np.argmax(x)
bl=[x[bl_i],y[bl_i]]
br_i=len(x)-1-np.argmax(np.flip(x))
br=[x[br_i],y[br_i]]
Using np.argwhere
and itertools.product
:
import numpy as np
from itertools import product
def corners(np_array):
ind = np.argwhere(np_array)
res = []
for f1, f2 in product([min,max], repeat=2):
res.append(f1(ind[ind[:, 0] == f2(ind[:, 0])], key=lambda x:x[1]))
return res
corners(arr)
Output:
[array([1, 1], dtype=int64),
array([2, 1], dtype=int64),
array([1, 3], dtype=int64),
array([2, 2], dtype=int64)]