問題描述
我有兩個 cv::Mat 實例:m1 和 m2.它們具有相同的數字類型和大小.OpenCV 中是否有任何函數返回矩陣是否相同(具有所有相同的值)?
I have two instances of cv::Mat : m1 and m2. They are of the same numeric type and sizes. Is there any function in OpenCV that returns whether the matrices are identical (have all the same values)?
推薦答案
正如 Acme 提到的,你可以使用 cv::compare
雖然它不像你希望的那么干凈.
在以下示例中,使用 cv::compare"noreferrer">!=
運算符:
As mentioned by Acme, you can use cv::compare
although it is not as clean as you might hope.
In the following example, cv::compare
is called by using the !=
operator:
// Get a matrix with non-zero values at points where the
// two matrices have different values
cv::Mat diff = a != b;
// Equal if no elements disagree
bool eq = cv::countNonZero(diff) == 0;
據推測,通過比較元素進行迭代會更快嗎?如果您知道類型,您可以使用 STL equal 函數:
Presumably it would be quicker to just iterate through comparing the elements though? If you know the type you could use the STL equal function:
bool eq = std::equal(a.begin<uchar>(), a.end<uchar>(), b.begin<uchar>());
這篇關于如何在 OpenCV 中檢查兩個矩陣是否相同的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!