problem with index matrix in 'max' function

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

Moderator: pavanky

problem with index matrix in 'max' function

Postby GBenito » Mon Dec 09, 2013 10:59 am


I'm having trouble with 'max' function. i use max to extract the maximum of a 3D array along dimension 2, and i do it so that it creates an array with the max values, and another array with the respective indexes. The problem is that once i get these arrays, whenever i try to save the index matrix (called MatAng), i get a runtime error (program asks to abort or retry).

I've tried using 'saveimage' as:
af::saveimage("MatAng.jpg", MatAng);

and also tried with a host-side pointer:
float *h_MatAng =<float>();

I have checked the contents of MatAng using 'print', and couldn't find irregularities (it's a matrix filled with only numbers), could it be perhaps a matter of number formats?

Here I include the whole code:

Code: Select all
af::array MatRes (image_H,image_W, ORIENTACIONES);
af::array Promedio= af::constant(0,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);


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");

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);


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

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

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;
MatRes(af::span,af::span,indice) = Promedio;

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

printf("\nDimensiones de Avg:\n");

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("\nDimensiones de MatAng:\n");
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

Re: problem with index matrix in 'max' function

Postby shehzan » Mon Dec 09, 2013 12:12 pm


The saveimage function requires array of type f32. The indices returned from max (MatAng in your code) are of type s32 (signed integer).
The easiest way to save it would be to use the "as()" functions.

User avatar
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: problem with index matrix in 'max' function

Postby GBenito » Mon Dec 09, 2013 12:37 pm

So that was the number format issue!!! Now it works just fine.

I really thank you, as MatAng was the most important matrix for the next step of my algorithm and now y know how 'max' works with indexes so i can keep going!
Posts: 32
Joined: Sun Nov 10, 2013 3:58 pm

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