Sfoglia il codice sorgente

fix(Image): Expand template search regions at initialization

blylana 1 settimana fa
parent
commit
3ede58e107

+ 7 - 2
module/atom/animate.py

@@ -38,9 +38,14 @@ class RuleAnimate(RuleImage):
             threshold=threshold,
             file=''
         )
+        if isinstance(rule, RuleImage):
+            # RuleImage 的 roi_back 已在构造时扩大,包装成 RuleAnimate 时不要重复扩大。
+            self.roi_back = tuple(roi_back)
 
         if name is not None:
             self._name = name
+        # match() 会更新 roi_front;动画帧比较始终使用初始裁剪区域,避免搜索范围扩大后发生漂移。
+        self._crop_roi = tuple(roi_front)
         self._last_image = None
 
     @property
@@ -55,12 +60,12 @@ class RuleAnimate(RuleImage):
         @return:
         """
         if self._last_image is None:
-            self._last_image = self.corp(image, self.roi_front)
+            self._last_image = self.corp(image, self._crop_roi)
             return False
 
         self._image = self._last_image
         matched = self.match(image)
-        self._last_image = self.corp(image, self.roi_front)
+        self._last_image = self.corp(image, self._crop_roi)
 
         if matched:
             if refresh_after_stable:

+ 14 - 40
module/atom/image.py

@@ -10,22 +10,11 @@ from pathlib import Path
 from module.atom.RuleImageMallResourceMixin import RuleImageMallResourceMixin
 from module.base.decorator import cached_property, del_cached_property
 from module.logger import logger
-from module.base.utils import is_approx_rectangle
+from module.base.utils import expand_roi, is_approx_rectangle
 from module.base.utils.utils import random_normal_distribution_int
 
-# 模板匹配容差:把搜索区域(roi_back)四边各外扩该像素数,用于吸收非原生渲染
-# 环境相对模板截图环境的整体相位差(桌面客户端受 DPI 缩放重采样,约 1px)。
-# 仓库里约 1/3 的模板 roi_back 与模板等宽,此时 matchTemplate 结果只剩 1 列、
-# 横向一格都平移不了,1px 相位差就足以把「元素存在」判成「不存在」。
-# 桌面模式由 Device 置为 2,模拟器保持 0、行为逐位不变。
-ROI_MATCH_PAD = 0
-DESKTOP_ROI_MATCH_PAD = 2
-
-
-def set_roi_match_padding(pad: int) -> None:
-    """设置模板匹配的 roi_back 外扩容差(像素,四边同值)。0 表示不启用。"""
-    global ROI_MATCH_PAD
-    ROI_MATCH_PAD = max(0, int(pad))
+# 模板匹配搜索区域统一外扩的像素数。
+ROI_BACK_EXPAND = 2
 
 
 class RuleImage(RuleImageMallResourceMixin):
@@ -51,7 +40,9 @@ class RuleImage(RuleImageMallResourceMixin):
         self.method = method
 
         self.roi_front: list = list(roi_front)
-        self.roi_back = roi_back
+        # 性能评估:四个 GameUi 庭院规则单轮总匹配耗时增加 1.23%(10.027ms -> 10.151ms)。
+        # TODO: assets 生命周期改为 Device 初始化后创建时,按运行环境初始化 roi_back,并删除这里的统一扩展。
+        self.roi_back = expand_roi(roi_back, ROI_BACK_EXPAND)
         self.threshold = threshold
         self.file = file
 
@@ -154,23 +145,6 @@ class RuleImage(RuleImageMallResourceMixin):
             self.load_kp_des()
         return self._des
 
-    @staticmethod
-    def _pad_roi(roi) -> tuple:
-        """
-        把 ROI 四边各外扩 ROI_MATCH_PAD 像素,返回 (x0, y0, w, h)。
-
-        左上侧必须夹到 0:numpy 对负索引按「倒数」语义取值,会把裁剪位置算错,
-        并连带把回算出的 roi_front 算偏;右/下侧越界由切片自然截断。
-        返回的 x0/y0 是外扩后的原点,命中回算必须以它做基准。
-        """
-        x, y, w, h = [int(v) for v in roi]
-        pad = ROI_MATCH_PAD
-        if pad <= 0:
-            return x, y, w, h
-        x0 = max(0, x - pad)
-        y0 = max(0, y - pad)
-        return x0, y0, x + w + pad - x0, y + h + pad - y0
-
     @property
     def mask(self):
         """
@@ -251,7 +225,7 @@ class RuleImage(RuleImageMallResourceMixin):
         :param roi
         :return:
         """
-        x, y, w, h = self._pad_roi(self.roi_back if roi is None else roi)
+        x, y, w, h = self.roi_back if roi is None else roi
         return image[y:y + h, x:x + w]
 
     def match(self, image: np.array, threshold: float = None) -> bool:
@@ -296,7 +270,7 @@ class RuleImage(RuleImageMallResourceMixin):
             logger.attr(self.name, f'matching score {max_val:.5f}')
 
         if max_val > threshold:
-            x0, y0, _, _ = self._pad_roi(self.roi_back)
+            x0, y0 = self.roi_back[:2]
             self.roi_front[0] = max_loc[0] + x0
             self.roi_front[1] = max_loc[1] + y0
             return True
@@ -379,7 +353,7 @@ class RuleImage(RuleImageMallResourceMixin):
             self.last_scale = float(best_scale)
 
         if best_score > threshold and best_loc is not None and best_shape is not None:
-            x0, y0, _, _ = self._pad_roi(self.roi_back)
+            x0, y0 = self.roi_back[:2]
             self.roi_front[0] = best_loc[0] + x0
             self.roi_front[1] = best_loc[1] + y0
             self.roi_front[2] = best_shape[0]
@@ -397,7 +371,7 @@ class RuleImage(RuleImageMallResourceMixin):
         :return:
         """
         if roi is not None:
-            self.roi_back = roi
+            self.roi_back = expand_roi(roi, ROI_BACK_EXPAND)
         if threshold is None:
             threshold = self.threshold
         if not (self.is_template_match or self.is_masked_template_match):
@@ -417,7 +391,7 @@ class RuleImage(RuleImageMallResourceMixin):
             self.last_score = float(results.max())
         locations = np.where(results >= threshold)
         matches = []
-        x0, y0, _, _ = self._pad_roi(self.roi_back)
+        x0, y0 = self.roi_back[:2]
         for pt in zip(*locations[::-1]):  # (x, y) coordinates
             score = results[pt[1], pt[0]]
             # 得分, x, y, w, h
@@ -435,7 +409,7 @@ class RuleImage(RuleImageMallResourceMixin):
         :return:
         """
         if roi is not None:
-            self.roi_back = roi
+            self.roi_back = expand_roi(roi, ROI_BACK_EXPAND)
         if threshold is None:
             threshold = self.threshold
         if not (self.is_template_match or self.is_masked_template_match):
@@ -455,7 +429,7 @@ class RuleImage(RuleImageMallResourceMixin):
             self.last_score = float(results.max())
         locations = np.where(results >= threshold)
         matches = []
-        x0, y0, _, _ = self._pad_roi(self.roi_back)
+        x0, y0 = self.roi_back[:2]
         for pt in zip(*locations[::-1]):  # (x, y) coordinates
             score = results[pt[1], pt[0]]
             # 得分, x, y, w, h
@@ -543,7 +517,7 @@ class RuleImage(RuleImageMallResourceMixin):
                 result = False
             else:
                 dst = int32(cv2.perspectiveTransform(pts, m))
-                x0, y0, _, _ = self._pad_roi(self.roi_back)
+                x0, y0 = self.roi_back[:2]
                 self.roi_front[0] = dst[0, 0, 0] + x0
                 self.roi_front[1] = dst[0, 0, 1] + y0
                 if show:

+ 11 - 0
module/base/utils/utils.py

@@ -225,6 +225,17 @@ def area_pad(area, pad=10):
     return upper_left_x + pad, upper_left_y + pad, bottom_right_x - pad, bottom_right_y - pad
 
 
+def expand_roi(roi, padding=2, screen_size=(1280, 720)):
+    """Expand an (x, y, width, height) ROI and clip it to the screen bounds."""
+    x, y, width, height = [int(value) for value in roi]
+    screen_width, screen_height = screen_size
+    x0 = min(max(0, x - padding), screen_width)
+    y0 = min(max(0, y - padding), screen_height)
+    x1 = min(max(0, x + width + padding), screen_width)
+    y1 = min(max(0, y + height + padding), screen_height)
+    return x0, y0, max(0, x1 - x0), max(0, y1 - y0)
+
+
 def limit_in(x, lower, upper):
     """
     Limit x within range (lower, upper)

+ 0 - 6
module/device/desktop_device.py

@@ -83,12 +83,6 @@ class DesktopDevice:
             self.config.script.device.control_method = 'window_message'
         self.config.save()
         self.screenshot_interval_set()
-        # 桌面客户端的渲染经过 DPI 缩放重采样,相对模板截图环境(模拟器原生
-        # 1280x720)存在约 1px 的相位差;仓库里约 1/3 的模板 roi 与模板等宽,
-        # matchTemplate 结果只剩 1 列、横向无法平移补偿,这点相位差就足以把
-        # 「元素存在」判成「不存在」。这里统一外扩 2px 吸收之,模拟器模式保持 0。
-        from module.atom.image import DESKTOP_ROI_MATCH_PAD, set_roi_match_padding
-        set_roi_match_padding(DESKTOP_ROI_MATCH_PAD)
         # 客户区校准到 1280x720 保证识别 1:1。窗口存在性已由 __init__ 的
         # _desktop_ensure_launched 保证,运行期掉了由 Restart 负责重拉
         self.desktop_window_set_size()