使用tinyXML2 读写配置文件

1. 前言

我们上次写了一段用来提取图片中交点信息的程序: http://blog.csdn.net/zhyh1435589631/article/details/53084795
但是, 我们发现代码中存在好多奇异值,非常的讨人厌, 我们就希望可以讲这部分变化的参数移动到配置文件中, 我们只需要让程序读取这个配置文件中的参数就可以了。

2. TinyXML2

2.1 TinyXML2 基本介绍

  1. TinyXML2 的官网地址: http://www.grinninglizard.com/tinyxml2docs/index.html
  2. github 地址: https://github.com/leethomason/tinyxml2
  3. 这是一个非常简单的用来读写xml 文件的一个C++开源工具

2.2 配置文件实例

参考文章: http://blog.csdn.net/k346k346/article/details/44258739
我们精选一下我们的配置文件

<config>
    <autoSet>
        <autoSetGray2BinaryLower>true</autoSetGray2BinaryLower>
        <CurrentMean>25</CurrentMean>
    </autoSet>
</config>

2.3 读取配置文件

  1. 绑定配置文件:

    tinyxml2::XMLDocument doc;
    doc.LoadFile(filename.c_str());
  2. 读取配置文件信息
    使用FirstChildElement 和 FirstChild 组合即可

    bool CXML::getAutoSetGray2BinaryLower(){
        string data = doc.FirstChildElement("config")
            ->FirstChildElement("autoSet")
            ->FirstChildElement("autoSetGray2BinaryLower")
            ->FirstChild()->ToText()->Value();
        return data == "true";
    }
    
    double CXML::getAutoSetCurrentMean(){
        string data = doc.FirstChildElement("config")
            ->FirstChildElement("autoSet")
            ->FirstChildElement("CurrentMean")
            ->FirstChild()->ToText()->Value();
        return stod(data);
    }   

2.4 写配置文件

使用setText 写数据到相应节点中去

bool CXML::setAutoSetCurrentMean(double value){
        tinyxml2::XMLElement * data = doc.FirstChildElement("config")
            ->FirstChildElement("autoSet")
            ->FirstChildElement("CurrentMean");
        data->SetText(value);
        return true;
    }

3. 加入工程文件

项目工程地址: https://code.csdn.net/zhyh1435589631/picture_handle_tianwentai/tree/master
相应的配置文件 xml, 通过修改这个文件中的参数, 我们可以非常方便的控制图像处理过程的控制~~

<config>
    <loadImage>
        <show>false</show>
    </loadImage>

    <downSample>
        <downSampleFactor_x>0.1</downSampleFactor_x>
        <downSampleFactor_y>0.1</downSampleFactor_y>
        <show>false</show>
    </downSample>

    <autoSet>
        <autoSetGray2BinaryLower>true</autoSetGray2BinaryLower>
        <CurrentMean>25</CurrentMean>
    </autoSet>

    <DealWithImageRough>
        <show>false</show>

        <Gray2Binary>
            <lower>120</lower>
            <higher>255</higher>
        </Gray2Binary>

        <EdgeDetection>
            <canny_lower>50</canny_lower>
            <canny_higher>200</canny_higher>
        </EdgeDetection>

        <DetectLines>
            <rho>1</rho>
            <theta_splitNums>180</theta_splitNums>
            <threshold>80</threshold>
            <minLineLength>70</minLineLength>
            <maxLineGap>50</maxLineGap>
        </DetectLines>

        <RemoveDuplicateLines>
            <threshold>70</threshold>
            <angle_splitNum>24</angle_splitNum>
        </RemoveDuplicateLines>

        <CalcPoints>

        </CalcPoints>

        <RemoveDuplicatePoints>

        </RemoveDuplicatePoints>
    </DealWithImageRough>

    <DealWithImageROI>
        <ROI_size>300</ROI_size>

        <show>false</show>

        <Gray2Binary>
            <lower>100</lower>
            <higher>255</higher>
        </Gray2Binary>

        <EdgeDetection>
            <canny_lower>50</canny_lower>
            <canny_higher>200</canny_higher>
        </EdgeDetection>

        <DetectLines>
            <rho>1</rho>
            <theta_splitNums>180</theta_splitNums>
            <threshold>80</threshold>
            <minLineLength>200</minLineLength>
            <maxLineGap>50</maxLineGap>
        </DetectLines>

        <RemoveDuplicateLines>
            <threshold>70</threshold>
            <angle_splitNum>24</angle_splitNum>
        </RemoveDuplicateLines>

        <CalcPoints>

        </CalcPoints>

        <RemoveDuplicatePoints>

        </RemoveDuplicatePoints>
    </DealWithImageROI>


</config>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值