错误:内存opencv不足

时间:2011-01-11 23:18:10

标签: c++ c opencv

我正在尝试做一个项目,但是我收到了一个错误,所以请帮助我,错误是这样的:

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

我的代码是这样的:

#include <stdio.h>
#include <stdlib.h>
//#include "/usr/include/opencv/cv.h"
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <math.h>
#include <iostream>
#define PI 3.1415926535898
double rads(double degs)
{
 return (PI/180 * degs);
}
CvCapture *cap;
IplImage *img;
IplImage *frame; 
IplImage *frame1;
IplImage *frame3;
IplImage *frame2;
IplImage *temp_image1;
IplImage *temp_image2;
IplImage *frame1_1C; 
IplImage *frame2_1C; 
IplImage *eig_image; 
IplImage *temp_image; 
IplImage *pyramid1 = NULL;
IplImage *pyramid2 = NULL;
char * mapx;
char * mapy;
int h;
int corner_count;
CvMat* M = cvCreateMat(3,3,CV_32FC1);
CvPoint p,q,_p,_q,l,s;
double hypotenuse;
double angle;
double angle1;

int line_thickness = 1, line_valid = 1, pos = 0;
CvScalar line_color;
CvScalar target_color[4] = { // in BGR order
  {{   0,   0, 255,   0 }},  // red
  {{   0, 255,   0,   0 }},  // green
  {{ 255,   0,   0,   0 }}, // blue
  {{   0, 255, 255,   0 }}   // yellow
};

inline static double square(int a)
{
 return a * a;
}
char* IntToChar(int num){return NULL;}
/*{
    char* retstr = static_cast<char*>(calloc(12, sizeof(char)));
    if (sprintf(retstr, "%i", num) > 0)
    {
        return retstr;
    }
    else
    {
        return NULL;
    }
}*/
inline static void allocateOnDemand( IplImage **img, CvSize size, int depth, int channels )
{
 if ( *img != NULL ) return;

 *img = cvCreateImage( size, depth, channels );
 if ( *img == NULL )
 {
  fprintf(stderr, "Error: Couldn't allocate image.  Out of memory?\n");
  exit(-1);
 }
}
void clearImage (IplImage *img)
{ for (int i=0; i<img->imageSize; i++)
    img->imageData[i] = (char) 0;
}

int main()
 {


/////////////////////////////////////////////////////////////////////////////////////////////
/*                                                                                         */
//                         Capturing from CAM or VIDEO                                     // 
/*                                                                                         */
/////////////////////////////////////////////////////////////////////////////////////////////

cap = cvCaptureFromCAM(0);

//cap = cvCaptureFromAVI("/home/saif/Desktop/NAO.. the project/jj/Test3.avi");


/////////////////////////////////////////////////////////////////////////////////////////////
/*                                                                                         */
//                           READING THE VIDEO'S FRAME SIZE                                // 
/*                                                                                         */
/////////////////////////////////////////////////////////////////////////////////////////////


CvSize frame_size;

frame_size.height = (int) cvGetCaptureProperty( cap, CV_CAP_PROP_FRAME_HEIGHT );
frame_size.width  = (int) cvGetCaptureProperty( cap, CV_CAP_PROP_FRAME_WIDTH );
cvNamedWindow("Optical Flow", CV_WINDOW_AUTOSIZE);
while(true)
 {
  frame = cvQueryFrame( cap );

if (frame == NULL)
 {
  fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
  return -1;
 }



/////////////////////////////////////////////////////////////////////////////////////////////
/*                                                                                         */
//               ALLOCATING ANOTHER IMAGE IF IT IS NOT ALLOCATED ALREDAY                   // 
/*                                                                                         */
/////////////////////////////////////////////////////////////////////////////////////////////


allocateOnDemand( &frame1_1C, frame_size, IPL_DEPTH_8U, 1 );
cvConvertImage(frame, frame1_1C, 0);
allocateOnDemand( &frame1, frame_size, IPL_DEPTH_8U, 3 );
cvConvertImage(frame, frame1, 0);

/////////////////////////////////////////////////////////////////////////////////////////////
/*                                                                                         */
//                                    GET THE SECOND FRAME OF VIDEO                        // 
/*                                                                                         */
/////////////////////////////////////////////////////////////////////////////////////////////


frame = cvQueryFrame( cap );
  if (frame == NULL)
  {
   fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
   return -1;
  }

  if(!frame)
   {
   printf("bad video \n");
   exit(0);
   }

allocateOnDemand( &frame2_1C, frame_size, IPL_DEPTH_8U, 1 );
cvConvertImage(frame, frame2_1C, 0);
allocateOnDemand( &frame2, frame_size, IPL_DEPTH_8U, 3 );
cvConvertImage(frame, frame2, 0);


CvSize optical_flow_window = cvSize(5,5);
eig_image = cvCreateImage( frame_size, IPL_DEPTH_32F, 1 );
temp_image = cvCreateImage( frame_size, IPL_DEPTH_32F, 1 );
CvTermCriteria optical_flow_termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 );

/////////////////////////////////////////////////////////////////////////////////////////////
/*                                                                                         */
//                                Feature tracking                                         // 
/*                                                                                         */
/////////////////////////////////////////////////////////////////////////////////////////////

CvPoint2D32f frame1_features[20];
CvPoint2D32f frame2_features[20];
//cvCornerEigenValsAndVecs(eig_image, temp_image, 1 );
corner_count = 20;

cvGoodFeaturesToTrack(frame1_1C,eig_image , temp_image, frame1_features, &corner_count, 0.1, .01, NULL, 5, 1);

cvFindCornerSubPix( frame1_1C, frame1_features, corner_count,cvSize(5, 5) ,optical_flow_window , optical_flow_termination_criteria);
 if ( corner_count <= 0 )

/******************   Printing out the detected number of features  ********************/ 

  printf( "\nNo features detected.\n" );
 else

  printf( "\nNumber of features found = %d\n", corner_count );

/////////////////////////////////////////////////////////////////////////////////////////////
/*                                                                                         */
//                                LUCAS-KANDE OPTICAL FLOW                                 // 
/*                                                                                         */
/////////////////////////////////////////////////////////////////////////////////////////////
char optical_flow_found_feature[20];
float optical_flow_feature_error[20];

allocateOnDemand( &pyramid1, frame_size, IPL_DEPTH_8U, 1 );
allocateOnDemand( &pyramid2, frame_size, IPL_DEPTH_8U, 1 );


cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features, frame2_features, corner_count, optical_flow_window, 5, optical_flow_found_feature, NULL, optical_flow_termination_criteria, NULL);






cvShowImage("Optical Flow", frame1);
cvWaitKey(50);
}

cvReleaseCapture(&cap);

//cvReleaseMat(&M);
cvDestroyWindow("cap");

return 0;
}

1 个答案:

答案 0 :(得分:5)

每次进行循环时,都会为eig_imagetemp_image创建新缓冲区,并泄漏旧缓冲区。

那些泄露的缓冲区会累积,直到内存不足为止。

相关问题