## Weird return of RMS

thunderstorm
Posts: 8
Joined: Sat Sep 02, 2017 9:39 pm

### Weird return of RMS

I get a return of 0x74b9337 when comparing two images with the code below from here:
http://code.activestate.com/recipes/577 ... wo-images/
I was expecting a number like 25.2 or something.
Thanks everyone. Btw, I didnt want to interrupt the previous question.

Code: Select all

``````from PIL import Image, ImageChops
import math, operator

im1 = ('/home/pi/Desktop/1.png')
im2 = ('/home/pi/Desktop/2.png')

def rmsdiff_2011(im1, im2):
"Calculate the root-mean-square difference between two images"
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[0] * im1.size[1]))
return rms
``````
Last edited by thunderstorm on Sat Oct 14, 2017 8:27 pm, edited 2 times in total.

OutoftheBOTS
Posts: 640
Joined: Tue Aug 01, 2017 10:06 am

### Re: Weird return of RMS

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

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[0] * im1.size[1]))``````
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[0] * im1.size[1]*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