...
/Find Distances Between the Camera and Markers
Find Distances Between the Camera and Markers
Learn how to find the distance between the camera and multiple markers.
We'll cover the following...
The first piece of the code below loads an image with ArUco markers, detects the ArUco markers in the image, and then calculates the distances from each marker to the camera’s center.
The second Python code will calculate the pairwise distances between detected ArUco markers in the image.
We’ll use an image that contains two Aruco markers. The centers of these markers are at a distance of approximately 19 cm from each other, and the camera was held approximately 60 cm away.
Calculating the distance from the camera to some markers
Here we’ll show an example of how to calculate the distance from a camera to some markers.
import cv2import numpy as npdef postprocessDistToCam(ids, tvec):if ids is not None:for i, tvec in enumerate(tvecs):marker_id = ids[i][0]distance = np.linalg.norm(tvec)print(f"Distance from marker {marker_id} to camera center: {distance:.2f} meters")image_file = './resources/marker_images_dist/calibrate_20230704_134548.png'image = cv2.imread(image_file)calibration_data_file = "./resources/calibration_data.npz"camera_matrix, distortion_coeffs = loadCalibrationData(calibration_data_file)height, width = image.shape[:2]new_camera_matrix, _ = cv2.getOptimalNewCameraMatrix(camera_matrix, distortion_coeffs, (width, height), 0, (width, height))image = cv2.undistort(image, camera_matrix, distortion_coeffs, None, new_camera_matrix)processed_image = preprocessImage(image)marker_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_5X5_250)detector_params = cv2.aruco.DetectorParameters_create()detections, ids = detectMarkers(processed_image, marker_dict, detector_params)marker_length = .05 #in metersrvecs, tvecs = poseEstimation(detections, marker_length, camera_matrix, distortion_coeffs)postprocessDistToCam(ids, tvecs)result = visualization(image, detections, ids, camera_matrix, distortion_coeffs, rvecs, tvecs, marker_length)#cv2.imwrite(f"./output/marker_detection.png", result)
Lines 1–2: First, we import cv2
(OpenCV), numpy
, and pandas
. OpenCV is a library for computer vision, numpy
is a package for scientific computing, and pandas
is a data analysis and ...