# How to output the difference between `image1 - image2` in value?

Hello friendos!

How may I get the value of a subtraction between two images? It can definitely display the difference, since there is even an output image which shows which parts of the picture is different from another.

I would appreciate your kind intellect to assist me

please show, what you tried so far

1 Like

The value is a matrix, which you can view as a image, but obviously you could print out the values as well (though a million numbers in a listing is challenging for humans to interpret), or figure out some summary function that suits your goals, and make do with outputting one number.

1 Like

Super simply explained this code takes two images (and removes most of the pictures colours, since I’m mostly interested in shapes) and subtracts them from each other

``````import cv2

gx = cv2.cvtColor(ix, cv2.COLOR_BGR2GRAY)
gy = cv2.cvtColor(iy, cv2.COLOR_BGR2GRAY)

tmin = 192
tmax = 255
_, tx = cv2.threshold(-gx, tmin, tmax, cv2.THRESH_BINARY)
_, ty = cv2.threshold(-gy, tmin, tmax, cv2.THRESH_BINARY)
sb = tx - ty
out = sb

while True:
cv2.imshow("Output :)", out)

key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
``````

Here are the two images, for reference:

Of course, you might want the output, and I got you here you go:

Please excuse my late reply, I had some technical difficulties

1 Like

You’re so right! I’ve tried with something like this, however with no luck but what you said will be useful I need to make a “summary function” somehow if there is not a way to do this Thank you so much for the help

you have to be careful with numpy arrays, some operations may lead to unexpected results, like:

``````a = np.array([3,1,7], np.uint8)
b = np.array([7,1,9], np.uint8)
print(a-b)

[252   0 254]
``````

(it does not truncate, but “wraps around” !)

maybe you want to use absdiff() instead

1 Like

Thank you so much for the info! Can you help me about how may I use `absdiff` in this situation? Tried making a material, but it didn’t return anything

absdiff will take the absolute difference or subtraction without signs:

absdiff(imageA, imageB, result)
imshow(“Results of difference”, result)

this is C++ format, Python may just return “result”… try and see.