Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check whether two matrices are identical in OpenCV

Tags:

c++

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)?

like image 211
kirkor Avatar asked Mar 28 '12 09:03

kirkor


People also ask

How can you tell if two matrices are identical?

For any two matrices to be equal, a number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal.

How do you check if two matrices are identical in Python?

Algorithm. Step 1: Create two matrix. Step 2: Then traverse every element of the first matrix and second matrix and compare every element of the first matrix with the second matrix. Step 3: If the both are same then both matrices are identical.

How do you compare two matrices?

If both matrix elements and size are equal, then it displays that the two matrices are equal. If size of matrix is equal but the elements are not equal, then it displays that the matrix can be compared but is not equal. If the size and elements are not matched, then it displays that the matrices cannot be compared.

How do you check if two matrices are equal in C++?

Approach. Iterate both matrices a[i][j] and b[i][j], and check a[i][j]==b[i][j] if true for all then print they are identical else print they are not identical.

Are the matrices of the same type identical in OpenCV?

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)? Show activity on this post.

How to check if two square matrices of size 4*4 are identical?

The below program checks if two square matrices of size 4*4 are identical or not. For any two matrices to be equal, a number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution.

Can two matrices be identical?

But only if you apply the principles laid out in these programs. Two matrices are said to be identical if both of them have the same number of rows, columns, and the same corresponding elements. In this article, you'll learn how to check if two matrices are identical using Python, C++, JavaScript, and C.

How to check if 2 CV::mat are equal?

If you need to check if 2 cv::Mat are equal, you can perform the AND XOR operator and check if the result is a cv::Mat full of zeros: Show activity on this post. The AND operator is not the good one for this task. If a matrix is all 0, it will always return true regardless if the other matrix is all 0 or not. The XOR must be used in this case.


1 Answers

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; 

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>()); 
like image 78
Tim MB Avatar answered Oct 14 '22 06:10

Tim MB