本文介绍了在t ++ opencv中将txt文件中的值插入矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经阅读了一个文本文件,我想将其值插入矩阵中。这些值以行和列的形式分隔,我的数据是:



5.000000 2.400000 4.000000 1.000000 0.800000

113.000000 94.320000 54.000000 59.000000 118.400000

384.000000 474.460000 330.000000 54.000000 158.050000

178.000000 185.710000 46.000000 132.000000 279.200000

174.000000 218.780000 61.000000 113.000000 255.000000

0.000000 0.000000 0.000000 0.000000 0.000000



我将值插入:



I have read a text file now I want to insert its values into the matrix. The values are tab separated in the form of rows and columns my data is :

5.0000002.4000004.0000001.0000000.800000
113.00000094.32000054.00000059.000000118.400000
384.000000474.460000330.00000054.000000158.050000
178.000000185.71000046.000000132.000000279.200000
174.000000218.78000061.000000113.000000255.000000
0.0000000.0000000.0000000.0000000.000000

I am inserting the values as:

#include <conio.h>
#include <io.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <string>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
using namespace cv;
using namespace cv::ml;

Mat CreateVector()
{
int Samples = 50, fea= 7;
Mat Vector(Samples, fea, CV_32FC1);
std::ifstream infile("table.txt");	
try
{
std::string line;
int rowNum = 0;
while (std::getline(infile, line))
{
	std::istringstream iss(line);
	int a, b, c, d, e, f, g;
	if (!(iss >> a >> b >> c >> d >> e >> f >> g)) { break; } // error
	// process pair (a,b,c,d,e,f,g)
	String Values[50][7] = line.split(" ");
	for (int colNum = 0; colNum <= 10; colNum++)
	{
				
	Vector.put(rowNum, colNum, (int)FeatureValues[colNum]);
	Vector.at(rowNum, colNum, (int)FeatureValues[colNum]);
	Vector.insert(rowNum, colNum, (int)FeatureValues[colNum]);
	Mat::inserter (rowNum, colNum, (int)FeatureValues[colNum]);
	}
	rowNum++; //Enter the second vector Values
}
infile.close();	//Close 
}
catch (Exception ex)
{ }
}





请告诉我如何拆分选项卡上的值可逐行获取离散值。因为我在java中得到的代码如下所示:

String FeatureValues [50] [7] = line.split();



其次指导我如何在Matrix命名向量中插入我的csv文件的值



我尝试过:



我在广泛的谷歌搜索之后尝试使用四种方法将值插入到我的载体中,即



Vector.put (rowNum,colNum,(int)FeatureValues [colNum]);

Vector.at(rowNum,colNum,(int)FeatureValues [colNum]);

Vector.insert (rowNum,colNum,(int)FeatureValues [colNum]);

Mat :: inserter(rowNum,colNum,(int)FeatureValues [colNum]);



但是没有工作,而我猜put是java的函数,因为与java工作正常但我想在cpp



Kindly tell me how to split the values on tabs to get discrete values row by row. Since I am getting the codes in java which looks like this:
String FeatureValues[50][7] = line.split(" ");

Secondly guide me how to insert values of my csv file in Matrix named vector

What I have tried:

I have tried inserting the values into my vector using four ways yet after extensive google search i.e.

Vector.put(rowNum, colNum, (int)FeatureValues[colNum]);
Vector.at(rowNum, colNum, (int)FeatureValues[colNum]);
Vector.insert(rowNum, colNum, (int)FeatureValues[colNum]);
Mat::inserter (rowNum, colNum, (int)FeatureValues[colNum]);

but none is working while I guess put is function of java since working fine with java but I want in cpp

推荐答案

#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

void CreateVector(vector<vector<double> > &res)
{
	ifstream infile("table.txt");
	string line;
	while (getline(infile,line)) // Read a line
	{
		res.push_back(vector<double>()); // Add a new row to the matrix
		vector<double>& row = res.back();
		istringstream iss(line);
		double value;
		while (iss >> value) // Read columns
			row.push_back(value); // Add a column to the current row
	}
	infile.close();
}



要测试它,您可能需要一个函数来打印输出和主要功能:


To test it, you may need a function to print the output and the main function:

// This function is just for printing the result
void PrintVector(const vector<vector<double> > &res) {
	for (auto row : res) {
		for (auto value : row)
			cout << value << "\t";
		cout << endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<vector<double> > mat;
	CreateVector(mat);
	PrintVector(mat);
	char c;
	cin >> c;
}



矩阵表示为矢量数据结构的矢量(稀疏数组)。 PrintVector函数还应该让您了解如何迭代此数组的项目。


The matrix is represented as vector of vectors data structure (sparse array). PrintVector function should also give you an idea on how to iterate items of this array.


这篇关于在t ++ opencv中将txt文件中的值插入矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:53