Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: I need a comparison/hashing algorithm (not the usual).

by BrowserUk (Patriarch)
on Sep 24, 2004 at 19:43 UTC ( [id://393649]=note: print w/replies, xml ) Need Help??


in reply to I need a comparison/hashing algorithm (not the usual).

This is fairly simplistic and quite slow as is, but it does a very good job of matching inverted, flipped and rotated images; pretty good on resized images; and it seems fairly effective on similar images in different formats.

It's currently not good at cropped; negative or greyscaled images, though 24-bit <-> 8-bit colour transforms in either direction seem to matched quite well.

The performance can be improved markedly by using PDL or similar.

#! perl -slw use strict; use Data::Dumper; use List::Util qw[ reduce sum max min ]; use GD; sub check { my $img = GD::Image->new( shift() ); my( $iMin, $iMax, %freq ) = ( 0, 0 ); my( $xMax, $yMax ) = $img->getBounds; for my $y ( 0 .. $yMax - 1 ) { for my $x ( 0 .. $xMax - 1 ) { my $i = reduce{ ($a<<8) + $b;} $img->rgb( $img->getPixel( +$x, $y ) ); $freq{ $i }++; $iMin = $iMin < $i ? $iMin : $i; $iMax = $iMax > $i ? $iMax : $i; } } my $count = $xMax * $yMax; my $ex = 16777216 / ( $iMax - $iMin ); my %norm; while( my( $i, $f ) = each %freq ) { $norm{ ( ( $i - $iMin ) * $ex ) / 16777216 } = $f / $count; } return 100 * sum map{ $_ * $norm{ $_ } } keys %norm; } my %sums = map{ check( $_ ), $_ } map glob, @ARGV; printf "%32s : %7.5f\n", $sums{ $_ }, $_ for sort { $a <=> $b } keys % +sums; __END__

I'd be interested to see pairs of disparate images that produce very close checksums--assuming you are comfortable sharing them:)

This isn't based upon any other algorithms that I am aware of--just made up as I went along--but if anyone see's a relationship between this and prior art I appreciate references.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-29 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found