0

I'm working with the library MaskRCNN and i want to get the area of each mask. All the masks are an array (W x H) with values False or True. All the trues represent a mask and all the falses a background.

#mask 0
r['masks'][:,:,0] 
#output-->
   array([[False, False, False, ..., False, False, False],
   [False, False, False, ..., False, False, False],
   [False, False, False, ..., False, False, False],
   ...,
   [False, False, False, ..., False, False, False],
   [False, False, False, ..., False, False, False],
   [False, False, False, ..., False, False, False]])

How can i iterate over all and sum all the true occurrences, both in columns and rows?

This i suppose give me the area in pixels of the mask, which i can interpolate real area.

hopieman
  • 399
  • 7
  • 22

2 Answers2

1

Based on this previous question, you need first to flatten the numpy array and therefore apply the same idea, i.e.,

flat_r = numpy.flatten(r['masks'][:,:,0])

unique, counts = numpy.unique(flat_r, return_counts=True)

print(dict(zip(unique, counts))) 
# {False: 7, True: 4}
Claudio Busatto
  • 721
  • 7
  • 17
0

Claudio answer was good, but i found a simple way too.

true_occurrences= np.sum(r['masks'][:,:,0])
hopieman
  • 399
  • 7
  • 22