OpenCV arithmetic operations produce a cv::MatExpr
, for example:
MatExpr
operator+
(const Mat & a, const Mat &b);
I see that this is used to represent an expression before it is evaluated. What is the purpose of doing this?
In OpenCV the main matrix class is called Mat and is contained in the OpenCV-namespace cv. This matrix is not templated but nevertheless can contain different data types. These are indicated by a certain type-number. Additionally, OpenCV provides a templated class called Mat_, which is derived from Mat.
The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.
Let's say you have this expression: Mat A = 3 + B * 5;
where B
is also a Mat
. If the operators +
and *
where to return a Mat
, B * 5
would have create a temporary Mat
and then the +
operator would have create another Mat
. Instead, B * 5
returns a MatExpr
that doesn't actually creates a Mat
, it just "remembers" the operation it needs to peform. Then, the +
operator creates another MatExpr
and only the =
operator creates a Mat
, thus avoiding the temporary Mat
.
See https://en.wikipedia.org/wiki/Lazy_evaluation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With