2

I'm trying to calculate the difference between 2 images. I'm expecting an integer as my result, but I'm not getting what I expect.

from imageio import imread
#https://raw.githubusercontent.com/glennford49/sampleImages/main/cat1.png
#https://raw.githubusercontent.com/glennford49/sampleImages/main/cat2.png
img1="cat1.png" # 183X276
img2="cat2.png" # 183x276
numpyImg1=[]
numpyImg2=[]
img1=imread(img1)
img2=imread(img2)
numpyImg1.append(img1)
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0] 
result = sum(abs(diff)) 

print("difference:",result)

print:

# it prints an array of images rather than printing an interger only

target:

difference: <int>
rayryeng
  • 102,964
  • 22
  • 184
  • 193
max2p
  • 57
  • 1
  • 8
  • Difference in images could mean a lot of things. The image data you are loading using `imread` will be a ndarray with shape (height, width, channels). So if you have 3 RGB values for each pixel of the image, how do you want to define difference? – MYousefi Dec 25 '20 at 07:09
  • Now , I've seen this. Looks like it can give an integer result https://stackoverflow.com/questions/189943/how-can-i-quantify-difference-between-two-images – max2p Dec 25 '20 at 08:46

4 Answers4

0

You are using Python's built-in sum function which only performs a summation along the first dimension of a NumPy array. This is the reason why you are getting a 2D array as the output instead of the single integer you expect. Please use numpy.sum on your result instead which will internally flatten a multi-dimensional NumPy array then sum over the results. In addition, you might as well use numpy.abs for the absolute computation too:

import numpy as np

result = np.sum(np.abs(diff)) 

Using numpy.sum means that you no longer need to reshape your array into a flattened representation prior to using the built-in sum function in your answer. For future development, always use NumPy methods on any arithmetic operations you want to perform on NumPy arrays. It prevents unexpected behaviour such as what you've just seen.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
-1

A (Colored) image is a 3D matrix, so what you can do is convert those image in numpy array using numpy.array(image) and then you can get the difference of those two numpy arrays.

The final answer will be an array in 3-dimenssion

ASLAN
  • 629
  • 1
  • 7
  • 20
-1

I believe the dimension of numpy array is not 1, You need to perform the sum the number of times as the dimesion of the array to have a single sum value.

   [1,2,3]
    sum gives : 6

   [[1,2,3],[1,2,3]]
   sum gives : [2,4,6]
   doing a second sum opertion gives
     : 12 (single value)

you may need to add one more "sum(result)" before printing data (if image is 2 dimension) .

eg:

     numpyImg2.append(img2)
     diff = numpyImg1[0] - numpyImg2[0] 
     result = sum(abs(diff)) 

     result = sum(result) >> Repeat

     print("difference:",result)
Nishad C M
  • 142
  • 6
  • this is the result : difference: [191 14 25] ,almost near to my expected output – max2p Dec 25 '20 at 07:28
  • We can use reshape on numpy array to flatten it using -1 shape input. Please try the same. numpyImg2.append(img2) diff = numpyImg1[0] - numpyImg2[0] result = sum(abs(diff.reshape(-1))) print("difference:",result) – Nishad C M Dec 25 '20 at 07:44
  • It now gives me a number of several digits, but the answer should be not less than 20 or so because image1 and image2 are the same cat , only the background color is the difference – max2p Dec 25 '20 at 07:50
  • Looks like it can give me an integer result, the arguments arr for normalize confuses me, any exxplaination on this? https://stackoverflow.com/questions/189943/how-can-i-quantify-difference-between-two-images – max2p Dec 25 '20 at 08:51
-1

This is my answer of finding the difference of 2 images in rgb channels.

If 2 the same images were to be subtracted, prints: difference per pixel: 0

from numpy import sum
from imageio import imread

#https://github.com/glennford49/sampleImages/blob/main/cat2.png
#https://github.com/glennford49/sampleImages/blob/main/cat2.png
img1="cat1.png"
img2="cat2.png"
numpyImg1=[]
numpyImg2=[]
img1=imread(img1)
img2=imread(img2)
numpyImg1.append(img1)
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0] 
result = sum(diff/numpyImg1[0].size)
result = sum(abs(result.reshape(-1))) 
print("difference per pixel:",result)
max2p
  • 57
  • 1
  • 8