How to Check Whether Two Matrices Are Identical in Opencv

Compare 2 cv::Mat

If you need to compare 2 cv::Mat by sizes, then you might check

if(mat1.size() == mat2.size())
//do stuff
else
//do other stuff

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:

cv::bitwise_xor(mat1, mat2, dst);        
if(cv::countNonZero(dst) > 0) //check non-0 pixels
//do stuff in case cv::Mat are not the same
else
//do stuff in case they are equal

How to compare two Mats columns in OpenCV

You can check if two matrix are identical as explained here.

You can pass the columns instead of the whole matrix:

bool are_equal(const cv::Mat& a, const cv::Mat& b)
{
return (cv::sum(a != b) == cv::Scalar(0, 0, 0, 0));
}

int main()
{
cv::Mat3b a(3, 3);
cv::Mat3b b(3, 3);

cv::randu(a, 0, 9);
cv::randu(b, 0, 9);

bool same_columns = are_equal(a.col(1), b.col(2));
}

Compare if BGR image is exactly the same

You can do this:

Mat diff_im = image - copy;
double s = cv::sum(diff_im)[0];

OpenCV - Fastest method to check if two images are 100% same or not

You can use a logical operator like xor operator. If you are using python you can use the following one-line function:

Python

def is_similar(image1, image2):
return image1.shape == image2.shape and not(np.bitwise_xor(image1,image2).any())

where shape is the property that shows the size of matrix and bitwise_xor is as the name suggests. The C++ version can be made in a similar way!

C++

Please see @berak code.


Notice: The Python code works for any depth images(1-D, 2-D, 3-D , ..), but the C++ version works just for 2-D images. It's easy to convert it to any depth images by yourself. I hope that gives you the insight! :)

Doc: bitwise_xor

EDIT: C++ was removed. Thanks to @Micka and @ berak for their comments.

Take elements that match from a pair of arrays (C++/OpenCV)

The compare function produces an output array with a value set to 255 if the corresponding comparison returns true.

What you could do is use a two-step process:

First, perform two individual comparisons to get two arrays with the respective true values.
Second, combine the two matrices using a Matrix Expression:

cv::Mat result0;
cv::Mat result1;

cv::compare(maskMat, cv::GC_FGD, result0, cv::CMP_EQ); // Compare for equality to 1.
cv::compare(maskMat, cv::GC_PR_FGD, result1, cv::CMP_EQ); // Compare for equality to 3.

cv::Mat result = cmp0 | cmp1; // Combine the result of the comparisons
// by using bit-wise OR.

The bit-wise OR is a matrix expression that calculates the output matrix by performing the bit-wise operation on the two input matrices. Since the two input matrices are just composed of 0 and 255 values, this effectively creates the output you desire.

You could also replace the compare function by the appropriate matrix expressions, which are just ==:

cv::Mat cmp1 = maskMat == 1; // Compare for equality to 1.
cv::Mat cmp3 = maskMat == 3; // Compare for equality to 3.

cv::Mat result = max(cmp0, cmp1); // Combine the result of the comparisons
// by using the max function.

The max function used above works equally as well as the bit-wise OR in this case.

Since matrix expressions can be arbitrarily combined, you could actually put it all on a single line:

cv::Mat result = (maskMat == 1) | (maskMat == 3);

Hope that helps!

Subtract Matrices row-wise in OpenCV

I solved it the following way:

Mat A(10,2, CV_64F);
Mat B(1,2, CV_64F);
Mat C(0,2, CV_64F);
Mat D(0,2, CV_64F);

for(int i=0;i<A.rows;i++)
{
C=(A.row(i)-B.row(0));
D.push_back(C.row(0));
}

cout<<"A\n"<<A<<endl;
cout<<"B\n"<<B<<endl;
cout<<"C\n"<<C<<endl;
cout<<"D\n"<<D<<endl;


Related Topics



Leave a reply



Submit