@@ -192,6 +192,8 @@ def generate(
192
192
erosion_kernel : Optional [Tuple [int , int ]] = None ,
193
193
mask_multiplier : int = 255 ,
194
194
unique : bool = True ,
195
+ min_size : int = 0 ,
196
+ max_size : int = None ,
195
197
** kwargs : Any ,
196
198
) -> List [Dict [str , Any ]]:
197
199
"""
@@ -215,6 +217,8 @@ def generate(
215
217
Defaults to True.
216
218
The unique value increases from 1 to the number of objects. The
217
219
larger the number, the larger the object area.
220
+ min_size (int): The minimum size of the object. Defaults to 0.
221
+ max_size (int): The maximum size of the object. Defaults to None.
218
222
**kwargs (Any): Additional keyword arguments.
219
223
220
224
Returns:
@@ -241,11 +245,20 @@ def generate(
241
245
mask_generator = self .mask_generator # The automatic mask generator
242
246
masks = mask_generator .generate (image ) # Segment the input image
243
247
self .masks = masks # Store the masks as a list of dictionaries
248
+ self ._min_size = min_size
249
+ self ._max_size = max_size
244
250
245
251
if output is not None :
246
252
# Save the masks to the output path. The output is either a binary mask or a mask of objects with unique values.
247
253
self .save_masks (
248
- output , foreground , unique , erosion_kernel , mask_multiplier , ** kwargs
254
+ output ,
255
+ foreground ,
256
+ unique ,
257
+ erosion_kernel ,
258
+ mask_multiplier ,
259
+ min_size ,
260
+ max_size ,
261
+ ** kwargs ,
249
262
)
250
263
251
264
def save_masks (
@@ -255,6 +268,8 @@ def save_masks(
255
268
unique : bool = True ,
256
269
erosion_kernel : Optional [Tuple [int , int ]] = None ,
257
270
mask_multiplier : int = 255 ,
271
+ min_size : int = 0 ,
272
+ max_size : int = None ,
258
273
** kwargs : Any ,
259
274
) -> None :
260
275
"""Save the masks to the output path. The output is either a binary mask
@@ -275,6 +290,9 @@ def save_masks(
275
290
mask, which is usually a binary mask [0, 1]. You can use this
276
291
parameter to scale the mask to a larger range, for example
277
292
[0, 255]. Defaults to 255.
293
+ min_size (int, optional): The minimum size of the object. Defaults to 0.
294
+ max_size (int, optional): The maximum size of the object. Defaults to None.
295
+ **kwargs: Additional keyword arguments for common.array_to_image().
278
296
"""
279
297
280
298
if self .masks is None :
@@ -307,6 +325,10 @@ def save_masks(
307
325
count = len (sorted_masks )
308
326
for index , ann in enumerate (sorted_masks ):
309
327
m = ann ["segmentation" ]
328
+ if min_size > 0 and ann ["area" ] < min_size :
329
+ continue
330
+ if max_size is not None and ann ["area" ] > max_size :
331
+ continue
310
332
objects [m ] = count - index
311
333
312
334
# Generate a binary mask
@@ -318,6 +340,10 @@ def save_masks(
318
340
resulting_borders = np .zeros ((h , w ), dtype = dtype )
319
341
320
342
for m in masks :
343
+ if min_size > 0 and m ["area" ] < min_size :
344
+ continue
345
+ if max_size is not None and m ["area" ] > max_size :
346
+ continue
321
347
mask = (m ["segmentation" ] > 0 ).astype (dtype )
322
348
resulting_mask += mask
323
349
@@ -415,6 +441,14 @@ def show_anns(
415
441
)
416
442
img [:, :, 3 ] = 0
417
443
for ann in sorted_anns :
444
+ if hasattr (self , "_min_size" ) and (ann ["area" ] < self ._min_size ):
445
+ continue
446
+ if (
447
+ hasattr (self , "_max_size" )
448
+ and isinstance (self ._max_size , int )
449
+ and ann ["area" ] > self ._max_size
450
+ ):
451
+ continue
418
452
m = ann ["segmentation" ]
419
453
color_mask = np .concatenate ([np .random .random (3 ), [alpha ]])
420
454
img [m ] = color_mask
0 commit comments