Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

yea, careful there ..

if you do something simple as Mat A,B; A=B; , then this is a shallow copy, then both Mats will point to the same data ! (another one, my favourite: vector<Mat> T(10,Mat(1,1,0)); same problem !)

here's a simplified example:

Mat A(1,1,CV_32F,1.0f);
Mat B = A;
cerr << A << " " << B << endl;
A = 3;
cerr << A << " " << B << endl;

[1] [1]
[3] [3] // yea, both.

to achieve a deep copy, use B=A.clone() or A.copyTo(B)

in your example above, you unfortunately reassign dest to itself (and then to B), so all your B's are indeed the same.

remedy:

either do a clone() (expensive):

for (int i = 0; i<2; i++) {
    for (int j = 0; j < 3; j++)  {
        dest = 2 * dest;
        B[i][j] = dest.clone();

or avoid the situation, by creating a new Mat header (the temp result from the multiplication):

for (int i = 0; i<2; i++) {
    for (int j = 0; j < 3; j++)  {
        B[i][j] = dest * (1<<((i+1)*(j+1)));