Thank you for posting this. I was able to speed it up some by storing the RGB pixel values in 2D arrays. The image that I am testing originally took 28 seconds to process and I have it down to 12 seconds. I'll cut-n-paste the parts that I changed:
my $gray = 0;
my ( $width, $height ) = $imSrcImg->Get( 'width', 'height' );
my @pixels_red;
my @pixels_green;
my @pixels_blue;
for (my $x = 0; $x < $width; $x++) {
for (my $y = 0; $y < $height; $y++) {
(my $red, my $green, my $blue) = split(',', $imSrcImg->Get("
+pixel[$x,$y]"));
$pixels_red[$x][$y] = $red;
$pixels_green[$x][$y] = $green;
$pixels_blue[$x][$y] = $blue;
}
}
# Iterate over every pixel in the image and change
for my $y ( 1 .. ( $height - 2 ) ) {
for my $x ( 1 .. ( $width - 2 ) ) {
for ( my $i = -1 ; $i <= 1 ; $i++ ) {
for ( my $j = -1 ; $j <= 1 ; $j++ ) {
my $r = $pixels_red[$x + $i][$y + $j];
my $g = $pixels_green[$x + $i][$y + $j];
my $b = $pixels_blue[$x + $i][$y + $j];
my $tmp_GX = $GX[ $i + 1 ][ $j + 1 ];
my $tmp_GY = $GY[ $i + 1 ][ $j + 1 ];
if ($tmp_GX) {
$sumRx += $r * $tmp_GX;
$sumGx += $g * $tmp_GX;
$sumBx += $b * $tmp_GX;
}
if ($tmp_GY) {
$sumRy += $r * $tmp_GY;
$sumGy += $g * $tmp_GY;
$sumBy += $b * $tmp_GY;
}
}
}
The rest of the code is the same.