You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArUco标记间测距异常:上下布局距离偏差问题求助

问题:ArUco标记上下布局时测距错误,左右布局正常

我编写程序计算两个ArUco标记间的距离,当标记位于相机画面左右两侧时,测距结果基本准确;但当标记位于画面上下两侧时,测距结果完全错误。

单独测试标记到相机的距离,无论标记在画面哪个位置,结果都正常。标记实际在纸上的间距为长向20cm,短向10cm(左右布局时测距结果接近实际,上下布局时偏差极大)。

代码实现

import numpy as np
import cv2
import cv2.aruco as aruco

    
def mag(x):
    return np.sqrt(x[0]**2+x[1]**2+x[2]**2)


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

mtx = np.array([[ 8.8213173810477792e+02, 0., 6.2265593183247574e+02],[ 0.,
       9.0710890795744012e+02, 4.1207655778884924e+02],[ 0., 0., 1. ]])

dist = np.array([ 5.3943178087169294e-03, -2.0750715497990183e-01,
       -1.2051405507801402e-03, -2.0261943010518283e-03,
       4.2906970296298941e-01 ])

cap = cv2.VideoCapture(0)  # 获取相机源

alltvec = np.zeros([50,3])

def track(matrix_coefficients, distortion_coefficients):
    while True:
        ret, frame = cap.read() # 捕获一帧画面
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 转为灰度图
        aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_50)  # 使用4x4字典识别标记
        parameters = aruco.DetectorParameters_create()  # 标记检测参数
        
        # 识别到的标记角点、ID和候选点列表
        corners, ids, rejected_img_points = aruco.detectMarkers(gray, aruco_dict,
                                                                parameters=parameters,
                                                                cameraMatrix=mtx,
                                                             distCoeff=dist)

        if np.all(ids is not None):  # 如果检测到标记
            for i in range(0, len(ids)):  # 遍历每个标记
                # 估计每个标记的位姿,返回rvec和tvec
                rvec, tvec, markerPoints = aruco.estimatePoseSingleMarkers(corners[i], 0.031, mtx,dist)
                (rvec - tvec).any()  # 消除numpy数组报错
                aruco.drawDetectedMarkers(frame, corners)  # 在标记周围绘制正方形
                aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01)  # 绘制坐标轴
                alltvec[ids[i]-1] = tvec # 将tvec值存入数组
        

        if ids is not None and len(ids) > 1:  # 如果检测到至少两个标记,计算间距
            vector = alltvec[1]-alltvec[0]
            distance = np.linalg.norm(vector)
            cv2.putText(frame, "Distance: {:.2f} m".format(distance), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        cv2.imshow('frame', frame)
        
        # 等待3毫秒交互,检测按键
        key = cv2.waitKey(3) & 0xFF
        if key == ord('q'):  # 按q退出
            break
    # 释放资源
    cap.release()
    cv2.destroyAllWindows()
    
track(mtx,dist)

标记布局说明

  • 左右布局:两个ArUco标记横向排列在相机画面的左右两侧
  • 上下布局:两个ArUco标记纵向排列在相机画面的上下两侧

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:31:01