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

前端之家收集整理的这篇文章主要介绍了OpenCV保存成XML(FileStorage)和CSV(重载<<运算符)文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


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

保存XML

保存示例如下:

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

得到结果文件如下所示:

<span class="hljs-pi" style="color: rgb(0,0.0470588);"><?xml version="1.0"?></span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);"></span><span class="hljs-tag" style="color: rgb(0,0.0470588);"><<span class="hljs-title" style="Box-sizing: border-Box; color: rgb(0,136);">opencv_storage</span>></span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136);">vocabulary</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">type_id</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"opencv-matrix"</span>></span><span style="color: rgb(51,0.0470588);"> </span>
<span class="hljs-tag" style="color: rgb(0,136);">rows</span>></span><span style="color: rgb(51,0.0470588);">12</span><span class="hljs-tag" style="color: rgb(0,0.0470588);"></<span class="hljs-title" style="Box-sizing: border-Box; color: rgb(0,136);">cols</span>></span><span style="color: rgb(51,136);">dt</span>></span><span style="color: rgb(51,0.0470588);">u</span><span class="hljs-tag" style="color: rgb(0,136);">data</span>></span><span style="color: rgb(51,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>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',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>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',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>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',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>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);"> 0 0 0 0 0 0 0 1</span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136);">data</span>></span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136);">vocabulary</span>></span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136);">opencv_storage</span>></span>

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

Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">fs << </span><span class="hljs-string" style="color: rgb(0,0.0470588);"> << mat;</span>

读取XML

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

Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">FileStorage </span><span class="hljs-function" style="Box-sizing: border-Box; color: rgb(51,0.0470588);">fs(<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">".\\vocabulary.xml"</span>,FileStorage::READ)</span><span style="color: rgb(51,0.0470588);">;</span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">Mat mat_vocabulary;</span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">fs</span><span class="hljs-attr_selector" style="color: rgb(0,0.0470588);">["vocabulary"]</span><span style="color: rgb(51,0.0470588);"> >> mat_vocabulary;</span>

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

Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">fs[</span><span class="hljs-string" style="color: rgb(0,0.0470588);">] >> mat_vocabulary;</span>

CSV文件

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

保存CSV文件

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

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

<span class="hljs-keyword" style="color: rgb(0,0.0470588);"> << </span><span class="hljs-keyword" style="color: rgb(0,0.0470588);">format</span><span style="color: rgb(51,0.0470588);">(mat,Formatter::FMT_CSV);</span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">.close();</span>

根据上面的代码只用ofstream对象打开要写入的csv文件,再直接写入数据即可,需要注意的是,接入的矩阵用format格式化,format函数第二个参数表示格式类型,这里选择CSV格式即Formatter::FMT_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',0.0470588);"><span style="line-height: 20.2999992370605px; white-space: pre;">class CV_EXPORTS Formatter{</span>
Box-sizing: border-Box; font-family: 'Source Code Pro',136); Box-sizing: border-Box; line-height: 20.2999992370605px; white-space: pre;">public</span><span style="line-height: 20.2999992370605px; white-space: pre;">:   </span>
Box-sizing: border-Box; font-family: 'Source Code Pro',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); 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); 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); 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); 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); 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); Box-sizing: border-Box; line-height: 20.2999992370605px; white-space: pre;">5</span><span style="line-height: 20.2999992370605px; white-space: pre;"> };   </span>
Box-sizing: border-Box; font-family: 'Source Code Pro',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>
Box-sizing: border-Box; font-family: 'Source Code Pro',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,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,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); Box-sizing: border-Box; line-height: 20.2999992370605px; white-space: pre;">0</span><span style="line-height: 20.2999992370605px; white-space: pre;">;   </span>
Box-sizing: border-Box; font-family: 'Source Code Pro',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,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,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); 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,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,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,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,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,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,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,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,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表示没有标题行,第三行获取得到的数据。

 
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);"><span style="color: rgb(51,0.0470588);">Ptr</span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);"><</span><span style="color: rgb(51,0.0470588);">TrainData</span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);">></span><span style="color: rgb(51,0.0470588);"> train_data;train_data </span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);"> TrainData</span><span class="hljs-tag" style="color: rgb(0,0.0470588);">::loadFromCSV</span><span style="color: rgb(51,0.0470588);">0</span><span style="color: rgb(51,0.0470588);">);</span></span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">Mat m </span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);"> train_data</span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);">-></span><span style="color: rgb(51,0.0470588);">getTrainSamples();</span></span>
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0.0470588);">cout </span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);"> format(m,Formatter</span><span class="hljs-tag" style="color: rgb(0,0.0470588);">::FMT_CSV</span><span style="color: rgb(51,0.0470588);">) </span><span class="hljs-subst" style="Box-sizing: border-Box; font-family: 'Source Code Pro',0.0470588);"> endl;</span>
</span>

代码运行结果为:

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

猜你在找的XML相关文章