Add: [ALAS] Button.match_template_binary()

This commit is contained in:
LmeSzinc 2024-05-13 12:34:36 +08:00
parent 092b2e60db
commit eb920b7fec

View File

@ -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