A
alexxx.magni
Hi everybody, I need a bit of help on the programming side:
I'm trying to write a program to perform a filtering operation on an
image,
represented as a matrix of integers.
So I'm 1st thing writing a procedure that scans the image, and, when
finding a pixel in the color to be processed, it scans all the
neighboring pixels until all the cluster is processed (and, if its size
is in the correct range, it applies the transform).
Right now I perform this scanning of the cluster in the way I find most
elegant: recursion:
sub recurseinto
{
my ($x,$y)=@_;
my $c;
$flag[$x][$y]=1;
$c=1;
if (($x-1>0)&&($flag[$x-1][$y]==0)&&($img[$x-1][$y]==$col)) { $c +=
recurseinto($x-1,$y) }
if (($x+1<=$width)&&($flag[$x+1][$y]==0)&&($img[$x+1][$y]==$col)) {
$c += recurseinto($x+1,$y) }
if (($y-1>0)&&($flag[$x][$y-1]==0)&&($img[$x][$y-1]==$col)) { $c +=
recurseinto($x,$y-1) }
if (($y+1<=$height)&&($flag[$x][$y+1]==0)&&($img[$x][$y+1]==$col))
{ $c += recurseinto($x,$y+1) }
return $c;
}
Well, it works, but - I hate to admit it - it's slow.
The question is: in which other way you would perform this task?
thanks for any info...
Alessandro Magni
I'm trying to write a program to perform a filtering operation on an
image,
represented as a matrix of integers.
So I'm 1st thing writing a procedure that scans the image, and, when
finding a pixel in the color to be processed, it scans all the
neighboring pixels until all the cluster is processed (and, if its size
is in the correct range, it applies the transform).
Right now I perform this scanning of the cluster in the way I find most
elegant: recursion:
sub recurseinto
{
my ($x,$y)=@_;
my $c;
$flag[$x][$y]=1;
$c=1;
if (($x-1>0)&&($flag[$x-1][$y]==0)&&($img[$x-1][$y]==$col)) { $c +=
recurseinto($x-1,$y) }
if (($x+1<=$width)&&($flag[$x+1][$y]==0)&&($img[$x+1][$y]==$col)) {
$c += recurseinto($x+1,$y) }
if (($y-1>0)&&($flag[$x][$y-1]==0)&&($img[$x][$y-1]==$col)) { $c +=
recurseinto($x,$y-1) }
if (($y+1<=$height)&&($flag[$x][$y+1]==0)&&($img[$x][$y+1]==$col))
{ $c += recurseinto($x,$y+1) }
return $c;
}
Well, it works, but - I hate to admit it - it's slow.
The question is: in which other way you would perform this task?
thanks for any info...
Alessandro Magni