Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

this should do the trick:

Mat m = ...;
std::fstream outputFile;
outputFile.open( "file.csv", std::ios::out ) ;

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

}
outputFile.close( );

Unfortunately, FileStorage does not support .csv or plain .txt format and it does not seem to exist any other way rather than looping trough the matrix data.

this should do the trick:

Mat m = ...;
std::fstream outputFile;
outputFile.open( "file.csv", std::ios::out ) ;

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

}
outputFile.close( );

Unfortunately, FileStorage does not support .csv or plain .txt format and it does not seem to exist any other way rather than looping trough the matrix data.data. Please if someone has a better solution feel free to post it. It would interest me as well.

this should do the trick:

Mat m = ...;
std::fstream outputFile;
outputFile.open( "file.csv", std::ios::out ) ;

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

}
outputFile.close( );

Unfortunately, FileStorage does not support .csv or plain .txt format and it does not seem to exist any other way rather than looping trough the matrix data. Please if someone has a better solution feel free to post it. It would interest me as well.


UPDATE

Many thanks @LorenaGdL for her solution. So in a neater way you can do:

WRITING

OpenCV 2.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(matrix, "CSV") << endl;
    outputFile.close();
}

all the alternative are:

DEFAULT
MATLAB
CSV
PYTHON
NUMPY
C

OpenCV 3.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(m, cv::Formatter::FMT_CSV) << endl;
    outputFile.close();
}

all the alternatives are:

FMT_DEFAULT
FMT_MATLAB
FMT_CSV
FMT_PYTHON
FMT_NUMPY
FMT_C

READING

OpenCV 2.x (not tested)

CvMLData mlData;
mlData.read_csv("cameraFrame1.csv");
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true);
tmp->CvMat::~CvMat();

OpenCV 3.x

cv::Ptr<cv::ml::TrainData> raw_data = cv::ml::TrainData::loadFromCSV("test.csv", 0);
cv::Mat data = raw_data->getSamples();
hconcat(data, raw_data->getResponses(), data);

this should do the trick:

Mat m = ...;
std::fstream outputFile;
outputFile.open( "file.csv", std::ios::out ) ;

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

}
outputFile.close( );

Unfortunately, FileStorage does not support .csv or plain .txt format and it does not seem to exist any other way rather than looping trough the matrix data. Please if someone has a better solution feel free to post it. It would interest me as well.


UPDATE

Many thanks @LorenaGdL for her solution. So in a neater way you can do:

WRITING

OpenCV 2.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(matrix, "CSV") << endl;
    outputFile.close();
}

all the alternative are:

DEFAULT
MATLAB
CSV
PYTHON
NUMPY
C

OpenCV 3.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(m, cv::Formatter::FMT_CSV) << endl;
    outputFile.close();
}

all the alternatives are:

FMT_DEFAULT
FMT_MATLAB
FMT_CSV
FMT_PYTHON
FMT_NUMPY
FMT_C

READING

OpenCV 2.x (not tested)

CvMLData mlData;
mlData.read_csv("cameraFrame1.csv");
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true);
tmp->CvMat::~CvMat();
// optional if you have a color image and not just raw data
img.convertTo(img, CV_8UC3);
img= img.reshape(3); //set number of channels

OpenCV 3.x

cv::Ptr<cv::ml::TrainData> raw_data = cv::ml::TrainData::loadFromCSV("test.csv", 0);
cv::Mat data = raw_data->getSamples();
hconcat(data, raw_data->getResponses(), data);
// optional if you have a color image and not just raw data img.convertTo(img, CV_8UC3); img= img.reshape(3); //set number of channels
click to hide/show revision 5
No.5 Revision

this should do the trick:

Mat m = ...;
std::fstream outputFile;
outputFile.open( "file.csv", std::ios::out ) ;

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

}
outputFile.close( );

Unfortunately, FileStorage does not support .csv or plain .txt format and it does not seem to exist any other way rather than looping trough the matrix data. Please if someone has a better solution feel free to post it. It would interest me as well.


UPDATE

Many thanks @LorenaGdL for her solution. So in a neater way you can do:

WRITING

OpenCV 2.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(matrix, "CSV") << endl;
    outputFile.close();
}

all the alternative are:

DEFAULT
MATLAB
CSV
PYTHON
NUMPY
C

OpenCV 3.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(m, cv::Formatter::FMT_CSV) << endl;
    outputFile.close();
}

all the alternatives are:

FMT_DEFAULT
FMT_MATLAB
FMT_CSV
FMT_PYTHON
FMT_NUMPY
FMT_C

READING

OpenCV 2.x (not tested)

CvMLData mlData;
mlData.read_csv("cameraFrame1.csv");
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true);
tmp->CvMat::~CvMat();
// optional if you have a color image and not just raw data
img.convertTo(img, CV_8UC3);
img= img.reshape(3); //set number of channels

OpenCV 3.x

cv::Ptr<cv::ml::TrainData> raw_data = cv::ml::TrainData::loadFromCSV("test.csv", 0);
cv::Mat data = raw_data->getSamples();
hconcat(data, raw_data->getResponses(), data);
// optional if you have a color image and not just raw data
img.convertTo(img, CV_8UC3);
img= img.reshape(3); //set number of channels

this should do the trick:

Mat m = ...;
std::fstream outputFile;
outputFile.open( "file.csv", std::ios::out ) ;

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

}
outputFile.close( );

Unfortunately, FileStorage does not support .csv or plain .txt format and it does not seem to exist any other way rather than looping trough the matrix data. Please if someone has a better solution feel free to post it. It would interest me as well.


UPDATE

Many thanks @LorenaGdL for her solution. So in a neater way you can do:

WRITING

OpenCV 2.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(matrix, "CSV") << endl;
    outputFile.close();
}

all the alternative are:

DEFAULT
MATLAB
CSV
PYTHON
NUMPY
C

OpenCV 3.x

#include <fstream>

void saveMatToCsv(Mat &matrix, string filename){
    ofstream outputFile(filename);
    outputFile << format(m, cv::Formatter::FMT_CSV) << endl;
    outputFile.close();
}

all the alternatives are:

FMT_DEFAULT
FMT_MATLAB
FMT_CSV
FMT_PYTHON
FMT_NUMPY
FMT_C

READING

OpenCV 2.x

CvMLData mlData;
mlData.read_csv("cameraFrame1.csv");
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true);
tmp->CvMat::~CvMat();
// optional if you have a color image and not just raw data
img.convertTo(img, CV_8UC3);
img= img.reshape(3); //set number of channels

OpenCV 3.x

cv::Ptr<cv::ml::TrainData> raw_data = cv::ml::TrainData::loadFromCSV("test.csv", 0, -2, 0);
cv::Mat data = raw_data->getSamples();
hconcat(data, raw_data->getResponses(), data);
// optional if you have a color image and not just raw data
img.convertTo(img, CV_8UC3);
img= img.reshape(3); //set number of channels