well i have most probably an extremly stupid problem but could not figure it out and I m about to lose my sanity hope someone can help
vector<CvMat*> sample;
for(int x = 0; x < 29; x += 2)
{
for(int b = 0; b < 22; b += 2)
{
cvmSet(g, 0, b, cvmGet(NormalVector, 0, x + b));
cvmSet(g, 0, b + 1, cvmGet(NormalVector, 0, x + b + 1));
}
sample.push_back(g);
}
Well i m using OpenCv for some matrix calculations basiacllay what I m doing is I m creating some small matrices from a big matrix and putting them into a Vector called “sample” in here.First loop is just a counter based thing and second loop for creating the small matrices after the second loop i m putting them to the vector
But the problem is after these loops when i try to reach one of the matrices in the vector I always get the one that was put into the vector at last.
I use these methods to access the vector elements
sample[0];
sample.at(6);
For these two I get the same matrix that was added to the vector at the end .What is the thing I am doing wrong?
Since your
samplevector is a list of pointers, you will need to make sure that you create a new instance ofCvMatfor each element that you add to the vector. Otherwise, it sounds like all your elements are pointing to the same thing (g).If
CvMathas a copy constructor, you may be able to fix it by doing this:This creates a vector of
CvMatobjects, and thepush_back(*g)makes a copy of the matrix and pushes it on to the back of the vector.