Reputation: 515
Opencv sometimes return a mask for filtering.
Give array A=[[1,2],[3,4],[5,6]]
and mask mask=[1,0,1]
How should I apply the mask to obtain [[1,2],[5,6]]
?
I tried A[mask==1]
but it says dimension not match.
np.where
and np.nonzero
seem not working too.
Edit:
Turns out A[mask==1]
works,
It is in the real case I faced that mask.shape
is (n,1)
but not (n,)
that extra 1 caused the trouble.
np.squeeze
solved the problem
Upvotes: 2
Views: 938
Reputation: 2702
Both
mask = mask.nonzero()
res = A[mask]
and
mask = mask.astype(bool)
res = A[mask]
should work!
Upvotes: 1
Reputation: 2503
Like this?
A = np.array([[1,2],[3,4],[5,6]])
mask = np.array[1,0,1])
>>> A[np.where(mask==1),:]
array([[[1, 2],
[5, 6]]])
Upvotes: 1