From eb920b7fecba5888144afc0937f923c6ee3b3d32 Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Mon, 13 May 2024 12:34:36 +0800 Subject: [PATCH] Add: [ALAS] Button.match_template_binary() --- module/base/button.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/module/base/button.py b/module/base/button.py index cae5fc18e..153df61df 100644 --- a/module/base/button.py +++ b/module/base/button.py @@ -55,8 +55,13 @@ class Button(Resource): def image(self): return load_image(self.file, self.area) + @cached_property + def image_binary(self): + return rgb2gray(self.image) + def resource_release(self): del_cached_property(self, 'image') + del_cached_property(self, 'image_binary') self.clear_offset() def __str__(self): @@ -113,6 +118,29 @@ class Button(Resource): self._button_offset = np.array(point) + self.search[:2] - self.area[:2] return sim > similarity + def match_template_binary(self, image, similarity=0.85, direct_match=False) -> bool: + """ + Detects assets by template matching. + + To Some buttons, its location may not be static, `_button_offset` will be set. + + Args: + image: Screenshot. + similarity (float): 0-1. + direct_match: True to ignore `self.search` + + Returns: + bool. + """ + if not direct_match: + image = crop(image, self.search, copy=False) + image = rgb2gray(image) + res = cv2.matchTemplate(self.image_binary, image, cv2.TM_CCOEFF_NORMED) + _, sim, _, point = cv2.minMaxLoc(res) + + self._button_offset = np.array(point) + self.search[:2] - self.area[:2] + return sim > similarity + def match_multi_template(self, image, similarity=0.85, direct_match=False): """ Detects assets by template matching, return multiple reults @@ -225,6 +253,13 @@ class ButtonWrapper(Resource): return True return False + def match_template_binary(self, image, similarity=0.85, direct_match=False) -> bool: + for assets in self.buttons: + if assets.match_template_binary(image, similarity=similarity, direct_match=direct_match): + self._matched_button = assets + return True + return False + def match_multi_template(self, image, similarity=0.85, threshold=5, direct_match=False): """ Detects assets by template matching, return multiple results