简体   繁体   English

从外部文件向ROS中的OpenCV C ++程序提供参数

[英]Provide parameters to OpenCV C++ program in ROS from external file

I want to write a program using OpenCV C++ interface in ROS . 我想在ROS中使用OpenCV C ++接口编写程序。 I want to provide parameters used by the program from external file(not sure which format will be apt). 我想从外部文件提供程序使用的参数(不确定哪种格式合适)。

Say, I use the threshold function in the program, instead of changing the threshold value each time in the program,make and then run , I want the program to fetch the threshold value from a file so that if I wish to change any value, I could do it in the external file . 说,我在程序中使用阈值函数, 而不是每次在程序中先更改阈值,先创建然后运行 ,我希望程序从文件中获取阈值,以便在希望更改任何值的情况下,我可以在外部文件中执行此操作

I want this because I think it will be helpful to a third-person who can just change the various parameters in the file and observe the result rather than opening the program, change values,make and run ( Sometimes the third person may not be a programmer and just wishes to see the results, in that case he would not know the technicalities of where to change parameters in the program ) 我想要这个是因为我认为这对仅更改文件中的各种参数并观察结果而不是打开程序,更改值,创建并运行的第三方 很有帮助有时第三方可能不是程序员,只希望看到结果,在那种情况下,他将不知道在程序中更改参数的技术性

Is there any way to do this in ROS ?? 有什么办法可以在ROS中做到这一点? Any method for just a C++ program requiring several parameters could also be suggested. 也可以建议仅需要几个参数的C ++程序的任何方法。

well, if it's just simple params: 好吧,如果只是简单的参数:

#include <fstream>

// write:
int param1 = 17;
float param2=2.5f;
ofstream o("params.txt");
o << param1 << endl;
o << param2 << endl;

// read:
int param1;
float param2;
ifstream i("params.txt");
i >> param1;
i >> param2;

similar idea, bit more advanced, for opencv stuff, use the FileStorage, it's a key-value store: 类似的想法,更高级,对于opencv东西,使用FileStorage,它是键值存储:

Mat face_labels;
Mat face_features;

cv::FileStorage fs("face.yml",cv::FileStorage::WRITE);
fs << "L" << face_labels;
fs << "F" << face_features;

cv::FileStorage sf("face.yml",cv::FileStorage::READ);
sf["L"] >> face_labels;
sf["F"] >> face_features;

You can do this via ROS parameters. 您可以通过ROS参数执行此操作。 See the roscpp params tutorial and the more detailed roscpp params overview . 请参阅roscpp params教程和更详细的roscpp params概述 You probably want to go ahead and use the NodeHandle::param(...) form as it makes it easy to specify defaults. 您可能想继续使用NodeHandle :: param(...)表单,因为它很容易指定默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM