LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Vision own DLL speed problem or error?

Hello,
i'm trying to calculate the contrast of pictures. At first a split the Array of the picture, so that I get little part arrays in these part arrays I calculate the the standard derivation and the mean score after that I divide these two values so that I get the contrast.
At first I tried to do this in LV (picture) but unfortunately the speed was not high enough, so I tried to write dll and hoped that this would do it more fast. Now the C Compiler doesn't deliver a error message but LV hangs every time I start it. So I don't know if my code is just very slow or if theres a problem with the code that I can't find.
Perhaps anybody here knows how I can improve or correct my C code if theres a problem with it or how I can speed up this calculations by other ways, perhaps theres even a better method in LV that I don't see.

Here is my Code:

[code]
/* Call Library source file */

#include "extcode.h"
#include
#include
#include


#ifdef __cplusplus
extern "C" {
#endif
/* LabVIEW created typedef */
typedef struct {
    int32 dimSizes[2];
    double elt[1];
    } TD1;
typedef TD1 **TD1Hdl;

double* callocvector(int);
double* freevector(double*);
double** callocmatrix(int, int);
double** freematrix(double**,int);


_declspec(dllexport) long KontrastC(long *Brows, long *Bcols, long *Lcols, long *Lrows, TD1Hdl Bild, TD1Hdl Kontrast,long *test);


#ifdef __cplusplus
} // extern "C"
#endif


//Hauptprogramm Kontrastberechnung




long KontrastC(long *Brows, long *Bcols, long *Lcols, long *Lrows, TD1Hdl Bild, TD1Hdl Kontrast,long *test)
{

double** Mittelwert = callocmatrix(*Brows,*Bcols);
int i=0,j=0,m=0,n=0;
double ZWt=1.0/(*Lrows * *Lcols);
int ZWm=0, ZWn=0, ZWk=0;

/* dimSizes[0] is the number of rows */
int numrow = (*Bild)->dimSizes[0];
/* dimSizes[1] is the number of columns */
int numcol = (*Bild)->dimSizes[1];

numrow=floor(numrow / *Lrows);
numcol=floor(numcol / *Lcols);

//(*Kontrast)->elt[(3*numcol)+2]=(*Bild)->elt[(3*numcol)+2]*1000;



// Mittelwertsberechnung
for (m=0;m    {
        ZWm=m* *Lrows;
    for(n=0;n        {
            ZWn=n* *Lcols;
        for (i=0;i<*Lrows;i++)
            {
            for(j=0;j<*Lcols;j++)
                {
                Mittelwert[m][n]=Mittelwert[m][n]+ (*Bild)->elt[((i+ZWm)* numcol) + (j+ZWn)] ;
                }
            }
            Mittelwert[m][n]=ZWt* Mittelwert[m][n];
(*Kontrast)->elt[(m*numcol)+n]=Mittelwert[m][n];
        }
    }



ZWm=0, ZWn=0;

//Kontrastberechnung
for (m=0;m    {
    ZWm=m* *Lrows;
    for(n=0;n        {
        ZWn=n* *Lcols;
        for (i=0;i<*Lrows;i++)
            {
            for(j=0;j<*Lcols;j++)
                {
                ZWk=((*Bild)->elt[((i+ZWm)*numcol) + (j+ZWn)]-Mittelwert[m][n]);
                (*Kontrast)->elt[(m* numcol)+n]= (*Kontrast)->elt[(m*numcol)+n] + ( ZWk*ZWk );
               
                }
            }
            (*Kontrast)->elt[(m*numcol)+n]=ZWt * (*Kontrast)->elt[(m*numcol)+n];
            (*Kontrast)->elt[(m*numcol)+n]=(*Kontrast)->elt[(m*numcol)+n]/Mittelwert[m][n];
        }
    }

//Freigeben des reservierten Speichers
Mittelwert = freematrix(Mittelwert,*Lrows);
return 0;
}




//Unterprogramme zur dynamischen Speicherallokation



/*--------------------------------------------
"callocvector" allocates the memory for a
dynamic vector of length n and initializes it
--------------------------------------------*/

double* callocvector(int n)
{
int j;
double* vector = calloc(n,sizeof(double));

for (j=0; j    {
    vector[j] = 0;
    }
return(vector);
}


/*--------------------------------------------
 "freevector" dis-allocates the memory of
 a dynamic vector of arbitrary length and
 sets the pointer to NULL
--------------------------------------------*/

double* freevector(double* vector)
 {
 free(vector);
 return(NULL);
 }



/*--------------------------------------------
 "callocmatrix" allocates the memory for a
 dynamic matrix of size (m \times n)
 and initializes it
--------------------------------------------*/

double** callocmatrix(int m, int n)
{
int i;
double** matrix = calloc(m,sizeof(double));

for (i=0; i    {
    matrix[i] = callocvector(n);
    }
return(matrix);
}


/*--------------------------------------------
 "freematrix" dis-allocates the memory of
 a dynamic matrix of size (m \times n)
 and sets the pointer to NULL
--------------------------------------------*/

double** freematrix(double** matrix,int m)
{
int i;

for (i=0; i    {
    free(matrix[i]);
    }
    free(matrix);
return(NULL);
}
[/code]
0 Kudos
Message 1 of 2
(2,395 Views)

I'm surprised that your C compiler doesn't throw any errors.

You have a bunch of #includes that aren't followed by file names.  You use a function "floor" that isn't defined anywhere.  And the for loops are like something out of a horror flick - I hope that the code just somehow got mangled on the way to this message post.  In any case, even if that code does compile somehow, you are still performing a ton of calculations at the center of a loop within a loop within a loop within a loop - this will not be fast regardless of the programming language.  My recommendation would be to attempt to simplify the code as much as possible, then look into using pre-calculated tables of values for some of the time-consuming operations, or look into using a third-party mathematics API that efficiently performs these kinds of computations.

Good luck,

Rob

0 Kudos
Message 2 of 2
(2,356 Views)