从txt读取数据到Cvmat

前提已经得到txt的行列数目:

#include <cv.h>
#include <highgui.h>
#include <fstream> 
#include <iostream>
using namespace std;
int main()
{
	ifstream myfile( "E:\\feature_size.txt "); 
	if (!myfile.is_open()) 
		cout << "Unable to open file";
	int rows = 0,cols = 0;
	myfile >> rows >> cols;
    //cout << f_m << " " << f_n;
	//矩阵存取
    //ifstream feature_file( "E:\\features.txt ");
	CvMat *fc = cvCreateMat(rows,cols,CV_32FC1);;
	ifstream fin("E:\\features.txt ",std::ifstream::in);
	for(int i = 0;i < rows; ++i)
		for(int j = 0;j < cols; ++j)
		{
			fin >> fc->data.fl[i*cols+j];
		}         
		fin.close();
		for(int i = 0;i < rows; ++i)
			for(int j = 0;j < cols; ++j)
		       cout << cvmGet(fc,i,j)<<" ";

}

  

原文地址:https://www.cnblogs.com/xiangshancuizhu/p/2215504.html