将路径用作FileStorage中的键 [英] Use path as key in FileStorage

查看:354
本文介绍了将路径用作FileStorage中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个文件(json/yaml/xml,格式无关紧要),该文件存储具有特定cv::Mat的文件路径.当我最终意识到这里,组合键不允许输入路径(例如/home/user).

I am trying to write a file (json/yaml/xml, the format does not matter) that stores a filepath with a specific cv::Mat. As I ended up realizing here, the key combination does not allow for a path (/home/user, for example).

void
persist_histograms(const QString& filepath, const QHash<QString, Mat>& histograms)
{
    cv::FileStorage storage(filepath.toStdString(), cv::FileStorage::WRITE);

    QHashIterator<QString, Mat> it(histograms);

    while (it.hasNext()) {
        it.next();
        storage << it.key().toStdString() << it.value();
    }
}

这是cv::FileStorage类的真正限制吗?还是有解决的办法?

Is this a real limitation of the cv::FileStorageclass? Or is there a way of going around it?

最终结果应该类似于:

{
    "/my/path/to/file.png": "<my cv::Mat serialization here>",
    "/my/path/to/another/file.png": "<my cv::Mat serialization here>",
    ...
}

观察

错误发生与格式无关.如果我通过filepath 以yaml/json/xml结尾,错误是相同的: 如果尝试在键的开头添加字母,则会出现此错误:

Observation

The error occurs independently of the format. If I pass a filepath that ends with either yaml/json/xml the errors are the same: This is the error if a try adding a letter at the beginning of the key:

这是我尝试上面的代码时遇到的错误:

This is the error I get when trying the code above:

OpenCV Error: Unspecified error (Incorrect element name /home/user/dtd/images/freckled/freckled_0055.jpg) in operator<<, file /build/opencv/src/opencv-3.2.0/modules/core/src/persistence.cpp, line 6877
terminate called after throwing an instance of 'cv::Exception'
what():  /build/opencv/src/opencv 3.2.0/modules/core/src/persistence.cpp:6877: error: (-2) Incorrect element name /home/user/dtd/images/freckled/freckled_0055.jpg in function operator<<

这是我尝试在键的开头添加字母的错误.

This is the error I get if I try to add a letter to the beginning of the key.

OpenCV Error: Bad argument (Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' ') in icvJSONWrite, file /build/opencv/src/opencv-3.2.0/modules/core/src/persistence.cpp, line 3896

推荐答案

似乎我遇到了类似的错误.如果我尝试像这样以YAML格式存储矩阵

It seems that I have encountered a similar error. If I try to store a matrix in YAML format like this

std::string fName("/path/to/cameraParams.yml");
cv::FileStorage fs(fName, cv::FileStorage::WRITE);
fs << "camera matrix" << cameraMatrix;

一切正常.但是,一旦我将文件格式从YAML更改为XML

everything works fine. But as soon as I change the file format from YAML to XML

std::string fName("/path/to/cameraParams.xml");

我得到一个错误

terminate called after throwing an instance of 'cv::Exception'
what():  OpenCV(4.0.0-pre) /home/shura/software.downloads/libraries/opencv/opencv/modules/core/src/persistence_xml.cpp:83: error: (-5:Bad argument) Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_' in function 'writeTag'

正如我已经意识到的那样,原因是XML格式不允许标记中的空格,而YAML允许.因此,对于YAML,以下文件完全有效

As I have realized, the reason is that XML format does not allow spaces in a tag, while YAML does. So for YAML the file below is completely valid

%YAML:1.0
---
camera matrix: !!opencv-matrix

XML不允许这样

<?xml version="1.0"?>
<opencv_storage>
<camera matrix type_id="opencv-matrix">

只要我用下划线替换相机矩阵"中的空格,一切就可以正常工作.

As soon as I replace the space in "camera matrix" by underscore, everything works fine.

fs << "camera_matrix" << cameraMatrix;

我发现此处 XML格式具有标记名称中对符号的一些限制.特别是,名称中不允许使用斜杠.标签中似乎允许使用点,但是由于某些原因,OpenCV拒绝使用包含点的标签.

As I have found out here XML format has some restrictions on symbols you can have in a tag name. In particular, slashes, which you have in names, are not allowed. Dots seem to be allowed in tags, but for some reason OpenCV refuses to work with tags, which contain dots.

这篇关于将路径用作FileStorage中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆