Can't create CSV file
Can someone tell me how I can create CSV file?
Can someone tell me how I can create CSV file?
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.
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
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, 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@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 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
@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 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
Asked: 2015-10-14 13:49:10 -0600
Seen: 6,258 times
Last updated: Oct 15 '15
How to reduce false positives for face detection
OpenCV DescriptorMatcher matches
Record/Store constant refreshing coordinates points into notepad
Conversion between IplImage and MxArray
How to save opencv data in Excel format?
How can solvePnPRansac be used with double values?
Combine SIFT with other method for object recognition
there's a little python script for that purpose: https://github.com/Itseez/opencv_cont...
mehdi, please edit the title back, and start a new question
@berak I did
good ! (it just gets too confusing else)
@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...