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

从YAML文件读取cv2.aruco.Dictionary对象失败求助

OpenCV 4.6.0中ArUco字典读写问题解决

问题场景

可以成功将cv2.aruco.Dictionary数据写入YAML文件,但无法从该文件重建字典对象:

写入代码

aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_100)
file_storage = cv2.FileStorage(str(file_path), cv2.FILE_STORAGE_WRITE)
aruco_dict.writeDictionary(file_storage)
file_storage.release()

生成的YAML示例

%YAML:1.0
---
nmarkers: 100
markersize: 4
maxCorrectionBits: 1
marker_0: "1011010100110010"
...

尝试的读取代码及报错

file_storage = cv2.FileStorage(str(file_path), cv2.FILE_STORAGE_READ)
file_node = file_storage.getFirstTopLevelNode()
dictionary = cv2.aruco.Dictionary.readDictionary(file_node)
file_storage.release()

报错信息:

TypeError: descriptor 'readDictionary' for 'cv2.aruco.Dictionary' objects doesn't apply to a 'cv2.FileNode' object

错误原因

readDictionary是cv2.aruco.Dictionary的实例方法,而非类方法。直接通过类调用该方法会导致类型不匹配,因为它需要绑定到具体的Dictionary实例上才能接收FileNode参数。

解决方法

先创建一个空的cv2.aruco.Dictionary实例,再调用其readDictionary方法读取FileNode:

正确读取代码

file_storage = cv2.FileStorage(str(file_path), cv2.FILE_STORAGE_READ)
file_node = file_storage.getFirstTopLevelNode()
# 创建空字典实例
dictionary = cv2.aruco.Dictionary()
# 调用实例方法读取数据
dictionary.readDictionary(file_node)
file_storage.release()

# 验证读取结果(可选)
print(f"读取的标记数量: {dictionary.nmarkers}")
print(f"标记尺寸: {dictionary.markerSize}")

补充说明

根据OpenCV 4.6.0官方文档,readDictionary方法用于从FileNode中加载字典数据,必须由已实例化的Dictionary对象调用,不能直接通过类调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 12:07:25