Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Can i Compare two images in perl ?

by Anonymous Monk
on May 30, 2011 at 22:15 UTC ( #907385=note: print w/replies, xml ) Need Help??


in reply to Can i Compare two images in perl ?

Compress the images three times: image1.zip, image2.zip both.zip.

Then calculate similarity = (size(image1.zip) + size(image2.zip)) / size(both.zip).

That value will approach 2.0 as the source bitmaps become more similar. No image processing required!

Replies are listed 'Best First'.
Re^2: Can i Compare two images in perl ? (It's a myth!)
by BrowserUk (Pope) on May 30, 2011 at 22:50 UTC

    This is one of those internet myths that just persists.

    According to your formula, this image and this one ought to approach 2.0. They are to all intents and purposes identical.

    The actual result:

    C:\test>jimagezipcmp.pl adding: 1.png (160 bytes security) (deflated 90%) adding: 2.png (160 bytes security) (deflated 89%) adding: 1.png (160 bytes security) (deflated 90%) adding: 2.png (160 bytes security) (deflated 89%) 1.0272952853598

    Try it yourself:

    #! perl -slw use strict; use GD; sub rgb2n { unpack 'N', pack 'CCCC', 0, @_ } for my $n ( 1, 2 ) { my $im1 = GD::Image->new( 600, 400, 1 ); $im1->filledRectangle( 0, 0, 600, 400, rgb2n( 255, 255, 255 ) ); $im1->filledRectangle( 100, 100, 500, 300, rgb2n( 255 - $n, 0, 0 ) + ); open O, '>:raw', $n . '.png' or die $!; print O $im1->png; close O; } system q[zip 1.zip 1.png]; system q[zip 2.zip 2.png]; system q[zip 3.zip 1.png 2.png]; my $size1 = -s '1.zip'; my $size2 = -s '2.zip'; my $size3 = -s '3.zip'; print +( $size1 + $size2 ) / $size3;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Those are PNGs, not uncomressed bitmaps.

      I certainly wouldn't expect that to work.

        Okay. I saved those same two pngs as bmps; deleted the 3 old zips; and then compressed the bmps per your post:

        C:\test>dir ?.bmp 31/05/2011 14:33 720,054 1.bmp 31/05/2011 14:33 720,054 2.bmp 2 File(s) 1,440,108 bytes C:\test>del 1.zip 2.zip 3.zip C:\test>zip 1.zip 1.bmp adding: 1.bmp (160 bytes security) (deflated 100%) C:\test>zip 2.zip 2.bmp adding: 2.bmp (160 bytes security) (deflated 100%) C:\test>zip 3.zip 1.bmp 2.bmp adding: 1.bmp (160 bytes security) (deflated 100%) adding: 2.bmp (160 bytes security) (deflated 100%) C:\test>dir ?.zip 31/05/2011 14:34 2,376 1.zip 31/05/2011 14:35 2,376 2.zip 31/05/2011 14:35 4,730 3.zip

        (2376 + 2376) / 4370 = 1.0874141876430205949656750572082.

        Still nowhere near the 2.0 you would expect if this worked.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907385]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-05-26 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?