Mat to vector [closed]

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

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 reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-08 08:01:03.858856

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 -0600 )edit

It returns 5

teenvan95 gravatar imageteenvan95 ( 2018-01-23 10:46:21 -0600 )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 -0600 )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 -0600 )edit