Fist of all I have to ask for what purpose do you want to use this function(finding difference between to images) for?? Here is a super simple example of compare images to detect value and suit of a card, from 2:50 it shows this https://www.youtube.com/watch?v=m-QPjO-2IkA
The method that you have used is a very very heavy computational method (RMS) that is supposedly to produce a very exact scientific measure of difference between to data sets.
Ur going to 1 require a massive amount of processing power but 2 more importantly your going to do what's known(in AI) as over fitting the data set. This is when you work out exact results on a data set when the data set has real world noise(not exact data).
First understanding the error in cameras taking pictures. You gets what's known as the salt and pepper effect. Take a pic then zoom in and you will see lots of pixels of white and pixels of black. You can take 2 pics in a row of the exact same scene and compare exact pixels and some of the exact same pixels could be white and some black giving you a big difference even tough the 2 images r the same pic. In most computer vision projects the first thing you do with an image is blur the image to remove the salt and pepper effect see https://docs.opencv.org/2.4/doc/tutoria ... ilter.html
Now for your code
Code: Select all
diff = ImageChops.difference(im1, im2)
h = diff.histogram()
sq = (value*(idx**2) for idx, value in enumerate(h))
sum_of_squares = sum(sq)
rms = math.sqrt(sum_of_squares/float(im1.size * im1.size))
Just remember you haven't converted to gray scale but r rather using a 3 channel image (BGR) so the line "sq = (value*(idx**2) for idx, value in enumerate(h))" the index will increment 3 times for each pixel so you may need "sq = (value*(math.ceil(idx/3)**2) for idx, value in enumerate(h))" and the last line might need a change like this " rms = math.sqrt(sum_of_squares/float(im1.size * im1.size*3))"
Again I would suggest that you think about a much simpler way to achieve what you need as this is super computationally heavy and will also have an over fitting problem