Ask Your Question
3

How to know if two Mat point to the same data?

asked 2013-05-30 02:55:30 -0500

yes123 gravatar image

updated 2015-09-04 02:43:00 -0500

I have two

Mat a,b;

Eventually in the code it could happen

a = b;

Is there any way to check whenever a and b points to the same data pointer (within code)?

(Without drawing stuff on one image and showing the other)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2013-05-30 03:22:08 -0500

berak gravatar image

to catch the case above, you just can check mat.data for equality:

Mat a=Mat::ones(3,3,CV_8U)*1;
Mat b=Mat::ones(3,3,CV_8U)*2;
cerr << "a" << a << " " << long(a.data) <<  endl;
cerr << "b" << b << " " << long(b.data) <<  endl;
cerr << "a==b " << (a.data==b.data) << endl;
b = a;   // same pointer
cerr << "b" << b << " " << long(b.data) <<  endl;
cerr << "a==b " << (a.data==b.data) << endl;

a[1, 1, 1;
  1, 1, 1;
  1, 1, 1] 3826464
b[2, 2, 2;
  2, 2, 2;
  2, 2, 2] 3826544
a==b 0
b[1, 1, 1;
  1, 1, 1;
  1, 1, 1] 3826464
a==b 1

also, there's another interesting case, where a and b don't share memory, but still have the same pixels:

Mat c=Mat::ones(3,3,CV_8U)*3;
cerr << "c" << c << " " << long(c.data) <<  endl;
cerr << "a==c " << (a.data==c.data) << endl;
c = a.clone(); // same pixels, different pointer
cerr << "a==c " << (a.data==c.data) << endl;
cerr << "a==c " << (a==c) << endl;
cerr << "a==c " << cv::norm(a,c,CV_L1) << endl;

c[3, 3, 3;
  3, 3, 3;
  3, 3, 3] 3826544
a==c 0
a==c 0
a==c [255, 255, 255;
  255, 255, 255;
  255, 255, 255]
a==c 0
edit flag offensive delete link more
2

answered 2013-05-30 03:13:02 -0500

Ben gravatar image

How about

if (a.data == b.data) {...}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-05-30 02:55:30 -0500

Seen: 1,579 times

Last updated: May 30 '13