|
Posted by A. Sinan Unur on May 7, 2008, 3:36 pm
Please log in for more thread options
[ 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__
--
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
|
|
Posted by A. Sinan Unur on May 7, 2008, 4:58 pm
Please log in for more thread options
> 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)
Actually, I should have said index rather than a regex match.
Sinan
--
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
|
|
Posted by elie on May 7, 2008, 6:28 pm
Please log in for more thread options okay I figured out the pnm format, Its explained in wikipedia :p
http://en.wikipedia.org/wiki/Portable_pixmap
|
|
Posted by elie on May 7, 2008, 6:29 pm
Please log in for more thread options thanks all for your help
|
|
Posted by sheinrich on May 7, 2008, 3:40 pm
Please log in for more thread options > 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.
>
By means of ImageMagick or any other library, I'd first turn both
images into some sort of bitmap format, preferably monochrome, and
reduce their sizes below the threshold of any pixel errors caused by
the transformation.
It should then be poossible to slide the smaller image over the bigger
one and apply some bitwise logic (XOR) to the corresponding pixels.
Cheers, Steffen
|
| Similar Threads | Posted | | how to simply parse raw image data and output to an image file ? | January 3, 2008, 11:19 am |
| best way to store binary image data in a variable using image magick | January 25, 2008, 1:17 am |
| binary searching in perl | October 10, 2006, 2:16 pm |
| Image processing in perl? | September 13, 2007, 11:03 pm |
| Searching database with perl script | June 19, 2006, 2:17 pm |
| Is it possible to resize or crop JPG image with Perl? | August 31, 2005, 8:44 am |
| Perl grabbing an external image | December 15, 2005, 2:36 pm |
| perl GD Image resolution problem | May 5, 2008, 11:28 pm |
| searching a "text" in word document using perl | January 28, 2005, 3:38 pm |
| How good is PERL at searching ASCII files? | December 7, 2006, 10:45 am |
|