Ask Your Question
0

insert elements of vector in cols of a mat

asked 2017-01-16 07:40:36 -0500

alexander33 gravatar image

I have a vector of floats and i want insert the element of this vector in the cols of the Mat. reading the doc doc of opencv says that when I pass a vector like parameter in the MAt constructor, The matrix has a single column and the number of rows equal to the number of vector elements. I want just that the matrix has a single row and the numbers of cols equal to the number of vector elements.

vector<float> descriptorsValues;
Mat fm = Mat(descriptorsValues);

thanks for the help !!!!!!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-01-16 07:49:58 -0500

pi-null-mezon gravatar image

To construct your Mat on vector's data:

Mat fm = Mat(Size(descriptorValues.size(),1), CV_32FC1, (void*)&descriptorsValues[0]);

If fm should live independently of vector also call clone():

Mat fm = Mat(Size(descriptorValues.size(),1), CV_32FC1, (void*)&descriptorsValues[0]).clone();
edit flag offensive delete link more

Comments

Works very fine Thanks......

alexander33 gravatar imagealexander33 ( 2017-01-16 10:48:35 -0500 )edit
0

answered 2017-01-16 08:21:29 -0500

berak gravatar image

updated 2017-01-16 08:30:06 -0500

you can use reshape , to adjust it without copying data internally (it just changes rows/cols members of the Mat):

// demo vector
vector<float> f {1,2,3,4,5};
Mat m(f, true); // deep copy (if you wanted that, - use false, if you did not) !

// now we have a single column:
cerr << m << endl;
[1;
 2;
 3;
 4;
 5]

// let's reshape it to a single row:
m = m.reshape(1,1);
cerr << m << endl;
[1, 2, 3, 4, 5]

// and, just for the show, back to a column:
m = m.reshape(1, m.total());
cerr << m << endl;
[1;
 2;
 3;
 4;
 5]
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-01-16 07:40:36 -0500

Seen: 1,491 times

Last updated: Jan 16 '17