diff --git a/breizorro/breizorro.py b/breizorro/breizorro.py index ca6f2a0..2e86f56 100644 --- a/breizorro/breizorro.py +++ b/breizorro/breizorro.py @@ -154,6 +154,8 @@ def main(): help='If an island specified by coordinates does not exist, do not throw an error') parser.add_argument('--extract-islands', dest='extract_isl', metavar='N|COORD', type=str, nargs='+', help='List of islands to extract from input mask. e.g. --extract-islands 1 18 20 20h10m13s,14d15m20s') + parser.add_argument('--minimum-size', dest='minsize', type=int, + help='Remove islands that have areas fewer than or equal to the specified number of pixels') parser.add_argument('--make-binary', action="store_true", help='Replace all island numbers with 1') parser.add_argument('--invert', action="store_true", @@ -288,6 +290,14 @@ def load_fits_or_region(filename): new_mask_image[mask_image == isl] = isl mask_image = new_mask_image + if args.minsize: + LOGGER.info(f"Removing islands that occupy fewer than or equal to {args.minsize} pixels") + mask_image = mask_image != 0 + island_labels, num_features = label(mask_image) + island_areas = numpy.array(scipy.ndimage.sum(mask_image,island_labels, numpy.arange(island_labels.max()+1))) + min_mask = island_areas >= args.minsize + mask_image = min_mask[island_labels.ravel()].reshape(island_labels.shape) + if args.make_binary: LOGGER.info(f"Converting mask to binary") mask_image = mask_image!=0