Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 2016-09-02 02:18:37 -0600

Nbb gravatar image

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.

std::ifstream file("img1.txt");

Mat depthImg;
int i = 0;
std::string line;
while (std::getline(file, line)) {  

    std::istringstream stream(line);

    double x;
    while (stream >> x) {

        depthImg.push_back(x);
    }

}

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.please. Data separated by commas

std::ifstream file("img1.txt");

Mat depthImg;
int i = 0;
std::string line;
while (std::getline(file, line)) {  

    std::istringstream stream(line);

    double x;
    while (stream >> x) {

        depthImg.push_back(x);
    }

}

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 i = 0;
std::string line;
while (std::getline(file, line)) {  

    std::istringstream stream(line);

    double x;
    while (stream >> x) {

        depthImg.push_back(x);
    }

}

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 i rows = 0;
std::string line;
while (std::getline(file, line)) {  

    std::istringstream stream(line);

    double //double x;
    while //while (stream >> x) {

        depthImg.push_back(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);