memecs
memecs

Reputation: 7574

OpenCV arithmetic operations on matrices

Can someone explain the following, totally unintuitive, results:

Mat_<Vec3f> mat(Size(3,3),0);
Mat_<Vec3f> mat_add = (mat + 9);
Mat_<Vec3f> mat_add_div = (mat + 9) / 3;

magically, I don't have any other explanation for it:

mat_add =     [9,0,0,9,0,0,9,0,0];
mat_add_div = [3,3,3,3,3,3,3,3,3];

EDIT:

My take: this is a legacy bug that can't be fixed anymore because it is retroactive and will impact lot of projects. Also it would be pretty nasty to debug (except project has already pretty thorough unit testing)

Upvotes: 2

Views: 456

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50717

Mat_<Vec3f> mat_add = (mat + 9);

is equivalent to

Mat_<Vec3f> temp(Size(3,3),Vec3f(9,0,0));
Mat_<Vec3f> mat_add = mat+temp;

So, you will get

mat_add =
        [9, 0, 0, 9, 0, 0, 9, 0, 0;
          9, 0, 0, 9, 0, 0, 9, 0, 0;
          9, 0, 0, 9, 0, 0, 9, 0, 0]

However, I have no clue for why you will get values like that for mat_add_div. In fact, if you replace it by:

Mat_<Vec3f> mat_add_div = mat_add / 3;

You will end up with

mat_add_div =
        [3, 0, 0, 3, 0, 0, 3, 0, 0;
          3, 0, 0, 3, 0, 0, 3, 0, 0;
          3, 0, 0, 3, 0, 0, 3, 0, 0]

This result is however reasonable based on the same theory as said in above.

Upvotes: 2

Related Questions