Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Relational operators and Mat in OpenCV

📅 2014-Feb-20 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ mat, opencv, operator ⬩ 📚 Archive

Mat is a common structure used in OpenCV to store two and higher dimensional data. It is a derivation of the internal Array structure. A relational operator can applied between two Mat objects or a Mat object and a scalar, and the result is a 8-bit single-channel Mat.

Any of these operations is nothing but a call to compare function. This results in a per-element comparison of the Mat. The key information to remember is that for every element whose result is true, the value in the result Mat is set to 0xFF, that is 255. If result is false, it is set to 0. This behavior is so that the resulting Mat can be used as a mask image if needed for other operations.

The operators that are supported are equal, not-equal, greater, greater-or-equal, lesser and lesser-or-equal.

Example usage of such operations:

cv::Mat m0, m1;
cv::Mat m2(m0 == 0);
m2 = m0 > m1;
m2 = m1 <= m0;
m2 = m1 != 1;

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧