OpenCV保存成XML(FileStorage)和CSV(重载<<运算符)文件

来源:互联网 发布:淘宝不能登录怎么办 编辑:程序博客网 时间:2024/06/05 03:09


使用OpenCV时不仅要保存影像结果,往往也需要保存中间的矩阵结果,而OpenCV的imwrite函数只支持CV8U类型的数据(使用OpenCV保存其他类型Mat的时候,程序不会报错,但是无法生成结果文件),因此会给工作带来很多不便。OpenCV在2.0以后的版本中提供了FileStorage类,供用户直接使用,保存为XML/YAML文件。

保存XML

保存示例如下:

<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">Mat mat </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">=</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> Mat</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">::eye</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">(Size(</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">12</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">,</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">12</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">), CV_8UC1);</span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">FileStorage fs(</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"f:\\vocabulary.xml"</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">, FileStorage</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">::WRITE</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">);</span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">fs </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"vocabulary"</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> mat;</span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">fs</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">.</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">release();</span>

得到结果文件如下所示:

<span class="hljs-pi" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><?xml version="1.0"?></span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">opencv_storage</span>></span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">vocabulary</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type_id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"opencv-matrix"</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">  </span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">rows</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">12</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">rows</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">  </span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">cols</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">12</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">cols</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">  </span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dt</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">u</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dt</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">  </span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">data</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">    1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0    </span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0   </span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0   </span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0   </span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> 0 0 0 0 0 0 0 1</span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">data</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">vocabulary</span>></span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">opencv_storage</span>></span>

需要注意的是,因为保存的结果为xml文件,所以再保存的时候需要给定标签,格式就是下面的形式,先存储标签内容,再存储矩阵:

<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">fs << </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"vocabulary"</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> << mat;</span>

读取XML

同样使用FileStorage类可以直接获取保存的xml文件内容。 
读取示例如下:

<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">FileStorage </span><span class="hljs-function" style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">fs(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">".\\vocabulary.xml"</span>, FileStorage::READ)</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">;</span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">Mat mat_vocabulary;</span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">fs</span><span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">["vocabulary"]</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> >> mat_vocabulary;</span>

读取文件的时候同样要给定标签内容,这里通过中括号[]给定:

<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">fs[</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"vocabulary"</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">] >> mat_vocabulary;</span>

CSV文件

很多时候,直接使用CSV文件相比XML更方便,使用XML保存得到的数据,无论是查看(excel查看,XML中矩阵不是格式化的)还是接着使用别的工具处理(例如matlab),都可以快速的读取,而XML需要解析则比较繁琐。那么该如何保存CSV文件呢,对保存的CSV文件又应该如何读取呢,请看下文分解。

保存CSV文件

OpenCV提供了结果保存的格式化函数format,使用format我们就可以根据自己想要的格式打印和保存数据了。

先直接看保存函数,再来分析具体含义吧:

<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">ofstream </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">file</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">(</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"test.csv"</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">);</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">file</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> << </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">format</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">(mat, Formatter::FMT_CSV);</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">file</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">.close();</span>

根据上面的代码只用ofstream对象打开要写入的csv文件,再直接写入数据即可,需要注意的是,接入的矩阵用format格式化,format函数第二个参数表示格式类型,这里选择CSV格式即Formatter::FMT_CSV

保存结果如图所示: 
CSV

这里保存格式为定义在Formatter类中的枚举类型,总共有6中选项,各位读者可以根据自己的需求选择。

<pre class="prettyprint" name="code" style="margin-top: 0px; margin-bottom: 1.1em; white-space: nowrap; word-wrap: break-word; box-sizing: border-box; font-family: 'Source Code Pro', monospace; padding: 5px 5px 5px 60px; font-size: 14px; line-height: 1.45; word-break: break-all; color: rgb(51, 51, 51); border: 1px solid rgba(128, 128, 128, 0.0745098); border-radius: 0px; position: relative; overflow-y: hidden; overflow-x: auto; background-color: rgba(128, 128, 128, 0.0470588);"><span style="line-height: 20.2999992370605px; white-space: pre;">class CV_EXPORTS Formatter{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">public</span><span style="line-height: 20.2999992370605px; white-space: pre;">:    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">enum</span><span style="line-height: 20.2999992370605px; white-space: pre;"> { FMT_DEFAULT = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">0</span><span style="line-height: 20.2999992370605px; white-space: pre;">, FMT_MATLAB = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">1</span><span style="line-height: 20.2999992370605px; white-space: pre;">,  FMT_CSV = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">2</span><span style="line-height: 20.2999992370605px; white-space: pre;">, FMT_PYTHON = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">3</span><span style="line-height: 20.2999992370605px; white-space: pre;">, FMT_NUMPY = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">4</span><span style="line-height: 20.2999992370605px; white-space: pre;">, FMT_C = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">5</span><span style="line-height: 20.2999992370605px; white-space: pre;"> };    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">virtual</span><span style="line-height: 20.2999992370605px; white-space: pre;"> ~Formatter();    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">virtual</span><span style="line-height: 20.2999992370605px; white-space: pre;"> Ptr<Formatted> format(</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">const</span><span style="line-height: 20.2999992370605px; white-space: pre;"> Mat& mtx) </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">const</span><span style="line-height: 20.2999992370605px; white-space: pre;"> = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">0</span><span style="line-height: 20.2999992370605px; white-space: pre;">;    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">virtual</span><span style="line-height: 20.2999992370605px; white-space: pre;"> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">void</span><span style="line-height: 20.2999992370605px; white-space: pre;"> set32fPrecision(</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">int</span><span style="line-height: 20.2999992370605px; white-space: pre;"> p = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">8</span><span style="line-height: 20.2999992370605px; white-space: pre;">) = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">0</span><span style="line-height: 20.2999992370605px; white-space: pre;">;    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">virtual</span><span style="line-height: 20.2999992370605px; white-space: pre;"> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">void</span><span style="line-height: 20.2999992370605px; white-space: pre;"> set64fPrecision(</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">int</span><span style="line-height: 20.2999992370605px; white-space: pre;"> p = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">16</span><span style="line-height: 20.2999992370605px; white-space: pre;">) = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">0</span><span style="line-height: 20.2999992370605px; white-space: pre;">;    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">virtual</span><span style="line-height: 20.2999992370605px; white-space: pre;"> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">void</span><span style="line-height: 20.2999992370605px; white-space: pre;"> setMultiline(</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">bool</span><span style="line-height: 20.2999992370605px; white-space: pre;"> ml = </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">true</span><span style="line-height: 20.2999992370605px; white-space: pre;">) = </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">0</span><span style="line-height: 20.2999992370605px; white-space: pre;">;    </span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">static</span><span style="line-height: 20.2999992370605px; white-space: pre;"> Ptr<Formatter> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">get</span><span style="line-height: 20.2999992370605px; white-space: pre;">(</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; line-height: 20.2999992370605px; white-space: pre;">int</span><span style="line-height: 20.2999992370605px; white-space: pre;"> fmt = FMT_DEFAULT);</span>
<span style="line-height: 20.2999992370605px; white-space: pre;">};</span>

读取CSV数据

在保存为CSV格式后该如何读取呢,这里可以使用OpenCV中ml模块的TrainData类的函数读取。具体代码如下所示,第二行为加载数据,参数0表示没有标题行,第三行获取得到的数据。

<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">Ptr</span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">TrainData</span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> train_data;train_data </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">=</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> TrainData</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">::loadFromCSV</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">(</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">"test.csv"</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">, </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">0</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">); </span></span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">Mat m </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">=</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> train_data</span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">-></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">getTrainSamples();</span></span>
<span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">cout </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> format(m, Formatter</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">::FMT_CSV</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">) </span><span class="hljs-subst" style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> endl;</span></span>

代码运行结果为: 
这里写图片描述

参考链接: 
1. OpenCV使用FileStorage保存Mat数据 
2. XML/YAML file storages. Writing to a file storage.

0 0