基于Opencv的文本数据与YML文件的转换程序

#include #include #include #include #include #include #include #include #include #include /***********************************************************/using namespace std;using namespace cv;/***********************************************************//从文本文件中读取数据,并以矩阵Mat的形式存储到YML文件;//文件中一行数据代表一组特征向量,且相邻分量的默认分割符为一个空格;//该函数接受参数:文本文件名和分割符;***********************************************************/void txtConvertToMat(string& Filename, char& delimiter){fstream fs(Filename.data(),ios::in);char data[100];fs.getline(data,100);bool flags = false;vector<vector> hu;while(data != NULL){//cout << data << endl;int count = 0;int pos = 0;int length = sizeof(data);char tmp[20] = "\0";cout.setf(ios::fixed);cout.precision(6);vector huTmp;for(int i = 0; i < length; i++){if(data[i] == '\0'){if(count == 0){flags = true;break;}tmp[pos+1] = '\0';//f[count] = atof(tmp);double d = atof(tmp);huTmp.push_back(d);hu.push_back(huTmp);huTmp.clear();pos = 0;cout << d << endl;count = 0;break;}else if(data[i] == delimiter){tmp[pos+1] = '\0';//cout << tmp << endl;//f[count] = atof(tmp);double d = atof(tmp);huTmp.push_back(d);pos = 0;count++;cout << d << " ";}else{tmp[pos] = data[i];pos++;}}cout << "------------------------" << endl;if(flags)break;fs.getline(data,100);}int huSize = hu.size();Mat m = Mat(huSize,7,CV_32FC1);for(int i = 0; i < huSize; i++){for(int j = 0; j < 7; j++){m.row(i).col(j) = hu[i][j];}}cout << "|***" << m.size() << "***|" << endl;char xmlFilename[100];cout << Filename << endl;int filenameLength = Filename.size();string tmpp = Filename.substr(0,filenameLength-4);tmpp = tmpp + ".xml";cout << tmpp << endl;FileStorage filel(tmpp,FileStorage::WRITE);filel << "Hu" << m;filel.release();for(int i = 0; i < huSize; i++){for(int j = 0; j < 7; j++){cout << m.at(i,j) << " ";}cout << endl;}}int main(){VideoCapture cap(0);Mat frame;Mat pre;Mat cur;cap >> frame;pre = Mat::zeros(frame.size(),CV_8UC1);cur = pre.clone();char file[100];/******************************************/string filename = "data_handV.txt";char deliter = ',';txtConvertToMat(filename, deliter);system("pause");return 0;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值