OpenCV detectMarkers()报'cameraMatrix'无效参数错误原因咨询
问题:OpenCV detectMarkers() 报错“'cameraMatrix' is an invalid keyword argument”
完整错误信息:
OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'detectMarkers' > Overload resolution failed: > - 'cameraMatrix' is an invalid keyword argument for detectMarkers()
用户表示相机标定代码正常,已输入标定参数,但运行时仍出现该错误,代码如下:
import numpy as np import cv2 import sys import time ARUCO_DICT = { "DICT_4X4_50": cv2.aruco.DICT_4X4_50, "DICT_4X4_100": cv2.aruco.DICT_4X4_100, "DICT_4X4_250": cv2.aruco.DICT_4X4_250, "DICT_4X4_1000": cv2.aruco.DICT_4X4_1000, "DICT_5X5_50": cv2.aruco.DICT_5X5_50, "DICT_5X5_100": cv2.aruco.DICT_5X5_100, "DICT_5X5_250": cv2.aruco.DICT_5X5_250, "DICT_5X5_1000": cv2.aruco.DICT_5X5_1000, "DICT_6X6_50": cv2.aruco.DICT_6X6_50, "DICT_6X6_100": cv2.aruco.DICT_6X6_100, "DICT_6X6_250": cv2.aruco.DICT_6X6_250, "DICT_6X6_1000": cv2.aruco.DICT_6X6_1000, "DICT_7X7_50": cv2.aruco.DICT_7X7_50, "DICT_7X7_100": cv2.aruco.DICT_7X7_100, "DICT_7X7_250": cv2.aruco.DICT_7X7_250, "DICT_7X7_1000": cv2.aruco.DICT_7X7_1000, "DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL, "DICT_APRILTAG_16h5": cv2.aruco.DICT_APRILTAG_16h5, "DICT_APRILTAG_25h9": cv2.aruco.DICT_APRILTAG_25h9, "DICT_APRILTAG_36h10": cv2.aruco.DICT_APRILTAG_36h10, "DICT_APRILTAG_36h11": cv2.aruco.DICT_APRILTAG_36h11 } def aruco_display(corners, ids, rejected, image): if len(corners) > 0: ids = ids.flatten() for (markerCorner, markerID) in zip(corners, ids): corners = markerCorner.reshape((4, 2)) (topLeft, topRight, bottomRight, bottomLeft) = corners topRight = (int(topRight[0]), int(topRight[1])) bottomRight = (int(bottomRight[0]), int(bottomRight[1])) bottomLeft = (int(bottomLeft[0]), int(bottomLeft[1])) topLeft = (int(topLeft[0]), int(topLeft[1])) cv2.line(image, topLeft, topRight, (0, 255, 0), 2) cv2.line(image, topRight, bottomRight, (0, 255, 0), 2) cv2.line(image, bottomRight, bottomLeft, (0, 255, 0), 2) cv2.line(image, bottomLeft, topLeft, (0, 255, 0), 2) cX = int((topLeft[0] + bottomRight[0]) / 2.0) cY = int((topLeft[1] + bottomRight[1]) / 2.0) cv2.circle(image, (cX, cY), 4, (0, 0, 255), -1) cv2.putText(image, str(markerID),(topLeft[0], topLeft[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) print("[Inference] ArUco marker ID: {}".format(markerID)) return image def pose_estimation(frame, aruco_dict_type, matrix_coefficients, distortion_coefficients): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.aruco_dict = cv2.aruco.getPredefinedDictionary(aruco_dict_type) parameters = cv2.aruco.DetectorParameters() corners, ids, rejected_img_points = cv2.aruco.detectMarkers(gray, cv2.aruco_dict,parameters=parameters, cameraMatrix=matrix_coefficients, distCoeff=distortion_coefficients) if len(corners) > 0: for i in range(0, len(ids)): rvec, tvec, markerPoints = cv2.aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients, distortion_coefficients) cv2.aruco.drawDetectedMarkers(frame, corners) cv2.aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01) return frame aruco_type = "DICT_4X4_50" arucoDict = cv2.aruco.getPredefinedDictionary(ARUCO_DICT[aruco_type]) arucoParams = cv2.aruco.DetectorParameters() intrinsic_camera = np.array(((981.69820777, 0, 637.86160616),(0,983.8672381, 364.31555519),(0,0,1))) distortion = np.array((0.0366562741,-0.153342145,-0.000307462615,-0.000078917106,0.215865749)) cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) while cap.isOpened(): ret, img = cap.read() output = pose_estimation(img, ARUCO_DICT[aruco_type], intrinsic_camera, distortion) cv2.imshow('Estimated Pose', output) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break cap.release() cv2.destroyAllWindows()
错误原因及解决方法
核心原因
cv2.aruco.detectMarkers() 函数不接受cameraMatrix和distCoeff作为关键字参数,这两个参数是用于姿态估计的,而非标记检测。
你混淆了检测与姿态估计的函数职责:
detectMarkers()仅负责识别图像中的ArUco标记轮廓,不需要相机内参;estimatePoseSingleMarkers()才需要cameraMatrix和distCoeff来计算标记在3D空间中的位姿。
修正步骤
移除
detectMarkers()中的相机参数:
将pose_estimation函数中的detectMarkers调用修改为:corners, ids, rejected_img_points = cv2.aruco.detectMarkers(gray, cv2.aruco_dict, parameters=parameters)保留姿态估计的参数:
当前estimatePoseSingleMarkers()中传入matrix_coefficients和distortion_coefficients的写法是正确的,无需修改。
修正后的pose_estimation函数
def pose_estimation(frame, aruco_dict_type, matrix_coefficients, distortion_coefficients): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) aruco_dict = cv2.aruco.getPredefinedDictionary(aruco_dict_type) parameters = cv2.aruco.DetectorParameters() # 仅传递必要参数给detectMarkers corners, ids, rejected_img_points = cv2.aruco.detectMarkers(gray, aruco_dict, parameters=parameters) if len(corners) > 0: for i in range(0, len(ids)): rvec, tvec, markerPoints = cv2.aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients, distortion_coefficients) cv2.aruco.drawDetectedMarkers(frame, corners) cv2.aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01) return frame
(注:建议将cv2.aruco_dict改为局部变量aruco_dict,避免污染全局命名空间)
内容的提问来源于stack exchange,提问作者NiccyT
相关产品推荐
相关产品推荐

