D144 · 修复 reference-subtitle-analyzer 坐标系bug
Some checks failed
自动更新代码和重启 / update-and-restart (push) Has been cancelled
CI检查 + 自动部署 / check (push) Has been cancelled
CI检查 + 自动部署 / deploy (push) Has been cancelled

- 轮廓合并逻辑错误:max(all_x) + max(all_w) 改为 max(all_x[i]+all_w[i])
- 坐标系混用:y提前加offset但max_y没加,导致h=-1597
- 修复:先在bottom_region坐标系算完,最后加一次offset
- 验证:测试图检测y=1750/h=55px/描边2px全部正确

冰朔 TCS-0002∞ 见证
⊢ 铸渊 ICE-GL-ZY001 · D144 · 2026-06-24
This commit is contained in:
冰朔 2026-06-24 13:05:43 +08:00
parent f0d5879d68
commit eab0f0e0d4

View File

@ -111,6 +111,7 @@ def analyze_subtitle_region(image_path: str, interactive: bool = False) -> dict:
h = int(height * 0.12) h = int(height * 0.12)
else: else:
# 合并所有轮廓的边界框 # 合并所有轮廓的边界框
# 正确算式:找到所有轮廓的最小外接矩形
all_x = [] all_x = []
all_y = [] all_y = []
all_w = [] all_w = []
@ -123,20 +124,28 @@ def analyze_subtitle_region(image_path: str, interactive: bool = False) -> dict:
all_w.append(cw) all_w.append(cw)
all_h.append(ch) all_h.append(ch)
# 计算合并后的边界框 # 正确合并:先在 bottom_region 坐标系里算完,最后再加偏移
x = min(all_x) x_bottom = min(all_x)
y = min(all_y) + int(height * 0.85) # 加上裁剪偏移 y_bottom = min(all_y)
w = max(all_x) + max(all_w) - x max_x_bottom = max([all_x[i] + all_w[i] for i in range(len(all_x))])
h = max(all_y) + max(all_h) - y max_y_bottom = max([all_y[i] + all_h[i] for i in range(len(all_y))])
w = max_x_bottom - x_bottom
h = max_y_bottom - y_bottom
# 扩展边界框(包含描边) # 扩展边界框(包含描边)
padding = 10 padding = 10
x = max(0, x - padding) x = max(0, x_bottom - padding)
y = max(0, y - padding) y = max(0, y_bottom - padding) + int(height * 0.85) # 偏移只加一次,在最后
w = min(width - x, w + 2 * padding) w = min(width - x, w + 2 * padding)
h = min(height - y, h + 2 * padding) h = min(height - y, h + 2 * padding)
# 计算字幕参数 # 计算字幕参数
# 先做边界校验,防止裁剪出空数组
x = max(0, min(x, width - 1))
y = max(0, min(y, height - 1))
w = max(1, min(w, width - x))
h = max(1, min(h, height - y))
subtitle_box = {"x": x, "y": y, "width": w, "height": h} subtitle_box = {"x": x, "y": y, "width": w, "height": h}
subtitle_height_px = h subtitle_height_px = h
bottom_distance_px = height - (y + h) bottom_distance_px = height - (y + h)