|
@@ -195,6 +195,39 @@ class RuleImage:
|
|
|
matches.append((score, x, y, mat.shape[1], mat.shape[0]))
|
|
matches.append((score, x, y, mat.shape[1], mat.shape[0]))
|
|
|
return matches
|
|
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:
|
|
def coord(self) -> tuple:
|
|
|
"""
|
|
"""
|