Mat to vector

asked 2018-01-23 10:41:24 -0500

teenvan95 gravatar image

I am trying to copy elements from a Mat to a vector. I tried the below method, but the vector has weird values which I am sure are actually the memory values.

                vector<int> vec;
                Mat v = preds[n];
                vec.assign(v.begin<int>(), v.end<int>());
                //cout<<v<<"\n";
                cout<<vec[k]<<"\n";

I also tried at but that doesn't return anything.

edit retag flag offensive close merge delete

Comments

1

what does v.type() return ? you have to use the correct type for those iterators. (and, ofc., the vector)

berak gravatar imageberak ( 2018-01-23 10:44:12 -0500 )edit

It returns 5

teenvan95 gravatar imageteenvan95 ( 2018-01-23 10:46:21 -0500 )edit

you can try :

    vector<int> vec;
    Mat v = (Mat_<int>(1,6) << 0, 1.1, 2, 10, 11, 12);
    v.copyTo(vec);
    for (auto i : vec)
        cout << i << "\t";
    cout << " <- vec : vector<int>\n";
LBerger gravatar imageLBerger ( 2018-01-23 10:57:04 -0500 )edit

@teevan95 -- 5 means float, not int (which would have been 4)

so you have to change all the types from int to float

berak gravatar imageberak ( 2018-01-23 11:06:59 -0500 )edit