# Absolute difference between images.

Hi,

I have two images that seem to be similar at a first sight. I have the following script that calculates the % difference of imgB - imgA. If the error is 0.0 then it calculates the % difference of imgA - imgB.

For this pair of images that seem similar the difference imgB - imgA gives 0.0 but the imgA - imgB gives 26%. How can I see what causes this difference?

How can I calculate the absolute difference of the two images?

## here is my code:

import numpy as np
from SimpleCV import Image

sourceimg = Image('44856.png')
target
img = Image('44856_target.png')

diffimg = targetimg - source_img

matrix = diff_img.getNumpy()
flat = matrix.flatten()

numchange = np.countnonzero(flat)
percentchange = 100 * float(numchange) / float(len(flat))
print percent_change

if percentchange == 0:
diff
img = sourceimg - targetimg
matrix = diffimg.getNumpy()
flat = matrix.flatten()
num
change = np.countnonzero(flat)
percent
change = 100 * float(num_change) / float(len(flat))

print percentchange
diff
filename = 'diff.png'
diffimg.save(difffilename)

edit retag close merge delete

Sort by » oldest newest most voted
img_diff=(imgA-imgB)+(imgB-imgA)


This works because of the way subtraction of color values occur in simplecv. If the difference is less than zero, it is rounded to 0.

So, if a (x,y) pixel in imgA and imgB be (25,100,75) and (50,70,30), then for that pixel:

imgA-imgB = (0,30,45)
imgB-imgA = (25,0,0)
(imgA-imgB)+(imgB-imgA) = (25,30,45)

more

It works indeed! Thanks a lot!

more