Ask Your Question
1

Can't create CSV file

asked 2015-10-14 13:49:10 -0500

Mehdi.Am gravatar image

updated 2015-10-15 07:21:29 -0500

Can someone tell me how I can create CSV file?

edit retag flag offensive close merge delete

Comments

there's a little python script for that purpose: https://github.com/Itseez/opencv_cont...

berak gravatar imageberak ( 2015-10-15 05:47:20 -0500 )edit
2

mehdi, please edit the title back, and start a new question

berak gravatar imageberak ( 2015-10-15 07:15:05 -0500 )edit

@berak I did

Mehdi.Am gravatar imageMehdi.Am ( 2015-10-15 07:23:24 -0500 )edit

good ! (it just gets too confusing else)

berak gravatar imageberak ( 2015-10-15 07:24:37 -0500 )edit

@Mehdi.Am just on a sidenote, take a look at the FAQ because this question has by far not the shape that it should have on this forum according to the guidelines. I suggest that you make it more clear ... because people who have not read your other topics, have no clue what you want...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-16 03:53:53 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
5

answered 2015-10-14 17:08:39 -0500

theodore gravatar image

updated 2015-10-15 04:23:53 -0500

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();
// 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
edit flag offensive delete link more

Comments

2

No need to loop:

#include <fstream>

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

If using OpenCV 3.0, then format(matrix, "CSV") should be replaced with format(matrix, Formatter::FMT_CSV)

LorenaGdL gravatar imageLorenaGdL ( 2015-10-14 17:17:38 -0500 )edit
1

@LorenaGdL, perfect!!! I was not aware of that at all, thanks. Though it seems that in version 3.x, format() no longer takes a string, but an enumeration from the ones below:

FMT_DEFAULT
FMT_MATLAB
FMT_CSV
FMT_PYTHON
FMT_NUMPY
FMT_C

so writing part in your code should be outputFile << cv::format(matrix, cv::Formatter::FMT_CSV) << endl;, but still thanks for the info ;-)

theodore gravatar imagetheodore ( 2015-10-14 17:30:39 -0500 )edit

you've been faster than me :-p...

theodore gravatar imagetheodore ( 2015-10-14 17:31:36 -0500 )edit
1

haha yep, just after writing I remembered the change in 3.0 :P

LorenaGdL gravatar imageLorenaGdL ( 2015-10-14 17:34:40 -0500 )edit

can you tell me how I can use the function by an example code:

void saveMatToCsv(Mat &matrix, string filename)
Mehdi.Am gravatar imageMehdi.Am ( 2015-10-15 00:41:03 -0500 )edit

@theodore@LorenaGdL I faced this error could you plz solve it?I captured my error.please look at it: error pic

and this is my main funtion that I call it: main fun pic

Mehdi.Am gravatar imageMehdi.Am ( 2015-10-15 01:05:36 -0500 )edit
3

@Mehdi.Am 1) seems you're using the incorrect version of the function. Take a careful look at @theodore explanation once again; 2) saving a 3-channel images as it is may not be the best option. It's probably better to reshape to 1-channel, so you know how to undo the operation once you load back the data

LorenaGdL gravatar imageLorenaGdL ( 2015-10-15 02:35:30 -0500 )edit
2

@theodore: edited the answer, reading method in 2.x is valid. And just for your info, though what you post is perfectly working, you can do this too in 3.x reading:

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

(no need for hconcat)

LorenaGdL gravatar imageLorenaGdL ( 2015-10-15 02:38:47 -0500 )edit

@LorenaGdL May I ask you to edit my code?

int _tmain(int argc, _TCHAR* argv[])
{
    Mat img = imread("f.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    saveMatToCsv(img,"mycsv.csv");
    //fisherFaceTrainer();
    return 0;
}

//------------------

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

I use OpenCV 2.4

Mehdi.Am gravatar imageMehdi.Am ( 2015-10-15 02:46:25 -0500 )edit
1

@Mehdi.Am it seems correct to me. Are you sure you're correctly loading your image?

LorenaGdL gravatar imageLorenaGdL ( 2015-10-15 03:07:27 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-10-14 13:49:10 -0500

Seen: 3,708 times

Last updated: Oct 15 '15