I'm doing a C-class work on image processing. And I need to find an alternative to optimize a particular piece of code. Here's how:
/* 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];
}
}
}
}
}
/* ================================================================================================================================ */
Operation
The operation is in the multi-pass algorithm, scanning the array of the image by checking the neighborhood. For the foward stage, the above and the left neighbors go from the [1,1] < [X,Y]
indexes, to the backward neighbors of the right and the lower going from [X-2,Y-2] > [0,0]
. Ignoring the edges because they are not reliable.
Here is an example image:
Whattodo?
Ineedtofindawaytooptimizethishugenestedif/else
,whichaccordingtotheteachertherearebetterwaystodo.I'vebeenpullingmyhairformorethantwohoursthinkingaboutasolution,butsofarIhavenotbeenabletocomeupwithasolutionthatismorefeasibleandwithalowercomputationalcost,alltheroutesleadmetonestedwithif/else
.>
Edit
Fowardroutineresult:
AftertheFowardperformstheBackwardRoutine.
ResultRoutineBackward: