1

我是 Open CV 的新手,一直在做一个手势识别项目。我正在实现 PCA 并在计算特征向量时。我偶然发现了内存不足错误。我使用的二维数组是动态分配的。仍然会出现同样的错误。这是代码:

    void initialization()
    {
      long int nrows=15000, ncolumns=20;
  int i,j;

      cov = (float**) malloc(nrows * sizeof(float *));     
       for(i = 0; i < nrows; i++) 
     {     

             cov[i] = (float*) malloc(ncolumns * sizeof(float));               
     }         


 matrix = (int**) malloc(15000 * sizeof(int *));     
for(i = 0; i < nrows; i++) 
     {     

             matrix[i] = (int*) malloc(20 * sizeof(int));               
     }         


matrix1 = (int**) malloc(15000* sizeof(int *));     
for(i = 0; i < nrows; i++) 
     {     

             matrix1[i] = (int*) malloc(20 * sizeof(int));               
     } 

      .....                
    } 
    void cal_eigen()
    {

      //mat is the original matrix
    cout<<"Mat conversion"<<endl;

    for (i = 0; i < 10; i++)
    {
  for (j = 0; j < 12300; j++)
  {
    matrix1_clone[i][j]=matrix1[j][i];
  }
    }

    cvInitMatHeader( mat, 12300, 10, CV_64FC1, matrix);
    cvInitMatHeader( matt, 12300, 10, CV_64FC1,matrix1);
     cvInitMatHeader( matty, 10, 12300, CV_64FC1, mat_inv);
         cvInitMatHeader( covar, 12300, 12300, CV_64FC1, cov);

      int i,j;
    int temp1 = 0;



          int arry[mat->cols];
          int arry1[matt->cols];
          int arry2[matty->cols];
           int * temp_array=  ( int *) malloc ( (mat->rows * mat->cols) * sizeof(int));
            int *temp1_array= (int*) malloc ((matt->rows * matt->cols) * sizeof(int));
             int counter=-1;
            for(j=0; j<mat->cols; j++)
           {
            arry[j] = matrix[0][j];// 0-9 values are stored.
            arry1[j] = matrix1[0][j];

           }
         for( i=1; i<mat->rows;i++)
      {
         for ( j=0;j<mat->cols;j++)
          {
                  counter ++;
             temp_array[counter]= matrix[i][j];

             temp1_array[counter]=matrix1[i][j]; 
           }         
    } 

   int k=0;

      for(i=0;i<mat->rows;i++)
     {   
        for ( j=0;j<mat->cols;j++)
           {     

    if(i == 0)
    {
            cvSetReal2D(mat,i,j,(double) arry[j]);
            cvSetReal2D(matt,i,j,(double) arry1[j]);


        }
     else{

         cvSetReal2D(mat,i,j,(double)temp_array[k]);
         cvSetReal2D(matt,i,j,(double)temp1_array[k]);
            k++;  
         }


              }
         }

        for(i=0;i<10;i++)
         { cout<<"ere";
            for ( j=0;j<12300;j++)
              {
              cvSetReal2D(matty,i,j,(double) matrix1_clone[i][j]);

              }
          }
           cvReleaseMat(&matt);
               cvMatMul(mat,matty,covar);

               for(i=0;i<12300;i++) 
         { 
           for(j=0;j<12300;j++) 
        { 
         temp = cvmGet(covar,i,j);
         temp = (temp/12300);
         cvmSet(covar,i,j,temp);  
        } 
        } 

             cvReleaseMat(&mat);
         cvReleaseMat(&matty);

           //ERROR POPS UP HERE 
        CvMat* evec  = cvCreateMat(1,10,CV_64FC1); //eigenvectors
        CvMat* eval  = cvCreateMat(1,10,CV_64FC1);  //eigenvalues (1xN)
        cvZero(evec);
        cvZero(eval);
        cvEigenVV( covar, evec, eval,DBL_EPSILON,0,0);
        cvReleaseMat(&covar);

错误是 OpenCV 错误:

Insufficient memory (Failed to allocate 1210320004 bytes) in OutOfMemoryError, file /home/ukri/src/OpenCV-2.4.2/modules/core/src/alloc.cpp, line 52
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/ukri/src/OpenCV-2.4.2/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 1210320004 bytes in function OutOfMemoryError

如果你们能帮助我解决这个问题,我将不胜感激。

4

0 回答 0