Ask Your Question
0

Load a csv containing string

asked 2017-01-18 01:55:27 -0500

damiya14 gravatar image

I have a csv file that contains file paths.

eg:

col1  |         col2
 1    | 173/d234c34a87ffb7.jpg
 1    | 173/de9f04d2497222.jpg

I want to read this file and store it in opencv. How can i do it? I have tried the code below but it only reads col1 correct.

CvMLData cvml;
    cvml.read_csv("/home/damiya/test/compcars/sv_data/CSV/asd.csv");
    cvml.set_response_idx(0);
    const CvMat* vs = cvml.get_values();
    cout << "Rows: " << vs->rows << " Cols: " << vs->cols << endl;
    for(int i = 0; i < vs->rows; i++ )
    {
        cout << vs->data.fl[i*vs->cols] << "\n";

    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-01-18 02:33:09 -0500

berak gravatar image

updated 2017-01-18 02:40:00 -0500

even if you're using some outdated opencv2.4 version, try to avoid c-based datastructures, like CvMat , IplImage, CvMlData , and such.

then, while CvMlData can be used for numerical data, it won't work with strings at all.

try with vector<string> , and something manual:

vector<string> names;
vector<int> ids;
int id;
char sep;
string name;

ifstream csv("asd.csv");
getline(csv, name); // skip header line
while(csv.good() && (csv >> id >> sep >> name)) {
    ids.push_back(id);
    names.push_back(name);
}
edit flag offensive delete link more

Comments

Thank you!!

damiya14 gravatar imagedamiya14 ( 2017-01-18 02:55:51 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-18 01:55:27 -0500

Seen: 170 times

Last updated: Jan 18 '17