Read text file into mat
Hi, my text file contains 640 rows 480 columns of floating points. Id like to read them into a matrix and reshape at the end. But the code below only reads a single data per row. Help me edit the stream >> x please. Data separated by commas
example 2.752,2.7521,2.7522,2.7524,2.7528,2.7532,2.7536,2.7542,2.7548,2.7555,2.7562,2.7569,2.7576,2.7583,2.7591,
std::ifstream file("img1.txt");
Mat depthImg;
int rows = 0;
std::string line;
while (std::getline(file, line)) {
std::istringstream stream(line);
//double x;
//while (stream >> x) {
//depthImg.push_back(x);
//}
while (stream)
{
string s;
if (!std::getline(stream, s, ',')) break;
depthImg.push_back(std::stof(s));
}
rows++;
}
// reshape to 2d:
depthImg = depthImg.reshape(1, rows);
Hi thanks ! the data is saved using matlab. im not sure how to replace commas with white spaces in matlab and I thought it'd be easier to just read it in C++/OpenCV
see answer below.