Saving several matrices into a 3D array

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

Saving several matrices into a 3D array

Postby GBenito » Sat Dec 07, 2013 4:55 pm

Hi,

I'm writing a program in which i create a bank of matrices within a 3d array following these steps:

-i create a loop where process a 2d array "Promedio" with a convolution with certain parameters that vary with each iteration (there are 36 iterations overall).

-Closing each loop, i copy that processed array into a 3d array using the following instruction:

int indice= (int) theta /5;
MatRes(af::span,af::span,indice) = Promedio;

Where "theta" is an angle parameter, which goes from 0 to 175, so "indice" goes from 0 to 35, and MatRes is the 3D array...

Well, this configuration does not copy Promedio into each layer of MatRes, and i'd like to know what am i doing wrong, or which would be the correct way to do so if possible...
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: Saving several matrices into a 3D array

Postby pavanky » Sat Dec 07, 2013 4:57 pm

Can you show more code and the errors you are seeing ?
Pavan Yalamanchili,
ArrayFire
--
~ If it is not broken, you have not tried hard enough ~
User avatar
pavanky
Site Admin
 
Posts: 1123
Joined: Mon Mar 15, 2010 7:39 pm
Location: Atlanta, GA

Re: Saving several matrices into a 3D array

Postby GBenito » Sat Dec 07, 2013 5:56 pm

Here´s the code:

af::array MatRes (image_H,image_W, ORIENTACIONES);
af::array Promedio(image_H,image_W);

for (theta=0 ; theta<=((ORIENTACIONES-1)*5) ; theta += 5){

for (int t= 0 ; t<=4 ; t++ ) {
//int t=0;
double lambda= lambda0 / (pow((float) 2,(float) t));
float menos05 = -0.5;
printf("\n\nIniciando kernel de GABOR %d...\n",t);

af::array CKernelR = af::constant(0,32,32);
af::array CKernelI = af::constant(0,32,32);

printf("%d,%d\n",CKernelR.dims(0),CKernelR.dims(1));

af::array secx(af::seq(32)-16), secy(af::seq(32)-16);

af::array x= af::tile(secx.T(), secy.elements(),1);
af::array y= af::tile(secy, 1,secx.elements());

af::array x_theta = x * cos(theta) + y * sin(theta);
af::array y_theta = -x * sin(theta) + y * cos(theta);

af::array div_pot_x = (pow(x_theta, 2))/ (pow(sigma, 2));
af::array div_pot_y = (pow(y_theta, 2))/ (pow(sigma, 2));

printf("\nDimensiones de div_pot_x:\n");
printf("%d,%d\n",div_pot_x.dims(0),div_pot_x.dims(1));

CKernelR = (1/(sqrt(pi)*sigma)) * exp(menos05 *( div_pot_x + gama *div_pot_y))*cos(2*pi/lambda*x_theta);
CKernelI = (1/(sqrt(pi)*sigma)) * exp(menos05 *( div_pot_x + gama *div_pot_y))*sin(2*pi/lambda*x_theta);

//Valor maximo del filtro
float maxkernelR;
int indexa;
max(&maxkernelR, &indexa, CKernelR);
printf("%g,%d\n", maxkernelR, indexa);

float maxkernelI;
int indexb;
max(&maxkernelI, &indexb, CKernelI);
printf("%g,%d\n", maxkernelI, indexb);

//Valor medio del filtro
float meankernelR = af::mean<float>(CKernelR);
float meankernelI = af::mean<float>(CKernelI);

printf("%f\n",meankernelR);
printf("%f\n",meankernelI);

//Normalizacion del filtro
CKernelR = CKernelR - meankernelR;
CKernelI = CKernelI - meankernelI;

CKernelR = CKernelR / maxkernelR;
CKernelR = CKernelR / maxkernelR;

//Convolucion
af::array filtradoR = convolve(imagen,CKernelR);
af::array filtradoI = convolve(imagen,CKernelI);


af::array sumacuad = pow(filtradoR,2)+pow(filtradoI,2);
float maxcuad;
int ind;
max(&maxcuad, &ind, sumacuad);
af::array energyFil = sumacuad / (maxcuad);

Promedio += energyFil;
}

Promedio= Promedio /5;

int indice= (int) theta /5;
printf("\nIndice:\n");
printf("%d",indice);
MatRes(af::span,af::span,indice) = Promedio;
}

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
af::array Avg = af::mean(MatRes,2);

printf("\nDimensiones de Avg:\n");
printf("%d,%d\n",Avg.dims(0),Avg.dims(1));

af::array Maxi(image_H,image_W);
af::array MatAng(image_H,image_W);
max(Maxi, MatAng, MatRes,2);

printf("\nDimensiones de Maxi:\n");
printf("%d,%d\n",Maxi.dims(0),Maxi.dims(1));

printf("\nDimensiones de MatAng:\n");
printf("%d,%d\n",MatAng.dims(0),MatAng.dims(1));



The errors i'm getting are not runtime type, but content in Avg, Maxi and MatAng arrays... Avg is filled with "1.#NQB"; Maxi is filled with "-3402823466385288700000000000000000000.0000" ; and MatAng is filled with "0"...
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: Saving several matrices into a 3D array

Postby GBenito » Sat Dec 07, 2013 6:39 pm

Seems I found the problem....

Promedio was initiated with no specific content, so it had conflicts with the "Promedio += energyFil" instruction which derived in "not a number" being delivered as final result....

initializing Promedio with the following instruction proved to workout:

af::array Promedio = af::constant(0,image_H,image_W);

Or at least that seems to be the case...
GBenito
 
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm


Return to [archive-commercial] Programming & Development with ArrayFire

cron