Way to translation of Mat class operators defined in C++
This is a list of implemented matrix operations that can be combined in arbitrary complex expressions (here A, B stand for matrices ( Mat ), s for a scalar ( Scalar ), alpha for a real-valued scalar ( double )):
Note: In C++, the left-hand operand of compound assignment operators like “A += B
” is reused, and operations such as “Core.add(A, B, A)
” are performed internally. However, in C#, it is not possible to explicitly overload compound assignment operators. Instead, binary operator overloading is used implicitly, which results in a new Mat object being created and assigned to A each time an operator is used. This behavior leads to different memory management between C++ and C#.
c++ | OpenCVForUnity(C#) | |
---|---|---|
Addition, subtraction, negation: A+B, A-B, A+s, A-s, s+A, s-A, -A | ||
A + B | M1 + M2 | Core.add (M1, M2, M_dst) |
A – B | M1 – M2 | Core.subtract (M1, M2, M_dst) |
A + s | M1 + s | Core.add (M1, s, M_dst) |
A – s | M1 – s | Core.subtract (M1, s, M_dst) |
-A | -M1 | Core.multiply (M1, Scalar.all (-1), M_dst) |
Scaling: A*alpha A/alpha | ||
A * α | M1 * 3 | Core.multiply (M1, Scalar.all (3), M_dst) |
A / α | M1 / 3 | Core.divide (M1, Scalar.all (3), M_dst) |
Per-element multiplication and division: A.mul(B), A/B, alpha/A | ||
A.mul(B) | M1.mul(M2) | M1.mul (M2) |
A / B | M1 / M2 | Core.divide (M1, M2, M_dst) |
α / A | 3 / M1 | Core.divide (new Mat (M1.size (), M1.type (), Scalar.all (3)), M1, M_dst) |
Matrix multiplication: A*B | ||
A * B | M1 * M2 | Core.gemm (M1, M2, 1, new Mat (), 0, M_dst) |
Comparison: A cmpop B, A cmpop alpha, alpha cmpop A, where cmpop is one of : >, >=, ==, !=, <=, <. The result of comparison is an 8-bit single channel mask whose elements are set to 255 (if the particular element or pair of elements satisfy the condition) or 0. | ||
A > B | M1 > M2 | Core.compare (M1, M2, M_dst, Core.CMP_GT) |
A >= B | M1 >= M2 | Core.compare (M1, M2, M_dst, Core.CMP_GE) |
A == B | M1 == M2 | Core.compare (M1, M2, M_dst, Core.CMP_EQ) |
A != B | M1 != M2 | Core.compare (M1, M2, M_dst, Core.CMP_NE) |
A <= B | M1 <= M2 | Core.compare (M1, M2, M_dst, Core.CMP_LE) |
A < B | M1 < M2 | Core.compare (M1, M2, M_dst, Core.CMP_LT) |
Bitwise logical operations: A logicop B, A logicop s, s logicop A, ~A, where logicop is one of : &, |, ^. | ||
A & B | M1 & M2 | Core.bitwise_and (M1, M2, M_dst) |
A | B | M1 | M2 | Core.bitwise_or (M1, M2, M_dst) |
A ^ B | M1 ^ M2 | Core.bitwise_xor (M1, M2, M_dst) |
~A | ~M1 | Core.bitwise_not (M1, M_dst) |
Matrix assignment operators: | ||
C = A + B | M3 = M1 + M2 | Core.add (M1, M2, M3) |
A = s | M1 = s | M1.setTo (s) |