vector of matrix How do i initialize a vector of Matrix of type float. Each matrix has 5 rows and 1 column initialized to [0,0,0,0,0]

I could only get to vector<mat_<float>> random;

I have no clue how to initialize size of each Mat. Help thanks

edit retag close merge delete

if you can live with stack-based Matx types, it could be:

typedef Matx<float, 5, 1> Matx51f;
vector<Matx51f> V(123, Matx51f(0,0,0,0,0)); // 123 preallocated items

V(2) = 7; // access 3rd element of 4th Matx

(you can't do that with 'heap' based Mat or Mat_<float>, since the data from the initialization would be shared among all elements)

@berak you mean that :

Mat x=Mat::zeros(5,1,CV_32F);
vector<Mat> v;
for (int i=0;i<10;i++)
v.push_back(x);

because memory used by v is same that v, v...as push_back just make a field copy of x

I have a doubt Is my answer good?

1

@LBerger, imho, your answer below is the obvious and correct solution.

but yes, if you push_back() shallow copies of the same Mat, they all will point to same data. it's the same problem with: vector<Mat> V(123,Mat(5,1,CV_32F,0.0f)); // all point to same data

(i just wanted to show up an alternative, but thinking twice about it, i'll rather delete it for being too confusing)

2

@berak No don't delete I have learn something! After there is many ways to go somewhere.

1

Sort by » oldest newest most voted
vector<Mat> v;
for (int i=0;i<10;i++)
v.push_back(Mat::zeros(5,1,CV_32F));
more

Stats

Asked: 2015-07-09 03:04:12 -0500

Seen: 589 times

Last updated: Jul 09 '15