OPENCV图像处理基础(六)序列化储存图像

在学习分类器的时候,我们会接触到xml,yaml等标记语言的文件。这些文件实际上就是记录了对一件事物(不仅仅是图片)的描述。那具体记录的是什么呢?等下就会有讲解。
先贴上代码

#include<iostream>
#include<opencv2\opencv.hpp>
using namespace cv;
using namespace std;

int main()
{ 
    Mat wirteimg = Mat::eye(10, 10, CV_8UC1);//生成一个100*100的对角矩阵
    Mat readimg;
    //imwrite("对角矩阵.jpg", img);//保存为jpg格式。测试时删除原文件可查看效果,用全局路径可以保存到特定路径
    //imread()函数可以进行读取
    //imwrite函数只支持CV8U类型的数据(使用OpenCV保存其他类型Mat的时候,程序不会报错,但是无法生成结果文件)可以用FileStorage类,来保存为XML/YAML文件(一种采用标记语言编码数据的文件格式)
    FileStorage fw("对角矩阵.xml", FileStorage::WRITE);//做输出
        fw << "Mat" << wirteimg;//通过输出流写文件,给定MAT标签
            fw.release();//释放文件流对象
    FileStorage fr("对角矩阵.xml", FileStorage::READ);//做读取
    //读取并打印XML文件
        fr["Mat"] >> readimg;//通过输入流读取数据,读取时也要先用标签
            fr.release();//调用这个函数后  
    cout << readimg;        
return 0;
}

在这里我们对FileStorage右键转到定义,就可以看到这个类的声明,其中我翻译了一部分用到的

class **CV_EXPORTS_W** *FileStorage*
{
public:
    //! file storage mode
    enum Mode //这里可以看到操作模式中是一个枚举类型,包含了一系列操作
    {
        READ        = 0, //读取!< value, open the file for reading
        WRITE       = 1, //写入!< value, open the file for writing
        APPEND      = 2, //打开一个文件并添加!< value, open the file for appending
        MEMORY      = 4, //从缓冲区读或写入一个FileStorage释放后返回的数据!< flag, read data from source or write data to the internal buffer (which is
                         //!< returned by FileStorage::release)
        FORMAT_MASK = (7<<3), //添加一个格式标签,如上个代码块中的写入前先写入个Mat字符串,读取时先读取其是否有Mat字符串!< mask for format flags
        FORMAT_AUTO = 0,      //自动添加!< flag, auto format
        FORMAT_XML  = (1<<3), //xml文件标签!< flag, XML format
        FORMAT_YAML = (2<<3)  //yaml标签!< flag, YAML format
    };
    enum
    {
        UNDEFINED      = 0,
        VALUE_EXPECTED = 1,
        NAME_EXPECTED  = 2,
        INSIDE_MAP     = 4
    };

    /** @brief The constructors.

    The full constructor opens the file. Alternatively you can use the default constructor and then
    call FileStorage::open.
     */
    CV_WRAP FileStorage();

    /** @overload
    @param source Name of the file to open or the text string to read the data from. Extension of the
    file (.xml or .yml/.yaml) determines its format (XML or YAML respectively). Also you can append .gz
    to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE and
    FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g.
    mydata.xml, .yml etc.).
    @param flags Mode of operation. See  FileStorage::Mode
    @param encoding Encoding of the file. Note that UTF-16 XML encoding is not supported currently and
    you should use 8-bit encoding instead of it.
    */
    CV_WRAP FileStorage(const String& source, int flags, const String& encoding=String());//上面使用到的是这个有参构造函数,参数分别为,文件名,操作类型,末尾表明该文件使用的编码格式,可以不写

    /** @overload */
    FileStorage(CvFileStorage* fs, bool owning=true);

好了,现在在来看下保存的xml文件到底写的是什么东西

<?xml version="1.0"?>
<opencv_storage>
<Mat type_id="opencv-matrix">//这里包含了标签,即描述的文件
  <rows>10</rows>//这里很直观的看出是图像的一些数据信息,包括行和列
  <cols>10</cols>
  <dt>u</dt>//这里是图像的通道类型,如果是CV_8UC2.则会变成"2u"。
  <data>//这里是数据
    1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1</data></Mat>
</opencv_storage>

看到这里就明晰前面的问题了,这文件其实就是对Mat的数据类型进行了标记。对图像的标记,其实也就是写入了一些关于图像被处理后得到的特征的数据。关键还是看人对一个事物进行了怎样的抽象,以及在计算机中实现相应的数据结构。

注:本人才疏学浅,欢迎指正。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值