Browse Source

feat(image): 添加match_all_any方法用于返回所有匹配结果并去重

- 实现了match_all_any函数,支持返回所有匹配结果
- 添加了非极大值抑制(NMS)来去除冗余匹配项
- 支持通过阈值和ROI参数进行匹配过滤
- 集成了OpenCV的NMSBoxes方法优化匹配框选择
- 提供了得分、坐标及尺寸信息的完整匹配结果列表
- 增强了图像模板匹配功能的灵活性和准确性
ljxun 1 year ago
parent
commit
f88fe20b89
1 changed files with 33 additions and 0 deletions
  1. 33 0
      module/atom/image.py

+ 33 - 0
module/atom/image.py

@@ -195,6 +195,39 @@ class RuleImage:
             matches.append((score, x, y, mat.shape[1], mat.shape[0]))
         return matches
 
+    def match_all_any(self, image: np.array, threshold: float = None, roi: list = None, nms_threshold: float = 0.3) -> list[tuple]:
+        """
+        区别于match,这个是返回所有的匹配结果,去除冗余匹配项(例如:多个框选区域重叠的情况)时使用。
+        :param roi:
+        :param image:
+        :param threshold:
+        :return:
+        """
+        if roi is not None:
+            self.roi_back = roi
+        if threshold is None:
+            threshold = self.threshold
+        if not self.is_template_match:
+            raise Exception(f"unknown method {self.method}")
+        source = self.corp(image)
+        mat = self.image
+        results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
+        locations = np.where(results >= threshold)
+        matches = []
+        for pt in zip(*locations[::-1]):  # (x, y) coordinates
+            score = results[pt[1], pt[0]]
+            # 得分, x, y, w, h
+            x = self.roi_back[0] + pt[0]
+            y = self.roi_back[1] + pt[1]
+            matches.append((score, x, y, mat.shape[1], mat.shape[0]))
+        if len(matches) > 0:
+            scores = np.array([m[0] for m in matches])
+            boxes = np.array([[m[1], m[2], m[3], m[4]] for m in matches])
+            # 使用OpenCV的NMSBoxes
+            indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), score_threshold=threshold, nms_threshold=nms_threshold)
+            filtered_matches = [matches[i] for i in indices]
+            return filtered_matches
+        return matches
 
     def coord(self) -> tuple:
         """