Reputation: 301
I have a 500,500,3
shaped numpy array containing repetitive integer values. I can use np.unique
to get unique integer values present in the said array (because they can be different for every such array).
Is there any way to split multiple masks in one line.
Here masks is a numpy array. It contains different repetitive integer values
ids = np.unique(masks) # ids = [0, 1, 2] for example
# currently doing this
mask0 = masks == ids[0]
mask1 = masks == ids[1]
mask2 = masks == ids[2]
Is there any single line method to get a set of all binary masks. For example something like.
all_masks = masks == ids[:] # for example
Upvotes: 0
Views: 861
Reputation: 2401
You can broadcast both arrays to a shape of (id, height, width, channels)
, and then check equality:
all_masks = (masks[np.newaxis] == ids[:, np.newaxis, np.newaxis, np.newaxis])
In the result, all_masks[i]
is the (height, width, channels)
binary mask for the i
th id.
Upvotes: 1