ros_demo系列——demo_yaml

demo_yaml

与json,lua文件差不多一类挺方便的保存读写文件格式,此次分别实用ymal-cpp与opencv的方法来实现yaml的保存读写等方法,方便日后开发。另外附上与别的demo来个组合功能。

参考地址资料:

yaml-cpp方法

  • cpp
#include <ros/ros.h>
#include <iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>

int main(int argc,char** argv)
{
    ros::init(argc, argv, "demo_yaml_v1");
    ros::NodeHandle n("~");

    std::string path;

    //???这里不知道怎么写成~开头的路径,等待解答
    n.param<std::string>("path", path, "../config/test.yaml");

    //YAML 读
    YAML::Node config = YAML::LoadFile(path);
    ROS_INFO("name: %s",config["name"].as<std::string>().c_str());
    ROS_INFO("sex: %s",config["sex"].as<std::string>().c_str());
    ROS_INFO("age: %s",config["age"].as<std::string>().c_str());

    //YAML 写
    int age = 456;
    std::ofstream fout(path.c_str());
    config["age"] = age;//添加新元素
    ROS_INFO("write_age: %d",age);
    fout << config;
    fout.close();
    return 0;
}
  • Cmakelist.txt
#主要改以下的东西
FIND_PACKAGE(yaml-cpp REQUIRED)
#...
add_executable(demo_yaml src/demo_yaml.cpp)
target_link_libraries(demo_yaml ${YAML_CPP_LIBRARIES} ${catkin_LIBRARIES})

Opencv方法

  • cpp(read)
#include <ros/ros.h>
#include "opencv2/opencv.hpp"
 
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    ros::init(argc, argv, "demo_yaml_v2_read");
    ros::NodeHandle n("~");

    string path;

    //运行ros节点后,ros的相对路径是工作包所在工作空间的根目录
    n.param<std::string>("path", path, "./src/ros_demo/demo_yaml/demo_yaml_v2/config/test_read.yaml");

    //加载opencv相关的yaml文件第一行必须是 %YAML:1.0
    FileStorage fs2(path.c_str(), FileStorage::READ);

    if ( !fs2.isOpened() )
    {
        ROS_INFO("READ File Failed!");
        return 0;
    }
 
    //直接读取
    // first method: use (type) operator on FileNode.
        int frameCount = (int)fs2["frameCount"];
        
        string date;
        // second method: use FileNode::operator >>
        fs2["calibrationDate"] >> date;
        
        Mat cameraMatrix2, distCoeffs2;
        fs2["cameraMatrix"] >> cameraMatrix2;
        fs2["distCoeffs"] >> distCoeffs2;
        
        cout << "frameCount: " << frameCount << endl
            << "calibration date: " << date << endl
            << "camera matrix: " << cameraMatrix2 << endl
            << "distortion coeffs: " << distCoeffs2 << endl;
    //
    
    //迭代器方法
        FileNode features = fs2["features"];
        FileNodeIterator it = features.begin(), it_end = features.end();
        int idx = 0;
        std::vector<uchar> lbpval;
        
        // iterate through a sequence using FileNodeIterator
        for( ; it != it_end; ++it, idx++ )
        {
            cout << "feature #" << idx << ": ";
            cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
            // you can also easily read numerical arrays using FileNode >> std::vector operator.
            (*it)["lbp"] >> lbpval;
            for( int i = 0; i < (int)lbpval.size(); i++ )
                cout << " " << (int)lbpval[i];
            cout << ")" << endl;
        }
    //
        fs2.release();
}
  • cpp(write)
// file write
#include <ros/ros.h>
#include "opencv2/opencv.hpp"
#include <time.h>
 
using namespace cv;
using namespace std;
 
int main(int argc, char** argv)
{
    ros::init(argc, argv, "demo_yaml_v2_write");
    ros::NodeHandle n("~");

    string path;

    //运行ros节点后,ros的相对路径是工作包所在工作空间的根目录
    n.param<std::string>("path", path, "./src/ros_demo/demo_yaml/demo_yaml_v2/config/test_write.yaml");

    FileStorage fs(path, FileStorage::WRITE);
 
    fs << "frameCount" << 5;
    time_t rawtime; time(&rawtime);
    fs << "calibrationDate" << asctime(localtime(&rawtime));
    Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
    Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
    fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
    
    //逐渐输入
    fs << "features" << "[";
    for( int i = 0; i < 3; i++ )
    {
        int x = rand() % 640;
        int y = rand() % 480;
        uchar lbp = rand() % 256;
 
        fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
        for( int j = 0; j < 8; j++ )
            fs << ((lbp >> j) & 1);
        fs << "]" << "}";
    }
    fs << "]";

    fs.release();
    return 0;
}
  • Cmakelist.txt
find_package(OpenCV REQUIRED)
#...
add_executable(demo_yaml_v2_read src/demo_yaml_v2_read.cpp)
target_link_libraries(demo_yaml_v2_read ${OpenCV_LIBS} ${catkin_LIBRARIES})

add_executable(demo_yaml_v2_write src/demo_yaml_v2_write.cpp)
target_link_libraries(demo_yaml_v2_write ${OpenCV_LIBS} ${catkin_LIBRARIES})

demo在以下网站

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值