0

I was trying to calculate similarity using MSE for the following images:

A: A B: B C: C

All the images are different but clearly the 1st & 3rd are the most similar. However, when I try to compute the MSE using numpy subtraction:

error = np.sum((imageA - imageB) ** 2)
error /= float(imageA.shape[0] * imageA.shape[1]) 

images A&B receive the smallest error (5.5 vs 7.7) which would be incorrect. However, if I convert the images to floats then do the subtraction

err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])

I get the right answer with A&C having the smallest error (1607 vs 24422). What would be the reason for this? The article this method is from mentions its to avoid "modulus operations wrapping around" but I also used cv2.subtract which I thought avoided that issue.

user491234
  • 13
  • 2

1 Answers1

0

There is not enough data to fully answer this question, but I believe this discrepancy is caused by a representation issue: probably your images are uint8 type so the subtraction of those can cause problems of overflow.

YoniChechik
  • 1,397
  • 16
  • 25