Ask Your Question
0

CV::MAT assignment operator "="

asked 2018-05-10 03:42:09 -0500

sjl gravatar image

updated 2018-05-10 04:41:33 -0500

berak gravatar image

I am confused by the following pseudocode:

cv::Mat a(row,col,datatype);

/*

codes fill data in a

*/

cv::Mat b(row,col,datatype);//allocate memory for b, b has same size with a

for(int i=0; i< a.rows; i++)
{
    b.row(i) = a.row(i);//for each line
}

does b shares the same date with a?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-05-10 04:03:40 -0500

berak gravatar image

this is a quite common pitfall.

Mat r = b.row(i);

makes a new Mat header, with data pointing to row i of the original Mat. so far, so good. but it's a copy !.

assigning another Mat to it will NOT change the original Mat ! so:

b.row(i) = a.row(i);

does not do anything (again, since you assign it to a copy)

a.row(i).copyTo(b.row(i));

would have done, what you probably expected. but since you try to do that for every row anyway, rather use a plain:

a.copyTo(b);

or even (same):

b = a.clone();
edit flag offensive delete link more

Comments

Thank you very much, with that pseudocode, I get strange result. Now I understand. thanks again!

sjl gravatar imagesjl ( 2018-05-10 04:31:15 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-10 03:39:32 -0500

Seen: 1,066 times

Last updated: May 10 '18