合并相近的平行线段,减少冗余 Args: lines: 线段列表,格式为 [(y, x1, x2), ...] 或 [(x, y1, y2), ...] threshold: 合并阈值,位置差小于此值的线段会被合并 Returns: 合并后的线段列表
(lines, threshold=10)
| 706 | |
| 707 | # ======================== CV矩形检测优化辅助函数 ======================== |
| 708 | def _merge_nearby_lines(lines, threshold=10): |
| 709 | """ |
| 710 | 合并相近的平行线段,减少冗余 |
| 711 | |
| 712 | Args: |
| 713 | lines: 线段列表,格式为 [(y, x1, x2), ...] 或 [(x, y1, y2), ...] |
| 714 | threshold: 合并阈值,位置差小于此值的线段会被合并 |
| 715 | |
| 716 | Returns: |
| 717 | 合并后的线段列表 |
| 718 | """ |
| 719 | if not lines: |
| 720 | return [] |
| 721 | |
| 722 | merged = [] |
| 723 | used = set() |
| 724 | |
| 725 | for i, line in enumerate(lines): |
| 726 | if i in used: |
| 727 | continue |
| 728 | |
| 729 | pos, start, end = line # y/x, x1/y1, x2/y2 |
| 730 | # 找到所有相近的线段 |
| 731 | group_pos = [pos] |
| 732 | group_start = [start] |
| 733 | group_end = [end] |
| 734 | |
| 735 | for j, other in enumerate(lines[i+1:], i+1): |
| 736 | if j in used: |
| 737 | continue |
| 738 | o_pos, o_start, o_end = other |
| 739 | if abs(o_pos - pos) < threshold: |
| 740 | group_pos.append(o_pos) |
| 741 | group_start.append(o_start) |
| 742 | group_end.append(o_end) |
| 743 | used.add(j) |
| 744 | |
| 745 | # 合并为一条线 |
| 746 | merged.append(( |
| 747 | int(np.mean(group_pos)), |
| 748 | min(group_start), |
| 749 | max(group_end) |
| 750 | )) |
| 751 | used.add(i) |
| 752 | |
| 753 | return merged |
| 754 | |
| 755 | |
| 756 | # ======================== CV结果验证 ======================== |
no outgoing calls
no test coverage detected