Getting colors from multiple regions of an image (perl)
Posted: 2013-02-12T08:46:18-07:00
I'm trying to clean up and center some bitonal page images from books. I've done this with a perl script that divides the page into a grid and looks for the percentage of white in each region. So the page essentially looks like this, except bigger:
The script works but it is unacceptably slow for a 400-page book. Right now I put "convert" into a nested loop, something like this:
(temp.tif is a version of the original image downsized to 25%)
The problem for me is the crop. I tried using -region so I could change that repeatedly in one command but Imagemagick seems to ignore it here and gives me the percentage of white in the whole image. So I have to re-read and re-crop the image over 600 times per page. So any ideas how to speed the process up?
I have PerlMagick installed and I'm thinking there might be a more efficient way with that, read the image once into memory and operate repeatedly on that. Is there a way to crop a region in PerlMagick to get the info I want, then undo the crop and recrop at the new coordinates? I see in the documentation that omitting the x and y offsets for the crop command will generate a series of images. That sounds like it could be more efficient. Can PerlMagick do this and store the segments in an array or something so I can loop through them all? Or is there a simple ImageMagick command that I've overlooked?
Arvin
Code: Select all
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0
Code: Select all
my $y = int (($height / $height_incr) - .5);
my $dy = 0;
until (($dy + $y) > $height) {
my $dx = 0;
my @values;
until ($dx >= ($width - $width_incr)) {
my $region = "${width_incr}x$y+$dx+$dy";
my $cmd = "convert -crop $region temp.tif -format \"%[fx:100*mean]\" info:";
open (INFO, "$cmd |");
my $info = <INFO>;
chomp ($info);
close (INFO);
push @values, $info;
$dx += $width_incr;
}
$dy += $y;
}
The problem for me is the crop. I tried using -region so I could change that repeatedly in one command but Imagemagick seems to ignore it here and gives me the percentage of white in the whole image. So I have to re-read and re-crop the image over 600 times per page. So any ideas how to speed the process up?
I have PerlMagick installed and I'm thinking there might be a more efficient way with that, read the image once into memory and operate repeatedly on that. Is there a way to crop a region in PerlMagick to get the info I want, then undo the crop and recrop at the new coordinates? I see in the documentation that omitting the x and y offsets for the crop command will generate a series of images. That sounds like it could be more efficient. Can PerlMagick do this and store the segments in an array or something so I can loop through them all? Or is there a simple ImageMagick command that I've overlooked?
Arvin