统一相似大小和类型的基本图形的边框厚度。 注意:参考 sam3_extractor.py 的简化逻辑,默认边框宽度为1, 这里主要用于确保同类元素风格一致。
(elements: list)
| 658 | |
| 659 | # ======================== 样式统一 ======================== |
| 660 | def unify_element_styles(elements: list) -> list: |
| 661 | """ |
| 662 | 统一相似大小和类型的基本图形的边框厚度。 |
| 663 | |
| 664 | 注意:参考 sam3_extractor.py 的简化逻辑,默认边框宽度为1, |
| 665 | 这里主要用于确保同类元素风格一致。 |
| 666 | """ |
| 667 | if not elements: |
| 668 | return elements |
| 669 | |
| 670 | groups = {} |
| 671 | |
| 672 | for i, elem in enumerate(elements): |
| 673 | shape_type = elem.get("_type", "rectangle") |
| 674 | bbox = elem["bbox"] |
| 675 | w = bbox[2] - bbox[0] |
| 676 | h = bbox[3] - bbox[1] |
| 677 | diag = math.sqrt(w**2 + h**2) |
| 678 | size_key = int(round(diag / 20)) |
| 679 | |
| 680 | key = (shape_type, size_key) |
| 681 | if key not in groups: |
| 682 | groups[key] = [] |
| 683 | groups[key].append(i) |
| 684 | |
| 685 | for key, indices in groups.items(): |
| 686 | if len(indices) < 2: |
| 687 | continue |
| 688 | |
| 689 | # 获取边框宽度,如果不存在则默认为1 |
| 690 | widths = [] |
| 691 | for i in indices: |
| 692 | style = elements[i].get("_style", {}) |
| 693 | widths.append(style.get("stroke_width", 1)) |
| 694 | |
| 695 | if not widths: |
| 696 | continue |
| 697 | median_width = int(np.median(widths)) |
| 698 | |
| 699 | for i in indices: |
| 700 | if "_style" not in elements[i]: |
| 701 | elements[i]["_style"] = {} |
| 702 | elements[i]["_style"]["stroke_width"] = median_width |
| 703 | |
| 704 | return elements |
| 705 | |
| 706 | |
| 707 | # ======================== CV矩形检测优化辅助函数 ======================== |
no outgoing calls
no test coverage detected