def recover_grid_structure(bboxes: List[List[float]]) -> List[Dict]:
# ...
# 1. 识别行分割线 (Y轴)
y_coords = []
for b in bboxes:
y_coords.append(b[1]) # top
y_coords.append(b[3]) # bottom
row_dividers = GridRecovery.find_grid_lines(y_coords, tolerance=5, min_support=2)
find_grid_lines 收集所有单元格的 top/bottom(或 left/right)坐标,聚类后生成网格线。min_support=2),就会产生一条网格线。 # 匹配行
matched_rows = []
for r in row_intervals:
inter_top = max(b_top, r["top"])
inter_bottom = min(b_bottom, r["bottom"])
inter_h = max(0, inter_bottom - inter_top)
if r["height"] > 0 and (inter_h / r["height"] > 0.5 or inter_h / b_h > 0.5):
matched_rows.append(r["index"])
inter_h / r["height"] > 0.5 或 inter_h / b_h > 0.5。 for cell in cells:
if cell["row"] < max_row:
row_occupied[cell["row"]] = True # 只标记起始行
if cell["col"] < max_col:
col_occupied[cell["col"]] = True # 只标记起始列
compress_grid 只标记单元格的起始行/列(cell["row"] 和 cell["col"])。假设有以下单元格:
如果网格线检测产生了行 0, 1, 2, 3, 4 的分割线:
compress_grid 会移除这些空行,最终得到紧凑的网格。
空行/空列主要来自:
compress_grid 会移除这些空行/空列,确保最终网格紧凑。