5
I’m doing a C-level college paper on image processing.
And I need to find an alternative to optimize a particular piece of code. Here:
/* Backward Rotina */
/* -------------------------------------------------------------------------------------------------------------------------------- */
for (altura = img->altura - 2; altura >= 0; altura--)
{
for (largura = img->largura - 2; largura >= 0; largura--)
{
if (aux->dados[0][altura][largura] != 0)
{
if ( aux->dados[0][altura + 1][largura + 1] != 0
&& aux->dados[0][altura + 1][largura + 1] < aux->dados[0][altura][largura])
{
out->dados[0][altura][largura] = aux->dados[0][altura + 1][largura + 1];
}
else if ( aux->dados[0][altura + 1][largura] != 0
&& aux->dados[0][altura + 1][largura] < aux->dados[0][altura][largura])
{
out->dados[0][altura][largura] = aux->dados[0][altura + 1][largura];
}
else if ( aux->dados[0][altura + 1][largura - 1] != 0
&& aux->dados[0][altura + 1][largura - 1] < aux->dados[0][altura][largura])
{
out->dados[0][altura][largura] = aux->dados[0][altura + 1][largura - 1];
}
else if ( aux->dados[0][altura][largura + 1] != 0
&& aux->dados[0][altura][largura + 1] < aux->dados[0][altura][largura])
{
out->dados[0][altura][largura] = aux->dados[0][altura][largura + 1];
}
}
}
}
}
/* ================================================================================================================================ */
Functioning
The function is in the algorithm multi-pass, scanning the image matrix checking the neighborhood. For step foward check the neighbors above and left by going index [1,1] < [X,Y]
, to the backward the right and lower neighbours going from [X-2,Y-2] > [0,0]
. Ignoring the edges because they are unreliable.
Following image of exemplification:
What to do?
I need to find a way to optimize this immense nestled if/else
, that according to the teacher there are better ways to do.
I’ve been plucking my hair for over two hours thinking of a solution, but so far I haven’t been able to come up with a solution that is more viable and at a lower computational cost, all routes lead me to nested if/else
.
Edit
After Foward performs the Backward Routine.
To understand, the algorithm needs to put in
out->dados[0][altura][largura]
one of the neighbors to the "southwest", "south", "southeast" and "east" (assuming that the height grows to the "south" and the width grows to the "east"), the first that is less than the current value, looking in the order "southeast"->"south"->"southwest"->"east", provided that no value of these is zero. Am I right? What does zero mean?– Wtrmute
Correct, the value zero means that that component is a background, and should be ignored. Anything that is non-zero is an image element that is interconnected, this change by a smaller value means that this point is interconnected and are the same component
– AComerlatto