| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- """在匹配图上绘制模板四角透视框(多边形)。"""
- from __future__ import annotations
- from pathlib import Path
- import cv2
- import numpy as np
- from routing import repo_path
- def draw_rect(
- corners_xy: np.ndarray | list,
- *,
- match_image: str | Path,
- output_path: str | Path,
- outline_color_bgr: tuple[int, int, int] = (0, 255, 0),
- outline_thickness_pixels: int = 6,
- ) -> Path:
- """
- ``corners_xy``:``(4, 2)`` 顶点顺序与 ``corner_points_on_match_img`` 一致(闭合四边形)。
- """
- scr = repo_path(match_image)
- bgr = cv2.imread(str(scr), cv2.IMREAD_COLOR)
- if bgr is None:
- raise FileNotFoundError(f"无法读取匹配图:{scr}")
- pts = np.asarray(corners_xy, dtype=np.float64).reshape(4, 2)
- if pts.shape != (4, 2) or not np.all(np.isfinite(pts)):
- raise ValueError("corners_xy 须为 (4, 2) 有限坐标")
- pi = pts.astype(np.int32).reshape(-1, 1, 2)
- cv2.polylines(
- bgr,
- [pi],
- isClosed=True,
- color=outline_color_bgr,
- thickness=int(outline_thickness_pixels),
- lineType=cv2.LINE_AA,
- )
- out = repo_path(output_path)
- out.parent.mkdir(parents=True, exist_ok=True)
- if not cv2.imwrite(str(out), bgr):
- raise OSError(f"无法写入:{out}")
- return out
|