|
@@ -104,11 +104,25 @@ def analyze_centering_rotated(inner_points: Union[str, List], outer_points: Unio
|
|
|
outer_rect = cv2.minAreaRect(outer_contour)
|
|
outer_rect = cv2.minAreaRect(outer_contour)
|
|
|
|
|
|
|
|
# 获取矩形的4个角点, 并转化为坐标
|
|
# 获取矩形的4个角点, 并转化为坐标
|
|
|
- outer_box_corners = cv2.boxPoints(outer_rect)
|
|
|
|
|
- outer_box_corners_int = np.intp(outer_box_corners).tolist()
|
|
|
|
|
-
|
|
|
|
|
- inner_box_corners = cv2.boxPoints(inner_rect)
|
|
|
|
|
- inner_box_corners_int = np.intp(inner_box_corners).tolist()
|
|
|
|
|
|
|
+ # outer_box_corners = cv2.boxPoints(outer_rect)
|
|
|
|
|
+ # outer_box_corners_int = np.intp(outer_box_corners).tolist()
|
|
|
|
|
+ #
|
|
|
|
|
+ # inner_box_corners = cv2.boxPoints(inner_rect)
|
|
|
|
|
+ # inner_box_corners_int = np.intp(inner_box_corners).tolist()
|
|
|
|
|
+ def get_rect(contour):
|
|
|
|
|
+ # 获取矩形框
|
|
|
|
|
+ x, y, w, h = cv2.boundingRect(contour)
|
|
|
|
|
+ # 2. 构造 4 个顶点坐标
|
|
|
|
|
+ box_corners_int = np.array([
|
|
|
|
|
+ [x, y], # 左上
|
|
|
|
|
+ [x + w, y], # 右上
|
|
|
|
|
+ [x + w, y + h], # 右下
|
|
|
|
|
+ [x, y + h] # 左下
|
|
|
|
|
+ ], dtype=np.int32).tolist()
|
|
|
|
|
+ return box_corners_int
|
|
|
|
|
+
|
|
|
|
|
+ inner_box_corners_int = get_rect(inner_contour)
|
|
|
|
|
+ outer_box_corners_int = get_rect(outer_contour)
|
|
|
|
|
|
|
|
# --- 1. 标准化矩形尺寸,确保宽度总是长边 ---
|
|
# --- 1. 标准化矩形尺寸,确保宽度总是长边 ---
|
|
|
def standardize_rect(rect):
|
|
def standardize_rect(rect):
|
|
@@ -194,6 +208,79 @@ def analyze_centering_rotated(inner_points: Union[str, List], outer_points: Unio
|
|
|
angle_diff), inner_box_corners_int, outer_box_corners_int
|
|
angle_diff), inner_box_corners_int, outer_box_corners_int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def analyze_centering_rect(inner_points: Union[str, List], outer_points: Union[str, List]):
|
|
|
|
|
+ """
|
|
|
|
|
+ 使用横平竖直的矩形 (Axis-Aligned Bounding Box) 进行居中分析。
|
|
|
|
|
+ 适用于前端已经修正过的矩形数据。
|
|
|
|
|
+ """
|
|
|
|
|
+ if isinstance(inner_points, str):
|
|
|
|
|
+ inner_points = get_points_from_file(inner_points)
|
|
|
|
|
+ if isinstance(outer_points, str):
|
|
|
|
|
+ outer_points = get_points_from_file(outer_points)
|
|
|
|
|
+
|
|
|
|
|
+ # 转换为 numpy 数组
|
|
|
|
|
+ inner_contour = np.array(inner_points, dtype=np.int32)
|
|
|
|
|
+ outer_contour = np.array(outer_points, dtype=np.int32)
|
|
|
|
|
+
|
|
|
|
|
+ # --- 1. 获取横平竖直的矩形参数 (x, y, w, h) ---
|
|
|
|
|
+ # 即使传入的是4个点,用 boundingRect 也能确保取出准确的边界
|
|
|
|
|
+ ix, iy, iw, ih = cv2.boundingRect(inner_contour)
|
|
|
|
|
+ ox, oy, ow, oh = cv2.boundingRect(outer_contour)
|
|
|
|
|
+
|
|
|
|
|
+ # 构造标准的4点格式返回 (保持和 analyze_centering_rotated 输出一致)
|
|
|
|
|
+ def get_box_corners(x, y, w, h):
|
|
|
|
|
+ return [[x, y], [x + w, y], [x + w, y + h], [x, y + h]]
|
|
|
|
|
+
|
|
|
|
|
+ inner_box_corners_int = get_box_corners(ix, iy, iw, ih)
|
|
|
|
|
+ outer_box_corners_int = get_box_corners(ox, oy, ow, oh)
|
|
|
|
|
+
|
|
|
|
|
+ print("\n--- 基于修正矩形(横平竖直)的分析 ---")
|
|
|
|
|
+ print(f"内框: x={ix}, y={iy}, w={iw}, h={ih}")
|
|
|
|
|
+ print(f"外框: x={ox}, y={oy}, w={ow}, h={oh}")
|
|
|
|
|
+
|
|
|
|
|
+ # --- 2. 计算边距 (Margins) ---
|
|
|
|
|
+ # 图像坐标系:x向右增加,y向下增加
|
|
|
|
|
+
|
|
|
|
|
+ # 左边距:内框左边 - 外框左边
|
|
|
|
|
+ margin_left = ix - ox
|
|
|
|
|
+ # 右边距:外框右边(x+w) - 内框右边(x+w)
|
|
|
|
|
+ margin_right = (ox + ow) - (ix + iw)
|
|
|
|
|
+
|
|
|
|
|
+ # 上边距:内框上边 - 外框上边
|
|
|
|
|
+ margin_top = iy - oy
|
|
|
|
|
+ # 下边距:外框下边(y+h) - 内框下边(y+h)
|
|
|
|
|
+ margin_bottom = (oy + oh) - (iy + ih)
|
|
|
|
|
+
|
|
|
|
|
+ # --- 3. 计算百分比 ---
|
|
|
|
|
+ total_horizontal_margin = margin_left + margin_right
|
|
|
|
|
+ total_vertical_margin = margin_top + margin_bottom
|
|
|
|
|
+
|
|
|
|
|
+ left_percent = 50.0
|
|
|
|
|
+ right_percent = 50.0
|
|
|
|
|
+ top_percent = 50.0
|
|
|
|
|
+ bottom_percent = 50.0
|
|
|
|
|
+
|
|
|
|
|
+ if total_horizontal_margin > 0:
|
|
|
|
|
+ left_percent = (margin_left / total_horizontal_margin) * 100
|
|
|
|
|
+ right_percent = (margin_right / total_horizontal_margin) * 100
|
|
|
|
|
+ print(f"水平边距分布: 左 {left_percent:.1f}% | 右 {right_percent:.1f}%")
|
|
|
|
|
+
|
|
|
|
|
+ if total_vertical_margin > 0:
|
|
|
|
|
+ top_percent = (margin_top / total_vertical_margin) * 100
|
|
|
|
|
+ bottom_percent = (margin_bottom / total_vertical_margin) * 100
|
|
|
|
|
+ print(f"垂直边距分布: 上 {top_percent:.1f}% | 下 {bottom_percent:.1f}%")
|
|
|
|
|
+
|
|
|
|
|
+ # --- 4. 角度差异 ---
|
|
|
|
|
+ # 因为已经是横平竖直的矩形,角度差默认为 0
|
|
|
|
|
+ angle_diff = 0.0
|
|
|
|
|
+
|
|
|
|
|
+ # 返回格式保持与原函数 analyze_centering_rotated 一致:
|
|
|
|
|
+ # ((左%, 右%), (上%, 下%), 角度差), 内框点集, 外框点集
|
|
|
|
|
+ return ((left_percent, right_percent),
|
|
|
|
|
+ (top_percent, bottom_percent),
|
|
|
|
|
+ angle_diff), inner_box_corners_int, outer_box_corners_int
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def formate_center_data(center_result,
|
|
def formate_center_data(center_result,
|
|
|
inner_data: dict, outer_data: dict,
|
|
inner_data: dict, outer_data: dict,
|
|
|
inner_rect_points: List, outer_rect_points: List):
|
|
inner_rect_points: List, outer_rect_points: List):
|