-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibration.py
More file actions
160 lines (136 loc) · 5.45 KB
/
Copy pathcalibration.py
File metadata and controls
160 lines (136 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import cv2
import numpy as np
import json
from config import Config
from hik_camera import HikCamera
# 新地图
# 因为opencv和dji的坐标系是不一样的,opencv是右手系,dji是左手系,所以五点标定点的y轴要反一下
calibrate_map_point = { # x, y, z
"left_buff" : (9.535-0.160, -(7.500-0.750), 0),
"right_buff" : (9.535-0.160, -(7.500+0.750), 0),
"self_tower" : (10.9945, -11.517, 1.331+0.400),
"enemy_base" : (25.504, -7.500, 1.043+0.200),
"enemy_tower" : (16.832, -3.6435, 1.331+0.400)
}#参数,std::unordered_map、boost.hana
# 旧地图点
# calibrate_map_point = {
# "left0" : (8.67 , -5.715, 0.120 + 0.3),
# "right0": (8.67 , -5.715 - 0.4, 0.120 + 0.3),
# "self_tower": (11.1865, -12.419, 1.003+0.118),
# "enemy_base": (26.153, -7.5, 1.043+0.2),
# "enemy_tower": (16.64, -2.4215, 1.331+0.118)
# }
current_point = 0
real_map_point = []
point_names = list(calibrate_map_point.keys())# for_each
display_frame = None
print("start calibrate...")
def click_callback(event, x, y, flags, param):
global current_point, real_map_point, display_frame
if event == cv2.EVENT_LBUTTONDOWN and current_point < 5:
cv2.circle(display_frame, (x, y), 5, (0, 0, 255), -1)
cv2.putText(display_frame, point_names[current_point], (x+10, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
real_map_point.append((x, y))
current_point += 1
# if display_frame is not None:
# temp_frame = display_frame.copy()
# draw_existing_points(temp_frame)
# cv2.imshow("calibrate", temp_frame)
cv2.imshow("calibrate", display_frame)#点击_回调函数
def draw_existing_points(display_frame):
if display_frame is not None:
for i, (x, y) in enumerate(real_map_point):# 元组对迭代/std::views::enumerate(c++20)/
cv2.circle(display_frame, (x, y), 5, (0, 0, 255), -1)
cv2.putText(display_frame, point_names[i], (x+10, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)#绘制函数
def finalize_calibrate():
cv2.destroyAllWindows()
if len(real_map_point) != 5:
print('需要5个标定点, 当前只有', len(real_map_point))
return#退出函数
# 标定
object_points = np.array([calibrate_map_point[name] for name in point_names], dtype=np.float32)
image_points = np.array(real_map_point, dtype=np.float32)
camera_matrix = Config.CAMERA_MATRIX
dist_coeffs = Config.DIST_COEFFS
success, rvec, tvec = cv2.solvePnP(object_points, image_points, camera_matrix, dist_coeffs, flags=cv2.SOLVEPNP_EPNP)
if not success:
print("求解pnp不成功")
return
calibrate_data = {
"rvec": rvec.tolist(),
"tvec": tvec.tolist(),
"real_points": {name: (int(p[0]), int(p[1])) for name, p in zip(point_names, real_map_point)}
}
with open("json/calibrate_result.json", "w") as f:
json.dump(calibrate_data, f, indent=4)
print("标定成功!结果已保存到 json/calibrate_result.json")
def calibrate_with_video(video_path):
global display_frame
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return
cv2.namedWindow("calibrate")
cv2.setMouseCallback("calibrate", click_callback)
try:
while cap.isOpened() and current_point < 5:
ret, frame = cap.read()
if not ret:
break
display_frame = frame.copy()
draw_existing_points()
cv2.imshow('calibrate', display_frame)
if current_point >= 5:
break
if cv2.waitKey(30) == 27: # ESC
break
finally:
cap.release()
finalize_calibrate()
def calibrate_with_hik_camera(camera_config):
global display_frame
camera = HikCamera(camera_config)
cv2.namedWindow('calibrate', cv2.WINDOW_NORMAL)
cv2.setMouseCallback('calibrate', click_callback)
try:
while current_point < 5:
frame = camera.get_latest_frame()
if frame is not None:
display_frame = frame.copy()
draw_existing_points()
cv2.imshow('calibrate', display_frame)
if current_point >= 5:
break
key = cv2.waitKey(1)
if key == 27: # ESC
break
except Exception as e:
print(f"相机标定出错: {str(e)}")
finally:
camera.stop()
finalize_calibrate()
def calibrate_with_image(image_path):
global display_frame
frame = cv2.imread(image_path)
if frame is None:
print(f"无法加载图片: {image_path}")
return
# frame = cv2.resize(frame, (1500, 1000))
cv2.namedWindow("calibrate", cv2.WINDOW_NORMAL)
cv2.setMouseCallback("calibrate", click_callback)
try:
display_frame = frame.copy()
draw_existing_points(display_frame)
cv2.imshow('calibrate', display_frame)
while current_point < 5:
key = cv2.waitKey(30)
if key == 27: # ESC
break
finally:
finalize_calibrate()
if __name__ == "__main__":
mode = Config.SELECT_MODE
if mode == 'test':
#calibrate_with_video(r"D:\fjut_radar\image\test20250512.mp4")
calibrate_with_image(Config.IMG_PATH)
elif mode == 'hik':
calibrate_with_hik_camera(Config.HIK_CONFIG)