# AbsDiff vs BitwiseXor?

I have been using the bitwiseXor so far to calculate the differences between layers. Now i noticed that opencv have a AbsDiff which perform the absolute difference. I run a simple test and noticed that the output is the same as using the bitwiseXor on my case. But since they are different functions when to use and what are the benefits from using one or another? Beside the possibility of using a mask on the bitwiseXor… Which one got more performance?

PS: My images are all grayscale 8bit and i don’t require a mask

they are not the same in general.

one is arithmetic. the other is operations on bits. they are not just different operations, the results are different for most values.

there is really no “choice” or “benefit” in which you “choose”. they have different purposes, in the sense that picking the other function will be mathematically wrong in most cases.

pick any two numbers.

``````>>> a,b = np.random.randint(0, 256, (2,1))
>>> print(a, bin(a[0]))
[214] 0b11010110
>>> print(b, bin(b[0]))
[153] 0b10011001
>>> cv.absdiff(a, b)
array([[61]], dtype=int32)
>>> cv.bitwise_xor(a, b)
array([[79]], dtype=int32)
``````

Thank you for clarifying, i wasn’t unable to spot the differences by eye, but i made a small script, in fact some values repeats and when not they are near but you are right.

My test:

``````import numpy as np

mat1 = np.random.randint(0,255, size=15)
mat2 = np.random.randint(0,255, size=15)
print("Mat1:", mat1)
print("Mat2:", mat2)

print("MatAbsDiff:", np.abs(mat1-mat2))
print("Mat1 ^ Mat2:", mat1^mat2)
``````

Output:

Mat1: [125 55 61 220 247 12 145 252 201 190 80 82 184 217 162]
Mat2: [221 144 20 126 14 232 233 115 132 24 183 247 17 17 230]
MatAbsDiff: [96, 89, 41, 94, 233, 220, 88, 137, 69, 166, 103, 165, 167, 200, 68]
Mat1 ^ Mat2: [160 167 41 162 249 228 120 143 77 166 231 165 169 200 68]

Thanks again