# peculiar matrix reference behaviour

Hi there, So this could very well be due to my lack of understanding of the matrix data type in opencv, so please let me know if that is the case. I have two vectors of Mat types and I am looping through all elements of all these matrices to find their respective norms as such:

    for (k = 0; k < inp.size(); k++)
{
for (i = 0; i < inp[k].rows; i++)
{

for (j = 0; j < inp[k].cols; j++)
{
Vec2f& elem = inp[k].at<Vec2f>(i, j);

a = elem[0];
b = elem[1];
c = elem[0];
d = -elem[1];
xx+= (a*c - b*d);

elem = modelp[k].at<Vec2f>(i, j);

a = elem[0];
b = elem[1];
c = elem[0];
d = -elem[1];
yy += (a*c - b*d);
}

}
}


So this should be a fairly simple operations. However, after this loop the two matrices become equal! I think I am messing up the references somehow and make each pixel in one matrix to point to that of the other matrix. But I dont see how this is happening! please help me out because I am very confused. Of course the problem is fixed if I make a new reference for the calculation of the norm of the second matrix, but I still need to know why this is happening.

Thank you J

edit retag close merge delete

2

you're assigning to elem, which is a reference to the pixel data in your input mat.

maybe you wanted:

 Vec2f &  elem2 = modelp[k].at<Vec2f>(i, j);

( 2015-03-27 02:00:11 -0500 )edit

oh i see what i did there lol. Thank you for your comment. Yes, what you wrote is exactly what I intended to do.

J

( 2015-03-27 18:45:24 -0500 )edit