Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Can i Compare two images in perl ?

by BrowserUk (Pope)
on May 31, 2011 at 14:58 UTC ( #907472=note: print w/replies, xml ) Need Help??


in reply to Can i Compare two images in perl ?

As an example of what can be achieved without "image processing".

This simple script compares two images and produces a single xx.xxx% similarity figure:

#! perl -slw use strict; use GD; GD::Image->trueColor( 1 ); my $im1 = GD::Image->new( $ARGV[ 0 ] ); my $im2 = GD::Image->new( $ARGV[ 1 ] ); my $raw1 = $im1->gd; my $raw2 = $im2->gd; my $xored = $raw1 ^ $raw2; my( $all, $diff ) = (0)x2; $all += 255, $diff += ord substr $xored, $_, 1 for 0 .. length( $xored + ) - 1; print $all, ' ', $diff; printf "The simlarity is %.3f%%\n", ( $all - $diff ) / $all * 100;

And here are the results of applying that to my previous 'problem' examples.

  • The 'spot-the-differences' ballons A & B:
    C:\test\xx>..\907337 ballons1.jpg ballons2.jpg 672410265 62443267 The simlarity is 90.714%
  • The 'identical except slight color fade' ballons; A & A':
    C:\test\xx>..\907337 ballons1.jpg ballons1a.jpg 672410265 92011553 The simlarity is 86.316%
  • The two BrowserUk late period variations of "Red rectangle on white background"; X & Y:
    C:\test\xx>..\907337 1.png 2.png 244802805 241803 The simlarity is 99.901%
  • And perhaps a more poignant example. MicroArray1 & MicroArray2:
    C:\test\xx>..\907337 microarray1.jpg microarray2.jpg 340764405 41409302 The simlarity is 87.848%

    In this example, the second microarray is just a mirror image of the first for test purposes as I couldn't find two similarly size examples. The reflection means that the registration is probably not as good as you'd expect from a true comparison, but it serves its purpose to highlight the possibilities and downsides of the technique.

The technique could be much improved with good examples and a clearer statement of requirements.


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.

Replies are listed 'Best First'.
Re^2: Can i Compare two images in perl ?
by deep3101 (Acolyte) on Jun 01, 2011 at 02:27 UTC

      That's interesting. But only 1/4 of the information required to tackle the problem.

      Only one pair of images, and no reference as to you would rate them--are they 2% similar or 98% similar?

      Isolating the pink in the images is relatively simple: cluster1 & cluster8.

      One problem is that the graphs have been produced using anti-aliasing. A process that trades accuracy for aesthetic appearance. Which means that if you try to subtract one image from another you get a big blurry streak of color: cluster1 - cluster8

      With some more processing it should be possible to retrieve a single, contiguous line from those fat, fuzzy, discontinuous streaks. Albiet that it might take several passes to do so.

      But the show stopper as far as achieving your single similarity figure is the lack of any indication of how to rate the two posted images for similarity.

      It is obvious that if the two lines, once reduced to continuous, single pixel wide lines, exactly overlaid each other, that would constitute 100% similarity. But how do you rate divergence?

      Possibilities:

      1. You might consider any two pixels that do not exactly align a percentage point of divergence.

        That is, in the following 10 pixel wide overlay:

        x o x o x o oxoxox o x o x

        Because X positions 4 through 7 overlap, that would be 40% similarity.

      2. Or, you might consider not only the fact that two pixels diverge, but also by how much they diverge.

        Meaning that this overlay would rate as having a higher similarity than the one above:

        xxx ooo oxoxox oo xx

        Even though they overlap to the same extent.

        Meaning that:

        xxxxxxxxxx oooooooooo

        Would have a much higher simlilarity rating than:

        xxxxxxxxxx oooooooooo

        Despite there being no overlap at all.

      So then the problem is, what do you consider to be 0% similarity? (Which is why I asked for two pairs of images and associated ratings in the first place.)


      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.

        Please tell me how did you isolate the pink line and that cluster1-cluster8, if through done through a perl code.

        The two images supplied are considered similar to proceed on with the data analysis, the basic criteria is that 80%-90% of the pink curve should be similar.

        And Thanks a lot for help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907472]
help
Chatterbox?
[1nickt]: ping from pm-cb-g

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-10-18 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (244 votes). Check out past polls.

    Notices?