From 1f4aaf5728db460c9052209105879d68b8ef4b59 Mon Sep 17 00:00:00 2001 From: 283375 Date: Wed, 13 Sep 2023 03:15:34 +0800 Subject: [PATCH] impr: `crop_black_edges` --- src/arcaea_offline_ocr/crop.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/arcaea_offline_ocr/crop.py b/src/arcaea_offline_ocr/crop.py index 59b663f..a65b6ea 100644 --- a/src/arcaea_offline_ocr/crop.py +++ b/src/arcaea_offline_ocr/crop.py @@ -13,19 +13,17 @@ def crop_xywh(mat: Mat, rect: Tuple[int, int, int, int]): return mat[y : y + h, x : x + w] -def is_black_edge(list_of_pixels: Mat, black_pixel=None): - if black_pixel is None: - black_pixel = np.array([0, 0, 0], list_of_pixels.dtype) +def is_black_edge(list_of_pixels: Mat, black_pixel: Mat, ratio: float = 0.6): pixels = list_of_pixels.reshape([-1, 3]) return np.count_nonzero(np.all(pixels < black_pixel, axis=1)) > floor( - len(pixels) * 0.6 + len(pixels) * ratio ) -def crop_black_edges(screenshot: Mat): - cropped = screenshot.copy() - black_pixel = np.array([50, 50, 50], screenshot.dtype) - height, width = screenshot.shape[:2] +def crop_black_edges(img_bgr: Mat, black_threshold: int = 50): + cropped = img_bgr.copy() + black_pixel = np.array([black_threshold] * 3, img_bgr.dtype) + height, width = img_bgr.shape[:2] left = 0 right = width top = 0