title: Insufficient memory error Opencv/c++
body: <p>When i run this code it give "insufficient memory error"</p>
  
  <pre><code>#include "stdafx.h"
  #include "PixelProcess.h"
  #include "ContourProcess.h"
  
  using namespace std;
  using namespace cv;
  
  int _tmain(int argc, _TCHAR* argv[])
  {
  
      int width = 480, height = 640, N = 3000, framesToLearn = 500, i, w, h, carCounter, PixelCounter, R, G, B, MyResult, c;
      float circularity;
      String fileName; 
      IplImage* image;
      Mat Output  = Mat::zeros(width, height,CV_8UC1);
      Mat Draw = Mat::zeros(width, height,CV_8UC3);
      vector&lt;vector&lt;Point&gt; &gt; contours;
      vector&lt;Point&gt; hull;
      vector&lt;Point&gt;  keys;
      vector&lt;Vec4i&gt; hierarchy;
      vector&lt;Point&gt; polygonRegion;
      Point centroid;
      GaussianMex *F = new GaussianMex[480*640];
  
      int Y[5] = {35,35,360,360,35};
      int X[5] = {223,340,590,1,223};
  
      clock_t begin = 0,end = 0;
  
      for(i = 1; i &lt;= N ; i++){
          if (i&gt;framesToLearn){begin = clock();}
          carCounter = 0;
          PixelCounter = 0;
          fileName = intToStr(i,"D:/Datasets/Elda2ry1/(");
          image = cvLoadImage(fileName.c_str(),CV_LOAD_IMAGE_COLOR);
          // Update the pixel gaussian model
          for(w = 0; w &lt; width ; w++)
              for(h = 0; h &lt; height ; h++)
              {
                  R = uchar((image-&gt;imageData+w*image-&gt;widthStep)[h*image-&gt;nChannels+0]);
                  G = uchar((image-&gt;imageData+w*image-&gt;widthStep)[h*image-&gt;nChannels+1]);
                  B = uchar((image-&gt;imageData+w*image-&gt;widthStep)[h*image-&gt;nChannels+2]);
                  MyResult = F[PixelCounter].Process(R,G,B);
                  Output.row(w).col(h) = MyResult*255;
                  PixelCounter ++;
              }
              if(i&gt;framesToLearn){ //Extract Cars
                  //Output dilation
                  dilate(Output,Output,Mat());
                  //Matrix to draw on
                  cvtColor(Output,Draw,CV_GRAY2RGB);
                  polygonRegion = DrawRegionOfInterest(Draw,X,Y,5);
                  findContours(Output,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point (0,0));
  
                  for(c = 0; c&lt; contours.size(); c++ )//for each contour
                  {
                      //get convex hull and it's centroid
                      convexHull(contours[c],hull,false);
                      centroid = GetCentroidOfConvexHull(hull);
                      //check if the centroid is within the region of interest
                      if (pointPolygonTest(polygonRegion,centroid,false) &gt;=0 )
                          // Check area and Solidity of the convex hull
                          if( contourArea(hull) &gt; 800 &amp;&amp; contourArea(contours[c])/contourArea(hull) &gt; 0.7)
                          {
                              circularity = GetCircularity(hull,contourArea(hull),centroid);
                              // Check circularity wrt the hull position
                              if((centroid.y &lt; width/3 &amp;&amp; circularity &lt;= 0.42)||(centroid.y &gt; width/3 &amp;&amp; (circularity &gt;= 0.33) &amp;&amp; (circularity &lt;= 0.4)))
                              {
  
                                  // Crop the car and save it
                                  fileName = "D:\\Dataset\\F" + itos(i) + "C" + itos(carCounter) + ".jpg";
                                  Rect myRect = cropMyImage(hull,image,fileName);
                              }
                          }
                  }
              }
              clrscr();
      }
  }
  </code></pre>
  
  <p>I found here : <a href="https://stackoverflow.com/questions/8196080/opencv-error-insufficient-memory">OpenCV Error: Insufficient memory</a></p>
  
  <p>that <strong>findcontours</strong> function can cause this error but in my case i declared the vector>  <strong>contours</strong>  at the beginning of the code.</p>