I am working on image processing project that detects ArUco markers and doing somethings according to those. When there is only one marker in the video or image everything works fine, but when I put another marker, the second ArUco marker's pose (axes) printing to the wrong place, not to the center of the marker. Please see the screenshot I shared for better understand.
The marker with the id[2] is fine. Pose axes are drawn to the center of marker. But, the marker's pose axes with the id1 are drawn to some random point.
Here is the code part of that detects ArUco markers and draws the axis.
while True:
ret, frame = cap.read()
# Operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Change grayscale
aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_250) # Specify marker size as 4x4, 5x5, 6x6
parameters = aruco.DetectorParameters_create() # Marker detection parameters
# Lists of ids and the corners beloning to each marker
corners, ids, rejected_img_points = aruco.detectMarkers(gray, aruco_dict,
parameters=parameters,
cameraMatrix=matrix_coefficients,
distCoeff=distortion_coefficients)
try:
if np.all(ids is not None): # If there are markers found by detector
for i in range(0, len(ids)): # Iterate in markers
# Estimate pose of each marker and return the values rvec and tvec---different from camera coefficients
rvec, tvec, markerPoints = aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients,
distortion_coefficients)
(rvec - tvec).any() # get rid of that nasty numpy value array error
aruco.drawDetectedMarkers(frame, corners) # Draw A square around the markers
aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01) # Draw axis
c_x = (corners[i][0][0][0] + corners[i][0][1][0] + corners[i][0][2][0] + corners[i][0][3][0]) / 4 # X coordinate of marker's center
c_y = (corners[i][0][0][1] + corners[i][0][1][1] + corners[i][0][2][1] + corners[i][0][3][1]) / 4 # Y coordinate of marker's center
cv2.putText(frame, "id"+str(ids[i]), (int(c_x), int(c_y)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (50,225,250), 2)
except:
if ids is None or len(ids) == 0:
print("******************************************************")
print("*************** Marker Detection Failed **************")
print("******************************************************")