LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-03-2011, 05:44 AM   #1
aarsh
Member
 
Registered: Mar 2010
Location: ATL, GA
Distribution: openSUSE 11.2 , salckware ,ubuntu
Posts: 169

Rep: Reputation: 18
Question Python : Will any one explain me this code, please ?


import math, operator
from PIL import Image
def compare(file1, file2):
image1 = Image.open(file1)
image2 = Image.open(file2)
h1 = image1.histogram()
h2 = image2.histogram()
rms = math.sqrt(reduce(operator.add,
map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
return rms

if __name__=='__main__':
import sys
file1, file2 = sys.argv[1:]
print compare(file1, file2)



Can any one explain me this code, please ?

Last edited by aarsh; 04-03-2011 at 06:46 AM.
 
Old 04-03-2011, 06:00 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Well assuming you read all the entries where the script is located, maybe you would let us know what it is you do not understand?
 
Old 04-03-2011, 06:45 AM   #3
aarsh
Member
 
Registered: Mar 2010
Location: ATL, GA
Distribution: openSUSE 11.2 , salckware ,ubuntu
Posts: 169

Original Poster
Rep: Reputation: 18
the programmer found the histogram of two images then, squared the difference b/w them and lastly found the square root ! why so ?

Also I am looking for ward to have some Python code that compares two images regardless their dimensions and file formant.
All are welcomed for their suggestions ...
 
Old 04-03-2011, 07:17 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Python is sensitive to white spaces. I suppose you already indented your code, so put them in code tags. Its easier to read and debug
 
Old 04-03-2011, 07:23 AM   #5
aarsh
Member
 
Registered: Mar 2010
Location: ATL, GA
Distribution: openSUSE 11.2 , salckware ,ubuntu
Posts: 169

Original Poster
Rep: Reputation: 18
@ kurumi :
yeah , i know and have also done that , so this is just copy-paste.


please visit this page , I want to compare two images, which might be different in dimensions and file formant, but I am not getting what I want, please help .

http://effbot.org/zone/pil-comparing-images.htm
 
Old 04-03-2011, 09:36 AM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by aarsh View Post
the programmer found the histogram of two images then, squared the difference b/w them and lastly found the square root ! why so ?
To have any idea what that function is doing, you need a detailed understanding of what the histogram function is doing. I had no clue of that when I read your post, but with google I found the histogram section of the following page:
http://www.pythonware.com/library/pi...book/image.htm

Also, be aware that you left out a key step in describing the function you quoted: "squared the difference b/w them and lastly found the square root"

A Root Mean Square computation, such as the one you quoted, computes the differences element by element for the elements of two different aggregates. Then it squares each of those differences. Then it averages all those square together into one (which step you left out of your description). Then computes the square root.

One key requirement for any validity in RMS is that the elements correspond in meaning between the two aggregates. Element N of one aggregate should carry the same meaning as element N of the other.

In two pictures of the identical scene would you expect the 523'rd pixel of one to correspond to the 523'rd pixel of the other? Even with a camera held still, while snapping two pictures in a row, the image would be expected to shift by more than a pixel. As soon as the elements fail to correspond, the whole validity of RMS is gone.

The page you linked takes the pixel by pixel difference between two images (meaningless unless the pixels perfectly correspond) then takes a histogram (I have no clue why) then does the rest of the RMS computation. It is hard to imagine any value in the result.

The code you quoted instead takes the histogram on each image before computing the difference. That fixes any issue of pixel position failing to correspond, but at the cost of throwing away 100% of the shape information in the image, and throwing away almost all the color information (by segregating the primary colors as described in that page I linked).

There might be some meaning left in the RMS result, but I find it hard to imagine. The slightest change in overall brightness between the two images would mis align the histogram elements destroying almost all the meaning in the RMS.

What is the source of the two images you want to compare? You need to understand the nature of the differences you want to measure (and even more so the nature of the differences you want to ignore) in order to have any hope.

Quote:
Also I am looking for ward to have some Python code that compares two images regardless their dimensions and file formant.
IIUC, the histogram is in raw pixel counts, so you can't meaningfully compare histograms from different size images. That would be easy to fix by scaling the histogram to some proportional scale instead of leaving counts in raw pixels.

Quote:
but I am not getting what I want
I don't know what you want, but I suspect what you want is either impossible or far too difficult for you to achieve.

Comparing two pictures in the same format and dimension to see whether they are absolutely identical should be pretty easy.

But comparing two pictures to see whether or not they are very similar is almost impossible.

I don't know any of the math behind the methods used to measure the effectiveness of lossy compression. What matters in lossy compression is whether the result will look nearly the same to a human viewing it. But no math is known to measure that. Some mathematical methods are used to estimate the significance of the differences from compression. That may be very similar to your request, in which case you might attack that extremely difficult problem by finding someone else's solution.

Last edited by johnsfine; 04-03-2011 at 09:53 AM.
 
Old 04-03-2011, 10:37 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by aarsh View Post
@ kurumi :
yeah , i know and have also done that , so this is just copy-paste.
Edit your original post to use BBCode code tags: http://www.bbcode.org/examples/?id=15
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone help explain this code? MTK358 Programming 27 04-01-2010 10:44 PM
can someone explain this line of python to me? rabbit2345 Programming 2 11-01-2009 01:44 PM
can someone explain the 'for' function in python to me deathalele Programming 8 10-16-2008 12:10 PM
help to explain the shell code ! nillgump Linux From Scratch 1 10-11-2008 12:09 PM
explain me some c code alaios Programming 4 11-28-2005 12:27 PM


All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration