在OpenCV中读取YAML文件

时间:2015-07-13 11:11:23

标签: opencv file-io yaml

我正在使用OpenCV来保存declare my_table%ROWTYPE rec; begin rec := mfi_cust_details('id'); insert into my_table( cust_title_code, cust_id, address1, address_2, city_code, country_code, zip_code) values ( rec.cust_title_code, rec.cust_id, rec.address1, rec.address_2, rec.city_code, rec.country_code, rec.zip_code ); -- don't forget to commit; end; 。这是我使用的代码

.yml

以下是YAML文件的输出

FileStorage fs;
fs.open("test", FileStorage::WRITE);
for (unsigned int i = 0; i < (block_hist_size * blocks_per_img.area()) ; i++ )
{
fs << "features" << dest_ptr[i];
}
fs.release();

有人可以帮我把yml文件读回dest_ptr。我只需要浮点值

2 个答案:

答案 0 :(得分:1)

您不能对多个值使用相同的密钥。在您的情况下,您应该将其存储为功能向量。见下面的代码

FileStorage fs, fs2;
fs.open("test.yml", FileStorage::WRITE);
fs << "features" << "[";
for (unsigned int i = 0; i < 20; i++) {
    fs << 1.0 / (i + 1);
}
fs << "]";
fs.release();

fs2.open("test.yml", FileStorage::READ);
vector<float> a;
fs2["features"] >> a;

答案 1 :(得分:1)

您必须误读YAML规范,因为您使用代码生成的内容不是YAML文件。
在YAML文件中,根据generic mapping定义,映射键必须是唯一的(并且您在使用通用映射时未指定标记):

Definition:

    Represents an associative container, where each key is unique in the
    association and mapped to exactly one value. YAML places no restrictions on
    the type of keys; in particular, they are not restricted to being  scalars. 
    Example bindings to native types include Perl’s hash, Python’s dictionary,
    and Java’s Hashtable.

真正的问题是你试图让一个简单的YAML发射器自己进行字符串写操作。您应该已经在C ++和emitted that结构中创建了所需的数据结构,然后YAML就是正确的(假设发射器没有错误)。

根据您选择的文件结构类型,您可能会得到:

%YAML:1.0
- features: 1.5302167832851410e-01
- features: 1.0552208870649338e-01
- features: 1.6659785807132721e-01
- features: 2.3539969325065613e-01
- features: 2.0810306072235107e-01
- features: 1.2627227604389191e-01
- features: 8.0759152770042419e-02
- features: 6.4930714666843414e-02
- features: 6.1364557594060898e-02
- features: 2.1614919602870941e-01
- features: 1.4714729785919189e-01
- features: 1.5476198494434357e-01

(一系列单键/值映射)或:

%YAML:1.0
features: 
- 1.5302167832851410e-01
- 1.0552208870649338e-01
- 1.6659785807132721e-01
- 2.3539969325065613e-01
- 2.0810306072235107e-01
- 1.2627227604389191e-01
- 8.0759152770042419e-02
- 6.4930714666843414e-02
- 6.1364557594060898e-02
- 2.1614919602870941e-01
- 1.4714729785919189e-01
- 1.5476198494434357e-01

(将单个键映射到作为序列的单个值)

正如您自己所经历的那样,使用字符串写入为数据结构创建正确的YAML甚至比这更简单是非常重要的。如果从适当的数据结构开始并发出该结构,您也知道可以使用相同的结构将其重新读入。

根据我的经验,生成XML / HTML / CSV / INI文件同样适用,具有正确的数据结构并使用发射器也可以避免输出格式中的错误,从而补偿任何略高的复杂性初始代码。