Ask Your Question

Matrix expressions comparison result

asked 2016-03-07 16:32:39 -0500

WJS gravatar image

I’m using the following matrix comparison to see if two 3 channel 8 bit color images are identical:

Mat result = image1 != image2;

Your documentation states: 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.

So, I was expecting the result matrix to be a single channel binary image. However, I find that result is actually a three channel matrix. If I split the result into three planes and count non-zero for each plane, and if each of the counts is zero then I know that the two images are identical. Do I misunderstand your documentation? Can someone clarify this for me?

Thank you, WJS

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2016-03-07 18:00:03 -0500

Tetragramm gravatar image

You are correct, the bitwise comparisons return the same size and type as the input Mat. So the output is one channel of binary results per channel of input.

The method you describe is indeed one way of checking.

If all you are checking is whether they are exactly identical, it maybe faster to use

cv::Scalar sum = cv::sum(result);
if(sum(0) + sum(1) + sum(2) == 0)//For a 3 channel image
{//Then Identical}
edit flag offensive delete link more


Tetragramm, Your suggestion is elegant in its simplicity! I will use it. WJS

WJS gravatar imageWJS ( 2016-03-08 07:19:35 -0500 )edit

Question Tools

1 follower


Asked: 2016-03-07 16:32:39 -0500

Seen: 73 times

Last updated: Mar 07 '16