I’m trying to detect an aruco marker and calculate the distance to it from the camera, but the results I’m getting are off by a factor of 2.37.
I’ve measured the real physical distance to 63cm, but according to my tvecs vector, the distance is ~149cm. So if I simply change the “marker_size” variable used to scale the objectpoints accordingly, the distance from tvecs is spot on.
Here’s the code performing the pose estimation:
def __init__(self, a_dict, marker_size):
self.a_dict = a_dict
self.params = aruco.DetectorParameters()
self.ad = aruco.ArucoDetector(self.a_dict, self.params)
self.util = Util()
self.marker_size = marker_size #mm
self.marker_objpts = np.array([[-self.marker_size/2, self.marker_size/2, 0],
[ self.marker_size/2, self.marker_size/2, 0],
[ self.marker_size/2, -self.marker_size/2, 0],
[-self.marker_size/2, -self.marker_size/2, 0]], dtype=np.float32)
def pose(self, frame, corners, ids, mtx, dist, flags):
"""
#### Estimate pose of Aruco marker ####
"""
markerLength = 156
axis_length = self.marker_size / 2
if len(corners) > 0:
ids_f = ids.copy()
ids_f= ids.flatten() #Why do this? (NicolaiNielsen)
ret, rvecs, tvecs = cv.solvePnP(self.marker_objpts, corners[0], mtx, dist, False, flags)
imgpts, jac = cv.projectPoints(self.marker_objpts, rvecs, tvecs, mtx, dist)
self.project_aruco_bounds(frame, imgpts)
self.util.project(frame, imgpts)
cv.drawFrameAxes(frame, mtx, dist, rvecs, tvecs, axis_length)
return rvecs, tvecs, imgpts
And the resulting frame shows:
And I performed validation of the calibration using a separate set of images of the same chessboard used for calibrating:
The mean reprojection error for the whole set of 9 validation images was ~0.82, which should be fine? I also used the calibration parameters to undistort the validation images, and they ended up looking fine with no strange curves or anything. Additionally, I’ve triple checked that all square/marker-sizes used in calibration and pose estimation are in the same unit (mm).
I’ve really run out of ideas for what may be causing this. Would really appreciate any help I can get.