Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Matching Images

by BrowserUk (Pope)
on Nov 22, 2010 at 14:18 UTC ( #872970=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Matching Images
in thread Matching Images

Really? IMHO png and jpg use compression methods resulting in unpredictable information loss. IMHO converting them to a raw format will not often produce identical data.

Really. And to resolve your doubts please try it.

The "secret" such as it is, is that the raw format used for the comparison is neither jpg nor png, but libgd's own internal format, which is an uncompressed, lossless format. The images of all formats supported by GD are converted fully expanded into this internal format, and it is this binary representation, returned by the $image->gd method that is being compared in the code I posted. As such, even individual pixel differences are detected.

By way of demonstration, in my post above, I took a jpg image of a mandrill monkey and used an image editor to convert it to a compressed but lossless .png. I then compared those two images and no discrepancies were found.

By way of further demonstration, using the slightly expanded version below, I then edited the ,png version by changing the color of a single pixel on the mandrill's nose, from it's original near white color to pure red. and then compared that against the original .jpg:

c:\test>cmpImages mandrill.png mandrill-2.png images are different Process pixels? Difference at (665:482): [248 248 238] .v. [255 0 0]

As you can see, that single pixel difference was correctly detected by the raw binary compare, and then subsequently isolated using a pixel-by-pixel comparison. Please try it to convince yourself that it is correct.

But do bear in mind my warning above. If, for example, you were to take a ,png image and convert it to a lossy image format such as .jpg, then although the images may appear superficially similar to the human eye, they may be quite different in actuality. This due to the jpeg algorithms practice of substituting a single color for many, closely aligned but mathematically different colors in order to reduce palette sizes and aid compression.

Such lossy transforms will be correctly detected as different, even though cursory, by-eye inspections might adjudge them to be the same.

The expanded code:

#! perl -slw use strict; use GD; GD::Image->trueColor( 1 ); my $img1 = GD::Image->new( $ARGV[ 0 ] ); my $img2 = GD::Image->new( $ARGV[ 1 ] ); die "Different dimensions" unless $img1->width == $img2->width and $img1->height == $img2->height; my $raw1 = $img1->gd; my $raw2 = $img2->gd; if( $raw1 ne $raw2 ) { print "images are different"; printf "Process pixels? "; <STDIN>; for my $y ( 0 .. $img1->height -1 ) { for my $x ( 0 .. $img1->width -1 ) { my $p1 = $img1->getPixel( $x, $y ); my $p2 = $img2->getPixel( $x, $y ); if( $p1 != $p2 ) { print "Difference at ($x:$y): [@{[ $img1->rgb( $p1 ) ] +}] .v. [@{[ $img2->rgb( $p2 ) ]}]"; } } } } else { print "Images are the same"; } __END__

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.


Comment on Re^3: Matching Images
Select or Download Code
Re^4: Matching Images
by LanX (Canon) on Nov 22, 2010 at 14:31 UTC
    > and used an image editor to convert it to a compressed but lossless .png.

    my point was that in "reality" lossless conversions are the exception and not the rule. IIRC the compression factor of a JPG is freely choosable.

    Cheers Rolf

      my point

      If information is "lost" through a lossy conversion, the images are different, and the code I posted will detect those differences.

      Note well: The OP didn't ask to find images that "looked the same" or were "substantially similar", or any other form of fuzzy comparison.

      He asked for:

      I would like my script to compare two images and tell me if even one single pixel is slightly different in one of them.

      Which is exactly what the code I posted--as opposed to your theory--does.

      Hence, your "point" is pointless.


      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.
        Funny, this subthread is a good example for aggressiv arguments. =)

        Cheers Rolf

        PS: BTW I'm off ... 8)

        UPDATE:

        > He asked for:

        > I would like my script to compare two images and tell me if even one single pixel is slightly different in one of them.

        Indeed! But he didn't talk about different image formats either, leaving much place for interpretations and clarifications.

Re^4: Matching Images
by Gavin (Canon) on Nov 22, 2010 at 18:26 UTC

    "I then edited the ,png version by changing the color of a single pixel on the mandrill's nose, from it's original near white color to pure red."

    Neat!

    Where d'get them paint brushes man.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://872970]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-09-22 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (178 votes), past polls