Removes small objects from the predicted binary mask.
Args: pred_mask: The (already binary) predicted mask to clean up. remove_size: Absolute reference object size in voxels (required, > 0). Objects smaller than remove_size * percentage are removed. percentage: Fraction of remove_size used as the cutoff. Defaults to 0.2. verbose: If True, print the number of components. Defaults to False.
Returns: The processed mask with small objects removed.
from fastcore.test import test_eq, test_fail# large 4x4x4 (64 vox) + small 2x2x2 (8 vox) disjoint cubes_m = np.zeros((10, 10, 10), dtype=np.uint8)_m[1:5, 1:5, 1:5] =1_m[7:9, 7:9, 7:9] =1# cutoff 64*0.2=12.8 removes the small cube, keeps the large_out = refine_binary_pred_mask(_m, remove_size=64, percentage=0.2)test_eq(float(_out.sum()), 64.)test_eq(float(_out[7:9, 7:9, 7:9].sum()), 0.)# cutoff 8*0.2=1.6 keeps both cubes_out2 = refine_binary_pred_mask(_m, remove_size=8, percentage=0.2)test_eq(float(_out2.sum()), 72.)# empty mask returns zeros_empty = np.zeros((4, 4, 4), dtype=np.uint8)test_eq(float(refine_binary_pred_mask(_empty, remove_size=8).sum()), 0.)# remove_size is required and must be positivetest_fail(lambda: refine_binary_pred_mask(_m))test_fail(lambda: refine_binary_pred_mask(_m, remove_size=None))test_fail(lambda: refine_binary_pred_mask(_m, remove_size=0))
Parameter `min_size` is deprecated since version 0.26.0 and will be removed in 2.0.0 (or later). To avoid this warning, please use the parameter `max_size` instead. For more details, see the documentation of `remove_small_objects`. Note that the new threshold removes objects smaller than **or equal to** its value, while the previous parameter only removed smaller ones.