从txt读取数据到Cvmat

来源:互联网 发布:厦门seo公司 编辑:程序博客网 时间:2024/06/11 11:08

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

view sourceprint?
#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)<<" ";
 
}
原创粉丝点击