mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-03-16 09:17:22 +08:00
Add: Binary template matching
Add match_binary() for binary template matching
This commit is contained in:
@@ -175,6 +175,57 @@ class Button(Resource):
|
|||||||
self._button_offset = area_offset(self._button, offset[:2] + np.array(point))
|
self._button_offset = area_offset(self._button, offset[:2] + np.array(point))
|
||||||
return similarity > threshold
|
return similarity > threshold
|
||||||
|
|
||||||
|
def match_binary(self, image, offset=30, threshold=0.85):
|
||||||
|
"""Detects button by template matching. To Some button, its location may not be static.
|
||||||
|
This method will apply template matching under binarization.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
image: Screenshot.
|
||||||
|
offset (int, tuple): Detection area offset.
|
||||||
|
threshold (float): 0-1. Similarity.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool.
|
||||||
|
"""
|
||||||
|
self.ensure_template()
|
||||||
|
|
||||||
|
if isinstance(offset, tuple):
|
||||||
|
if len(offset) == 2:
|
||||||
|
offset = np.array((-offset[0], -offset[1], offset[0], offset[1]))
|
||||||
|
else:
|
||||||
|
offset = np.array(offset)
|
||||||
|
else:
|
||||||
|
offset = np.array((-3, -offset, 3, offset))
|
||||||
|
image = crop(image, offset + self.area)
|
||||||
|
|
||||||
|
if self.is_gif:
|
||||||
|
for template in self.image:
|
||||||
|
# graying
|
||||||
|
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
|
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
|
||||||
|
# binarization
|
||||||
|
_, image_binary = cv2.threshold(image_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
||||||
|
_, template_binary = cv2.threshold(template_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
||||||
|
# template matching
|
||||||
|
res = cv2.matchTemplate(template_binary, image_binary, cv2.TM_CCOEFF_NORMED)
|
||||||
|
_, similarity, _, point = cv2.minMaxLoc(res)
|
||||||
|
self._button_offset = area_offset(self._button, offset[:2] + np.array(point))
|
||||||
|
if similarity > threshold:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
# graying
|
||||||
|
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
|
self_image_gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
|
||||||
|
# binarization
|
||||||
|
image_binary = cv2.threshold(image_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
||||||
|
self_image_binary = cv2.threshold(self_image_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
||||||
|
# template matching
|
||||||
|
res = cv2.matchTemplate(self_image_binary, image_binary, cv2.TM_CCOEFF_NORMED)
|
||||||
|
_, similarity, _, point = cv2.minMaxLoc(res)
|
||||||
|
self._button_offset = area_offset(self._button, offset[:2] + np.array(point))
|
||||||
|
return similarity > threshold
|
||||||
|
|
||||||
def match_appear_on(self, image, threshold=30):
|
def match_appear_on(self, image, threshold=30):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
Reference in New Issue
Block a user