OpenCV aruco 检测在 estimatePoseBoard() 上失败

OpenCV aruco detection fails on estimatePoseBoard()

我正在尝试检测 3x3 Aruco 板。

代码运行直到满足第 33 行的参数,然后第 35 行抛出相同的错误,无论帧中有多少标记(即在 1 和所有 9 之间)

输出为

width: 3
height: 3
marker_size: 0.05
marker_separation: 0.05
cameraMat: D:\Aruco\CameraCalib\L515_camMat_0.37.dat
distCoeff: D:\Aruco\CameraCalib\L515_distCoeffs_0.37.dat
output:
  width: 700
  height: 700
  margin: 10

[597.43121351   0.         317.54758687   0.         597.50401806
 264.76581981   0.           0.           1.        ]
[ 0.07009082  0.15721941  0.00925155 -0.00357965 -0.9931981 ]
None None
Traceback (most recent call last):
  File "d:\Aruco\CameraPose\pose.py", line 35, in mainFunc
    success, rvec, tvec = cv.aruco.estimatePoseBoard(corners, ids, board, cameraMatrix, distCoeffs, rvec, tvec)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\pip-req-build-i1s8y2i1\opencv\modules\core\src\convert_c.cpp:112: error: (-215:Assertion failed) src.size == dst.size && src.channels() == dst.channels() in function 'cvConvertScale'

整个脚本是

import cv2 as cv
from omegaconf import DictConfig, OmegaConf
import numpy as np
import hydra

vid = cv.VideoCapture(1) 

@hydra.main(config_name='config')
def mainFunc(cfg: DictConfig) -> None:
    print(OmegaConf.to_yaml(cfg))

    # camera parameters are read from somewhere
    cameraMatrix = np.fromfile("D:\Aruco\CameraCalib\L515_camMat_0.37.dat")
    print(cameraMatrix)
    distCoeffs = np.fromfile("D:\Aruco\CameraCalib\L515_distCoeffs_0.37.dat")
    print(distCoeffs)

    # create the board
    markerDict = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_50)
    board = cv.aruco.GridBoard_create(cfg.width,cfg.height, cfg.marker_size, cfg.marker_separation, markerDict)

    rvec = None
    tvec = None
    while(True):
        # get frame from camera
        ret, inputImg = vid.read() 
        gray = cv.cvtColor(inputImg, cv.COLOR_BGR2GRAY)

        # detect markers
        corners, ids, reject = cv.aruco.detectMarkers(gray, markerDict, cameraMatrix=cameraMatrix, distCoeff=distCoeffs)

        # if at least one marker detected
        if (len(corners) > 0) and (len(ids) > 0):
            print(rvec, tvec)
            success, rvec, tvec = cv.aruco.estimatePoseBoard(corners, ids, board, cameraMatrix, distCoeffs, rvec, tvec)

            if success > 0:
                gray = cv.aruco.drawAxis(gray, cameraMatrix, distCoeffs, rvec, tvec, 0.05)
                
        cv.imshow('frame', gray)
        #print(gray.shape)
        cv.waitKey(1)

if __name__ == "__main__":
    mainFunc()

我也尝试过其他人 (Link) 编写的脚本,它们声称可以做同样的事情,但它们在相同的函数调用中以相同的方式失败

已解决,如果其他人遇到此问题:

np.fromfile("D:\Aruco\CameraCalib\L515_camMat_0.37.dat")

具有 [9] 的形状,而不是保存前矩阵的 [3,3],因此相机矩阵在传递给函数时的形状不正确

奇怪的是 cv.aruco.detectMarkers() 不会抛出任何错误并且 returns 在传递形状不正确的矩阵时正常

已通过在相机校准和此文件中切换为使用 np.save 和 np.load 进行修复,然后重做校准以保存正确的格式