opencv 从yaml文件问题阅读cv2.aruco.Dictionary

i5desfxk  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(138)

我可以将cv2.aruco.Dictionary数据写入磁盘上的YAML文件。但我无法使用此yaml文件重新创建此对象。目前我正在像这样将aruco字典写入磁盘。

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:1.0
---
nmarkers: 100
markersize: 4
maxCorrectionBits: 1
marker_0: "1011010100110010"
marker_1: "0000111110011010"
marker_2: "0011001100101101"
marker_3: "1001100101000110"
marker_4: "0101010010011110"
marker_5: "0111100111001101"
marker_6: "1001111000101110"
marker_7: "1100010011110010"
marker_8: "1111111011011010"
marker_9: "1100111101010110"
marker_10: "1111100110010001"
...

我已经尝试了很多不同的方法来加载这个。下面是我目前的尝试

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()

在这里我得到这个错误

dictionary = cv2.aruco.Dictionary.readDictionary(file_node)
TypeError: descriptor 'readDictionary' for 'cv2.aruco.Dictionary' objects doesn't apply to a 'cv2.FileNode' object

我正在运行opencv 4.6.0https://docs.opencv.org/4.6.0/d5/d0b/classcv_1_1aruco_1_1Dictionary.html#a69ec5ae547b01be07b7ce8c437ad1db4

bqjvbblv

bqjvbblv1#

我一直在这周围混日子。看起来我现在可以通过这样做来解决这个问题。我仍然想使用从opencv tho读取和写入

aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_100)

with open(file_path, "w") as FILE:
    FILE.write(json.dumps({
        "marker_size" : aruco_dict.markerSize,
        "bytes_list": aruco_dict.bytesList.tolist(),
        "max_corr": aruco_dict.maxCorrectionBits,
    }, indent=4))

with open(file_path, "r") as FILE:
    data = json.loads(FILE.read())
    loaded_bytes = np.array(data["bytes_list"], dtype=np.uint8)
    loaded_dict = cv2.aruco.Dictionary(
        len(loaded_bytes),
        data["marker_size"],
        data["max_corr"]
    )
    loaded_dict.bytesList = loaded_bytes

相关问题