Howdy all,

There is an open-source program that creates an image sa the output files (this comes from my earlier question about drawing chromosomes. I suggested to the primary developer that perhaps it would be useful to add testing to see if "back-end" changes ended up impacting the final product. He said "go for it". So I'm now, err, trying to do that!

So two basic questions:

  1. Is there a general way to compare two binary files and see if they are identical?
  2. Even better, is there a way to calculate the "distance" between two images of the same size?

So far, this is what I came up with:

  1. encode both files (with something like ROT), write the encodings to a file, and do a diff on the encoded files
  2. walk over each image, pixel by pixel and calculate the Euclidean distance between corresponding pixels on the two images

In case #2 isn't clear, I would use something like this:

# two image objects my $image1 = new <some image object>; my $image2 = new <some image object>; # ensure images are same size if ($image1->size != $image2->size) { die "Images different sizes\n"; } # locals my $position = 0; my $cumulative_distance = 0; my %bad_pixels; my $threshold = 100; # loop over all pixels while ($position < $image1->size) { # get current pixels my $pixel1 = $image1->getpixel($position); my $pixel2 = $image2->getpixel($position); my $distance = 0; # calculate distance for each colour $distance += ($pixel1->red - $pixel2->red )^2; $distance += ($pixel1->yellow - $pixel2->yellow)^2; $distance += ($pixel1->blue - $pixel2->blue )^2; $distance = $distance^0.5; # if distance very large, add to bad pixel-list if ($distance > $threshold) { $bad_pixels{$position} = $distance; } $cumulative_distance += $distance; $position++; } my $average_distance = $cumulative_distance / $position; my $bad_pixels = scalar(keys(%bad_pixels)); print "Total Distance: $cumulative_distance\n", "Avg Distance: $average_distance\n", "Deviant Pixels: $bad_pixels\n";

Of course that all requires some image library that lets me walk through pixel-by-pixel and extract the colour values.

Any other (easier?) approaches I could go for? Also, any comments/criticisms of what I've come up with are always appreciated.

-Tats

In reply to Matching Binary Files by Itatsumaki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.