import cv2 import sys img = cv2.imread(sys.argv[1] if len(sys.argv) > 1 else "_capture.jpg") if img is None: print("找不到图片") sys.exit(1) H, W = img.shape[:2] points = [] labels = ["A 近左", "B 近右", "C 远左", "D 远右"] def on_click(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN and len(points) < 4: points.append((x, y)) cv2.circle(img, (x, y), 4, (0, 0, 255), -1) cv2.putText(img, labels[len(points)-1], (x+8, y-8), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1) print(f" {labels[len(points)-1]}: ({x:4d}, {y:4d})") if len(points) == 4: print("\n--- 标定数据 ---") for i, p in enumerate(points): print(f"{labels[i]}: ({p[0]:4d}, {p[1]:4d})") # 换算到 80x60 sx, sy = 80/W, 60/H print(f"\n80x60 处理分辨率:") for i, p in enumerate(points): print(f"{labels[i]}: ({int(p[0]*sx):3d}, {int(p[1]*sy):3d})") print("\n按任意键退出...") cv2.namedWindow("calib", cv2.WINDOW_NORMAL) cv2.setMouseCallback("calib", on_click) print(f"图片 {W}x{H},按 A B C D 顺序点击 4 个点:") print(" A=近处左边界 B=近处右边界 C=远处左边界 D=远处右边界\n") while True: cv2.imshow("calib", img) if cv2.waitKey(20) != -1 or len(points) >= 4: break cv2.waitKey(0) cv2.destroyAllWindows()