OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Fri, 27 Mar 2015 18:45:24 -0500peculiar matrix reference behaviourhttp://answers.opencv.org/question/58465/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
JThu, 26 Mar 2015 17:46:22 -0500http://answers.opencv.org/question/58465/peculiar-matrix-reference-behaviour/Comment by houmanr for <p>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: </p>
<pre><code> 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);
}
}
}
</code></pre>
<p>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. </p>
<p>Thank you
J</p>
http://answers.opencv.org/question/58465/peculiar-matrix-reference-behaviour/?comment=58559#post-id-58559oh i see what i did there lol. Thank you for your comment.
Yes, what you wrote is exactly what I intended to do.
JFri, 27 Mar 2015 18:45:24 -0500http://answers.opencv.org/question/58465/peculiar-matrix-reference-behaviour/?comment=58559#post-id-58559Comment by berak for <p>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: </p>
<pre><code> 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);
}
}
}
</code></pre>
<p>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. </p>
<p>Thank you
J</p>
http://answers.opencv.org/question/58465/peculiar-matrix-reference-behaviour/?comment=58482#post-id-58482you'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);Fri, 27 Mar 2015 02:00:11 -0500http://answers.opencv.org/question/58465/peculiar-matrix-reference-behaviour/?comment=58482#post-id-58482