Просмотр исходного кода

fix(module): #431 Feature matching exception

huangrunheng 2 лет назад
Родитель
Сommit
cfb1a01444
2 измененных файлов с 42 добавлено и 0 удалено
  1. 3 0
      module/atom/image.py
  2. 39 0
      module/base/utils.py

+ 3 - 0
module/atom/image.py

@@ -9,6 +9,7 @@ from pathlib import Path
 
 from module.base.decorator import cached_property
 from module.logger import logger
+from module.base.utils import is_approx_rectangle
 
 class RuleImage:
 
@@ -261,6 +262,8 @@ class RuleImage:
                 self.roi_front[1] = dst[0, 0, 1] + self.roi_back[1]
                 if show:
                     cv2.polylines(source, [dst], isClosed=True, color=(0, 0, 255), thickness=2)
+                if not is_approx_rectangle(np.array([pos[0] for pos in dst])):
+                    result = False
         else:
             result = False
 

+ 39 - 0
module/base/utils.py

@@ -900,3 +900,42 @@ def load_module(moduleName :str, moduleFile :str):
     spec.loader.exec_module(module)
     sys.modules[moduleName] = module
     return module
+
+
+def angle(p1, p2, p3):
+    """计算三点形成的角度(单位:度)"""
+    v1 = p2 - p1
+    v2 = p3 - p2
+    cos_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
+    return np.degrees(np.arccos(np.clip(cos_angle, -1.0, 1.0)))  # 限制范围以防止浮点错误
+
+
+def is_approx_rectangle(points, tolerance=30):
+    """判断四个点是否近似于矩形
+    :param points: 坐上开始,逆时针
+    :param tolerance: 角度 不是弧度
+    """
+    if points.shape[0] != 4:
+        return False
+    # # 计算边长
+    # distances = [distance(points[i], points[j]) for i in range(4) for j in range(i + 1, 4)]
+    # distances.sort()
+    # # 最小边和最大边
+    # short_sides = distances[:4]
+    # long_sides = distances[4:]
+    # # 判断边长是否接近
+    # if not (np.isclose(short_sides[0], short_sides[1], atol=tolerance) and
+    #         np.isclose(long_sides[0], long_sides[1], atol=tolerance)):
+    #     return False
+
+    # 检查角度
+    angles = [
+        angle(points[0], points[1], points[2]),
+        angle(points[1], points[2], points[3]),
+        angle(points[2], points[3], points[0]),
+        angle(points[3], points[0], points[1])
+    ]
+
+    # 判断角度是否接近90度
+    return all(np.isclose(a, 90, atol=tolerance) for a in angles)
+