tags:

views:

250

answers:

2

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?

A: 
vector < CvMat*> sample;

is a vector of pointers to CvMat, and you are continously pushing 'g' into it. You need to be creating new instances of CvMat and adding those to your vector, not adding 'g' to it every time.

jscharf
ahhh I got it. how did i miss that. thank u
Emre
+4  A: 

Since your sample vector is a list of pointers, you will need to make sure that you create a new instance of CvMat for each element that you add to the vector. Otherwise, it sounds like all your elements are pointing to the same thing (g).

If CvMat has a copy constructor, you may be able to fix it by doing this:

vector <CvMat> sample;
for(int x =0 ; x<29; x+=2) {
    // ...
    sample.push_back(*g);
}

This creates a vector of CvMat objects, and the push_back(*g) makes a copy of the matrix and pushes it on to the back of the vector.

Greg Hewgill
thx I got it now
Emre