@@ -263,16 +263,24 @@ def remove_false_positives_by_foreground_probability(segmentation: np.array,
263
263
instances , _ , _ = relabel_sequential (segmentation )
264
264
265
265
regions = regionprops (instances )
266
- for region in regions :
266
+ to_keep = np .ones (len (regions ) + 1 )
267
+ pixel_count = np .zeros (len (regions ) + 1 )
268
+ pixel_value = np .zeros (len (regions ) + 1 )
269
+
270
+ for region in tqdm .tqdm (regions ):
267
271
bbox = region .bbox
268
272
cube = instances [bbox [0 ]:bbox [3 ], bbox [1 ]:bbox [4 ], bbox [2 ]:bbox [5 ]] == region .label # other instances may exist, don't use `> 0`
269
273
prob = foreground [bbox [0 ]:bbox [3 ], bbox [1 ]:bbox [4 ], bbox [2 ]:bbox [5 ]]
270
- pixel_count = region .area
271
- pixel_value = (cube * prob ).sum ()
272
- likelihood = pixel_value / pixel_count
273
- if likelihood < threshold :
274
- instances [instances == region .label ] = 0
275
- print (f" Removing instance { region .label } : pixel count: { pixel_count } , pixel value: { pixel_value } , likelihood: { likelihood } " )
274
+ pixel_count [region .label ] = region .area
275
+ pixel_value [region .label ] = (cube * prob ).sum ()
276
+
277
+ likelihood = pixel_value / pixel_count
278
+ to_keep [likelihood < threshold ] = 0
279
+ ids_to_delete = np .argwhere (to_keep == 0 )
280
+ assert ids_to_delete .shape [1 ] == 1
281
+ ids_to_delete = ids_to_delete .flatten ()
282
+ # print(f" Removing instance {region.label}: pixel count: {pixel_count}, pixel value: {pixel_value}, likelihood: {likelihood}")
276
283
284
+ instances [np .isin (instances , ids_to_delete )] = 0
277
285
instances , _ , _ = relabel_sequential (instances )
278
286
return instances
0 commit comments