|
|
@@ -11,8 +11,9 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
|
|
from default_config import *
|
|
|
|
|
|
+visualization = True
|
|
|
|
|
|
-
|
|
|
+# Tải dữ liệu hiệu chuẩn camera, phù hợp với độ phân giải ban đầu
|
|
|
calib_data_path = f"camera_calibration/calib_data/{set_resolution}/MultiMatrix.npz"
|
|
|
calib_data = np.load(calib_data_path)
|
|
|
print('resolution:', set_resolution)
|
|
|
@@ -28,7 +29,7 @@ cap = cv2.VideoCapture(0)
|
|
|
|
|
|
# Thay đổi độ phân giải phù hợp với yêu cầu ban đầu
|
|
|
new_height = RESOLUTION_STANDARDS[set_resolution]
|
|
|
-new_width, new_height = change_resolution(cap,new_height)
|
|
|
+new_width, new_height = change_resolution(cap,new_height)
|
|
|
print(f'Resolution camera {new_width}x{new_height}')
|
|
|
|
|
|
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
@@ -70,13 +71,14 @@ while True:
|
|
|
total_markers = range(0, marker_IDs.size)
|
|
|
markers = {}
|
|
|
for ids, corners, i in zip(marker_IDs, marker_corners, total_markers):
|
|
|
+
|
|
|
corners = corners.reshape(4, 2)
|
|
|
corners = corners.astype(int)
|
|
|
top_right = corners[0].ravel()
|
|
|
top_left = corners[1].ravel()
|
|
|
bottom_right = corners[2].ravel()
|
|
|
bottom_left = corners[3].ravel()
|
|
|
-
|
|
|
+
|
|
|
# Chuyển đổi rVec thành ma trận quay R
|
|
|
R, _ = cv2.Rodrigues(rVec[i])
|
|
|
|
|
|
@@ -90,42 +92,54 @@ while True:
|
|
|
marker_points_camera = R @ object_points.T + tVec[i].reshape(3, 1)
|
|
|
marker_points_camera = marker_points_camera.T
|
|
|
|
|
|
- distance = np.sqrt(
|
|
|
- tVec[i][0][2] ** 2 + tVec[i][0][0] ** 2 + tVec[i][0][1] ** 2
|
|
|
- )
|
|
|
+ distance = np.linalg.norm(tVec[i][0])
|
|
|
+
|
|
|
# Draw the pose of the marker
|
|
|
- # point = cv2.drawFrameAxes(frame, cam_mat, dist_coef, rVec[i], tVec[i], 4, 4)
|
|
|
- x = -tVec[i][0][0]
|
|
|
- y = tVec[i][0][1]
|
|
|
- z = tVec[i][0][2]
|
|
|
- pos = np.array([x, y, z])
|
|
|
+ point = cv2.drawFrameAxes(frame, cam_mat, dist_coef, rVec[i], tVec[i], 4, 4)
|
|
|
+
|
|
|
+
|
|
|
+ R, _ = cv2.Rodrigues(rVec[i]) # Chuyển rVec sang ma trận quay
|
|
|
+ R_inv = np.linalg.inv(R) # Lấy nghịch đảo của ma trận quay (tức là R^T nếu R là trực giao)
|
|
|
+ position = - R_inv @ tVec[i].reshape(3, 1) # Tính vị trí camera trong hệ marker
|
|
|
+ position = position.reshape(3,)
|
|
|
+ # position += default_positions[ids[0]]
|
|
|
+
|
|
|
angles = rotationMatrixEulerAngles(R.T)
|
|
|
- if ids == 2:
|
|
|
- markers['marker 2'] = np.hstack((pos,angles))
|
|
|
- if ids == 0:
|
|
|
- pos += np.array([-12.3, 9.1, 0])
|
|
|
- markers['marker 0'] = np.hstack((pos,angles))
|
|
|
- if ids == 1:
|
|
|
- pos += np.array([12.3, 9.1, 0.])
|
|
|
- markers['marker 1'] = np.hstack((pos,angles))
|
|
|
- if ids == 3:
|
|
|
- pos += np.array([-7.2, -9.1, 0.])
|
|
|
- markers['marker 3'] = np.hstack((pos,angles))
|
|
|
- if ids == 4:
|
|
|
- pos += np.array([12.3, -5., 0.])
|
|
|
- markers['marker 4'] = np.hstack((pos,angles))
|
|
|
- if 'marker 2' in markers.keys():
|
|
|
- x, y, z, roll, pitch, yaw = np.round(markers['marker 2'], 1)
|
|
|
- else:
|
|
|
- cam_position = np.array([0., 0., 0., 0., 0., 0.])
|
|
|
- for v in markers.values():
|
|
|
- cam_position += v
|
|
|
- cam_position /= len(markers)
|
|
|
- x, y, z, roll, pitch, yaw = np.round(cam_position, 1)
|
|
|
- print(x, y, z, roll, pitch, yaw)
|
|
|
- # cv2.imshow("frame", frame)
|
|
|
- key = cv2.waitKey(1)
|
|
|
- if key == ord("q"):
|
|
|
- break
|
|
|
+ markers[f'marker {ids[0]}'] = np.hstack((position, angles))
|
|
|
+
|
|
|
+ if visualization:
|
|
|
+ cv2.putText(
|
|
|
+ frame,
|
|
|
+ f"id: {ids[0]}",
|
|
|
+ top_right,
|
|
|
+ cv2.FONT_HERSHEY_PLAIN,
|
|
|
+ 1.3,
|
|
|
+ (0, 0, 255),
|
|
|
+ 2,
|
|
|
+ cv2.LINE_AA,
|
|
|
+ )
|
|
|
+ if ids[0] == 3:
|
|
|
+ position += np.array([2.5, 2.5, 0])
|
|
|
+ if ids[0] == 4:
|
|
|
+ position += np.array([-2.5, 12.8, 0.])
|
|
|
+
|
|
|
+ # Nếu marker trung tâm (marker 0) xuất hiện thì chỉ cần lấy vị trí so với marker này
|
|
|
+ # if 'marker 0' in markers.keys():
|
|
|
+ # cam_position = markers['marker 0']
|
|
|
+ # # Nếu không, xét trung bình các marker
|
|
|
+ # else:
|
|
|
+ # cam_position = np.array([0., 0., 0., 0., 0., 0.])
|
|
|
+ # for v in markers.values():
|
|
|
+ # cam_position += v
|
|
|
+ # cam_position /= len(markers)
|
|
|
+
|
|
|
+ print(','.join(f'{i:.1f}' for i in position.tolist()))
|
|
|
+ if visualization:
|
|
|
+ cv2.imshow("frame", frame)
|
|
|
+ key = cv2.waitKey(1)
|
|
|
+ if key == ord("q"):
|
|
|
+ break
|
|
|
cap.release()
|
|
|
-cv2.destroyAllWindows()
|
|
|
+
|
|
|
+if visualization:
|
|
|
+ cv2.destroyAllWindows()
|