From 0aaee5eaeabd7083ce670c0fcef2208c2d74cf03 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 18 Aug 2024 06:07:51 -0700 Subject: [PATCH] Remove falsely keyed out noise as well --- chromakey.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/chromakey.py b/chromakey.py index 42d71d4..aed754a 100644 --- a/chromakey.py +++ b/chromakey.py @@ -62,11 +62,13 @@ def _islands(mask): def _filter_islands(mask, ids, min_object_pixels): values, counts = np.unique(ids.flatten(), return_counts=True) - ix = np.where(counts < min_object_pixels) - mask[np.isin(ids, values[ix])] = 0 + values_ix = np.where(counts < min_object_pixels) + mask_ix = np.isin(ids, values[values_ix]) + mask[mask_ix] = np.where(mask[mask_ix] == 255, 0, 255) def _flood_fill(ids, mask, i, j, val): + base_val = mask[i, j] queue = [(i, j)] while queue: @@ -75,7 +77,7 @@ def _flood_fill(ids, mask, i, j, val): if i < 0 or i >= mask.shape[0] or j < 0 or j >= mask.shape[1]: continue - if mask[i, j] == 0 or ids[i, j] != 0: + if mask[i, j] != base_val or ids[i, j] != 0: continue ids[i, j] = val @@ -107,4 +109,4 @@ filename = os.path.join(path, '41244/back.orf') with Image(filename=filename) as img: img.crop(left=0, top=0, width=4000, height=3000) chroma_key(img) - img.save(filename='chromakey.png') \ No newline at end of file + img.save(filename='output.png') \ No newline at end of file