Fill a vector<vector<Mat>>
I want to fill a vector<vector<mat>> B with a Mat dest, this matrix will change in a for loop. The problem is B[0][0] is the same that B[last_number][last_numer]. Here is the code:
vector<vector<Mat>> B;
for (int i = 0; i<2; i++) {
B.push_back(vector<Mat>());
for (int j = 0; j < 3; j++)
B[i].push_back(Mat::zeros(10, 10, CV_64F));
}
Mat dest = Mat::ones(10, 10, CV_64F);
for (int i = 0; i<2; i++) {
for (int j = 0; j < 3; j++)
{
dest = 2 * dest;
B[i][j] = dest;
}
}
cout << B[0][0];
An this is the output:
[64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64;
64, 64, 64, 64, 64, 64, 64, 64, 64, 64]
as berak has well explained your problem is shallow copy. Here I would just suggest a different method to declare your matrix. You might avoid
push_back
which causes an automatic reallocation of the allocated storage space. When it's possible I prefer to construct a container withN x M
elements like below: