Write data to text file

asked 2014-12-03 01:15:30 -0500

tarmizi_adam2005 gravatar image

Hi, I am trying to read a ".csv" file that contains double valued data like this:

1   0.0922279   0.0977822   0.0567845   0.0308806   0.0491464   0.0436114   
1   0.277745    0.130502    0.118348    0.0365346   0.0669739   0.00924114  
1   0.137181    0.140128    0.116455    0.122226    0.151289    0.129282    
1   0.182825    0.0810142   0.0811148   0.0588809   0.147732    0.0410181

The above is just an example. The real ".csv" file is 269 row by 3780 column (269 x 3780).

I try to read the csv file, put the values in a Mat and then write that Mat to another file called "data.txt".

However, when I open the data.txt nothing is written except a value of 1. This is obviously wrong as, the data to the file would have contained the same values of the original "csv" file. This is the code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp> // for CvMLData
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

int main()
{

    ofstream data("data.txt"); // 
    CvMLData mlData;
    mlData.read_csv("HOG_data.csv"); // File containing 269 row by 3780 column

    if(mlData.get_values() == nullptr)
    {
        cout << "Unable to open file !" << endl;
        return 1;
    }

    //const Mat m = mlData.get_values();  <---Also tried this;
    const CvMat *m = mlData.get_values();

    Mat dataset;
    dataset = m;

    for(int i = 0; i < dataset.rows; i++)
    {
        for(int j  =0; j < dataset.cols; j++)
        {
            data << dataset.at<float>(i,j) << " ";
        }

        data << endl;
    }

    waitKey();
    return 0;
}

Any suggestions or something i've probably overlooked ?

edit retag flag offensive close merge delete