-3

From the link: https://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html

operator+ is not overloaded in the definition of Mat class. However, oprator+ can be used to add two objects of class Mat. It really confuses me.

Jogging Song
  • 573
  • 6
  • 28
  • Where does it say that `operator+` can be used to add two objects of class `Mat`? Are you using the same version of opencv as the docs? Your question is very vague... – Rikus Honey Jul 11 '20 at 03:23
  • In my program using OpenCV, I can add two Mat objecs without any problem. https://stackoverflow.com/questions/28590031/add-the-contents-of-2-mats-to-another-mat-opencv-c It is easy to find such example over the internet. – Jogging Song Jul 11 '20 at 03:33

1 Answers1

1

By consulting the cv::Mat documentation, the functionality you are looking for is under operator=(const MatExpr& expr): https://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html#a2a0798475170fb3a23b5bc63686de334

Specifically, this section of the documentation is useful (emphasis mine):


This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters

expr: Assigned matrix expression object. As opposite to the first form of the assignment operation, the second form can reuse an already allocated matrix if it has the right size and type to fit the matrix expression result. It is automatically handled by the real function that the matrix expressions is expanded to. For example, C=A+B is expanded to add(A, B, C), and add takes care of automatic C reallocation.


Therefore, what is happening is that the overloaded operator for assignment (=) is what is being called, and a MatExpr expr, which is a MatExpr expression is being evaluated on the right hand side of it. It so happens that the expression being evaluated is the addition operator, so the addition of the two cv::Mat is performed which then creates a new matrix on the left-hand side of the expression.

The moral of this story is to search through the documentation to find your answer. That's how I found out about it.

rayryeng
  • 102,964
  • 22
  • 184
  • 193