[ Don't top post here ]
I've been playing around with perlMagick (which is a perl interface to
Image-Magic), I think it can decompress the PNG into some binary, but
its way too complex, I get some binary, but I can't tell what it is,
the number of bytes doesn't match the number of pixels, and is not a
multiple. I've been using the perlmagik function getPixels() but I
don't know what the binary its returning is yet.
I'm going to try to match part of the binary from the sub image in the
big image, but its still not making sence.
I don't know what size images you are playing with, how much CPU
and RAM are available. However, the naive implementation of this
functionality is really not that hard.
Below, for convenience, I used the GD library. The code looks for
a 32x32 pattern in a 2816x2112 photo with the pattern pasted in to
roughly the center of the larger image.
perl takes about 4Mb memory when the program below is running.
First, the results:
E:\img> timethis fi
TimeThis : Command Line : fi
TimeThis : Start Time : Wed May 07 15:29:14 2008
Possible match at 1393 1041
Matched line: 0
Matched line: 1
Matched line: 2
Matched line: 3
Matched line: 4
Matched line: 5
Matched line: 6
Matched line: 7
Matched line: 8
Matched line: 9
Matched line: 10
Matched line: 11
Matched line: 12
Matched line: 13
Matched line: 14
Matched line: 15
Matched line: 16
Matched line: 17
Matched line: 18
Matched line: 19
Matched line: 20
Matched line: 21
Matched line: 22
Matched line: 23
Matched line: 24
Matched line: 25
Matched line: 26
Matched line: 27
Matched line: 28
Matched line: 29
Matched line: 30
Matched line: 31
Definite match at 1393 1041 ( 32 x 32 )
TimeThis : Command Line : fi
TimeThis : Start Time : Wed May 07 15:29:14 2008
TimeThis : End Time : Wed May 07 15:29:29 2008
TimeThis : Elapsed Time : 00:00:15.015
That took 15 seconds to find the 32x32 pattern in the larger
image. That is probably not fast enough, but it is a starting
point.
Of course, it might be easier just to convert both images to
binary or ASCII encoded PPM and do the matching from there
(in that case, the regex approach is almost trivial) and I
would guess would be faster than dealing with the repeated
rgb and getPixel calls.
#!/usr/bin/perl
use strict;
use warnings;
use GD;
GD::Image->trueColor(1);
use constant FIND_MULTIPLE => 0;
my ($source, $pattern) = qw( source.png pattern.png );
my $sgd = GD::Image->new( $source );
my $pgd = GD::Image->new( $pattern );
my ( $start_x, $start_y ) = (0, 0);
COORD:
while ( my @coord = find_first_match( $sgd, $pgd, $start_x, $start_y ) ) {
warn "Possible match at @coord\n";
my ( $pw, $ph ) = $pgd->getBounds;
SCAN:
for ( my $py = 0; $py < $ph; $py += 1 ) {
if ( match_hscanline($sgd, $pgd, @coord, $py) ) {
warn "Matched line: $py\n";
}
else {
warn "Failed to match line: $py\n";
$start_x = $coord[0] + 1;
$start_y = $coord[1];
next COORD;
}
}
warn "Definite match at @coord ( $pw x $ph )\n";
last unless FIND_MULTIPLE;
$start_x = $coord[0] + $pw;
$start_y = $coord[1];
}
sub find_first_match {
my ( $sgd, $pgd, $start_x, $start_y ) = @_;
my ( $sw, $sh ) = $sgd->getBounds;
my ( $pw, $ph ) = $pgd->getBounds;
my $lookfor = make_rgb( $pgd->rgb( $pgd->getPixel(0, 0) ) );
for ( my $y = $start_y; $y < $sh - $ph; $y += 1 ) {
for ( my $x = $start_x; $x < $sw - $pw; $x += 1 ) {
if ( $lookfor == make_rgb(
$sgd->rgb( $sgd->getPixel( $x, $y ) ) ) ) {
return my @r = ($x, $y);
}
}
}
return;
}
sub match_hscanline {
my ( $sgd, $pgd, $sx, $sy, $py ) = @_;
my ( $pw, $ph ) = $pgd->getBounds;
for ( my $px = 0; $px < $pw; $px += 1 ) {
return if make_rgb($pgd->rgb( $pgd->getPixel($px, $py)))
!= make_rgb($sgd->rgb( $sgd->getPixel($sx + $px, $sy + $py)));
}
return 1;
}
# memoizing this function does not speed things up
sub make_rgb {
my ( $r, $g, $b ) = @_;
return ( $r << 16 ) | ( $g << 8 ) | $b;
}
__END__
--
A. Sinan Unur <
[email protected]>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/